views:

57

answers:

3

i need to get started with accessing a database with c#

please give me the simplest example possible!!

perhaps a mysql database would be the simplest example?

please show me how to connect to a mysql database and get data

+1  A: 

This is a decent example of accessing databases in .net from first principles.

amelvin
+2  A: 

If you want to use MySQL, you'll need to get a .Net Data Provider for MySQL or a MySQL ODBC driver.

Or you could install SQL Server Express Edition (free download).

Then just walk through a beginner tutorial, of which there are plenty on the web: MSDN tutorial here, one here, another here, and here is a simple MSDN sample covering ADO, ODBC, OleDB.

Really there is a huge wealth of help out there.

Charles
A: 

Scott Guthrie wrote a very nice (and lengthly) tutorial on data access using ASP.NET.

To keep things simple, you only need to look at the following:

  1. tutorial 1 - focus on option 2
  2. tutorial 2 - skip the part about advanced options

Guthrie provides code in VB.NET. I'll rewrite the code in C# (which should be placed within the scope of the Page_Load method under Default.aspx):

NorthwindTableAdapters.SuppliersTableAdapter suppliersAdapter = 
    new NorthwindTableAdapters.SuppliersTableAdapter();

Northwind.SuppliersDataTable suppliers = suppliersAdapter.GetAllSuppliers();

foreach (Northwind.SuppliersRow supplierRow in suppliers)
    Response.Write("Supplier: " + supplierRow.CompanyName + "<br />");

By the way, the entire tutorial is worth reading.

Jimmy W