The following code snippet shows how to connect to a MySQL database using ADO.NET and C#.
string ConnectionString = "Driver={MySQL};SERVER = localhost; " + "DATABASE= NorthwindMySQL; ";
OdbcConnection conn = new OdbcConnection(ConnectionString);
conn.Open();
OdbcDataAdapter da = new OdbcDataAdapter
("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn);
DataSet ds = new DataSet("cust");
da.Fill(ds, "Customers");
dataGrid1.DataSource = ds.DefaultViewManager;
conn.Close();
You must have MySQL drive installed on your machine. You also need to replace SERVER and DATABASE names to your server name and your database name.
In the above code, the data is coming from the Customers table. You need to make sure to replace your table name and table column names.
Cheers!