Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ADO.NET & Database » Accessing MySQL Database in VB.NET

Accessing MySQL Database in VB.NET

In this article, I will show you how to access MySQL server database using ODBC data provider. There are two ways to access MySQL Server database using ODBC data providers. First, using ODBC data source name and second by passing connection string direct in the OdbcConnection object.

Author Rank:
Technologies: .NET 1.0/1.1, ADO.NET,Visual Basic .NET
Total downloads :
Total page views :  57197
Rating :
 2.67/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor


Related EbooksTop Videos

In this article, I will show you how to access MySQL server database using ODBC data provider. There are two ways to access MySQL Server database using ODBC data providers. First, using ODBC data source name and second by passing connection string direct in the OdbcConnection object.

Using MySQL Database

As I've been saying, working with different data sources is only a matter of changing the connection string. You can access a MySQL database either using a DSN or using the  direct database name in the connection string. You can use a database name directly as shown in the following code:

Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;"

Or you can use an ODBC DSN, as you can see from the following code that I've used TestDSN DSN to connect to the data source:    

Dim conn As New OdbcConnection("DSN=TestDSN")

To test this code, create a Windows application and add a DataGrid control to the form and write code listed in Listing 1 on the form load. As you can see in Listing this code is similar to the code you saw earlier. It creates a connection, a data adapter, fills the dataset from the data adapter, and sets the dataset's DefaultViewManager as the DataGrid control's DataSource property.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;" '
Dim conn As New OdbcConnection(connectionString)
conn.Open()
Dim da As New OdbcDataAdapter("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn)
Dim ds As New DataSet("Cust")
da.Fill(ds, "Customers")
dataGrid1.DataSource = ds.DefaultViewManager
conn.Close()
End Sub 'Form1_Load

Listing 1. Accessing a MySQL database

Note:

In this code, I have converted Northwind Access database to MySQL database. You make sure you replace your database name, table, user ID and password.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him at mahesh AT c-sharpcorner DOT com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
where is the Listing 11-12rolin3/2/2006

hi there...

u mentioned in ur article : "To test this code, create a Windows application and add a DataGrid control to the form and write code listed in Listing 11-2 on the form load."

but i can only see Listing 1. so where can i find the Lingting 11 and 12?

thank you...

Reply | Email | Delete | Modify | 
 
 
Re: where is the Listing 11-12Mahesh9/28/2007
It supposed to be Listing1. I fixed it. Thanks.
Reply | Email | Delete | Modify | 
how about...?jhae9/27/2007
how about... ADo.net and msql in to VB2005? & ADO.net and/or msql in to VCpp.net
Reply | Email | Delete | Modify | 
ado.netsrivatsan10/29/2007
the article is useful.....
Reply | Email | Delete | Modify | 
thankskyoshiro11/19/2007
this article really is reliable. thanks !
Reply | Email | Delete | Modify | 
OdbcConnection not definenell12/14/2007
i was trying your code in listing 1 and i have this error on my vb.net code OdbcConnection not define what seems to be the problem?
Reply | Email | Delete | Modify | 
Questionmehrdad1/28/2008
hi mahesh I'm mehrdad from Iran Please tell me a reference for VB.net Database Sql server 2005 and asp.net thak you
Reply | Email | Delete | Modify | 
 
 
Re: QuestionMahesh4/7/2009
Click on ADO.NET link in the left side bar on the home page of this site. ADO.NET section has many articles on database accessing using ADO.NET and VB.NET. Post your questions on the forums if you can't find your answer.
Reply | Email | Delete | Modify | 
give a ideasamiullah2/9/2008
Dear Sir my name is samiullah im student of computer science in afhanistan. Dear sir i know some basic thing about vb.net and i have developed some small databases like for shops and schools. dear sir i wanna to learn vb.net more here in afghanistan are not smart teachers that i learn something new,if you dont mind would you please send some lecture that i learn something from your notes i wil be very glad best regard samiullah
Reply | Email | Delete | Modify | 
 
 
Re: give a ideaMahesh4/7/2009
This site and C# Corner are all filled with lot of tutorials and articles. Start with beginner section at the top and post your questions on the forums.
Reply | Email | Delete | Modify | 
give a idea2samiullah2/9/2008
dear sir i forgot something to tell you that im make connectiveties with sqlserver2000 toward that send me lectures please sir bye
Reply | Email | Delete | Modify | 
conneccting to an access database using ADO.netDamion8/17/2008
Mahesh, I notice that the codes for connecting to an access database are not as they were with VB6. I am trying to connect to an access database in ado.net. How is that done? That was a good article by the way!
Reply | Email | Delete | Modify | 
 
 
Re: conneccting to an access database using ADO.netMahesh4/7/2009
Here is an article in C#. Conversion to VB.NET should be pretty straight forward:
http://www.c-sharpcorner.com/UploadFile/mimrantaj/Database102102008130743PM/Database1.aspx
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved