ARTICLE

Connecting to a MySQL Database in ADO.NET

Posted by Rahul Kumar Saxena Articles | ADO.NET in VB.NET February 24, 2010
In this article I will explain Connecting to a MySQL Database in ADO.NET.
 
Reader Level:

As you can see, working with different data source 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 databases 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 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 Form1 Sub _Load(ByVal sender As Object, ByVal EvetArgs As System.)
    Dim ConnectionString As String = "Driver={MySQL};SERVER = localhost; " + "DATABASE= NorthwindMySQL; "
    Dim conn As OdbcConnection = New OdbcConnection(ConnectionString)
conn.Open()
    Dim da As OdbcDataAdapter = New OdbcDataAdapter("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn)
    Dim ds As DataSet = New DataSet("cust")
da.Fill(ds, "Customers")
dataGrid1.DataSource = ds.DefaultViewManager
conn.Close()
End Sub

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor