Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Connecting to a MySQL Database in ADO.NET
WhatsApp
Mahesh Chand
4y
26k
0
1
100
Article
Take the challenge
Connecting to a MySQL Database in ADO.zip
This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
As you can see, working with different data sources means nothing except changing the connection string. Listing 5-27 shows the connection string for the MySQL database. You access a MySQL database through odbc data providers. (I'll discuss My SQL database connectivity and show how to install an ODBC driver for MySQL with a step-by-step example later article). As you can see from Listing 5-27, you can use a similar database as Northwind. To provide a similar sample, I exported the Access 2000 Northwind database as a MySQL database. You can use any database. Just replace the database name and change the SQL statement. To test this application, create a Windows application, drop a data grid to the form, add a reference to the System.Data and Microsoft.Data.Odbc namespaces, and type the following code in Listing 5-27 on the Form_load event.
Listing 5-27. Reading data from a MySQL database using ODBC
private
void
Form1 _Load(
object
sender, System. EvetArgs e)
{
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();
}
Conclusion
Hope this article would have helped you in understanding Connecting to a MySQL Database in ADO.NET. See my other articles on the website on ADO.NET.
This essential guide to Microsoft's ADO.NET overviews C# then leads you toward a deeper understanding of ADO.NET.
Connecting to a MySQL Database in ADO.NET
MySQL
ODBC
System.Data Microsoft.Data.Odbc
People also reading
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found