Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ADO.NET & Database » Access MySQL Server Database using ODBC data provider

Access MySQL Server Database using ODBC data provider

In this article, I will show you how to access MySQL server database using ODBC data provider.

Author Rank :
Page Views : 32859
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

If you are new to ODBC data provider, please try Understanding ODBC .NET Data Provider.

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.

The following text is taken from Chapter 11: Working with ODBC .NET Data Provider of A Programmer's Guide to ADO.NET in C#. This chapter teaches you how to work with various data sources such as Oracle, MySQL, Excel, Access, Text, and Sybase using ODBC data provider.

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:

string connectionString = @"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:

OdbcConnection conn= 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 11-2 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.

Listing 11-2. Accessing a MySQL database

private void Form1_Load(object sender, System.EventArgs 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();
}

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line 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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
How to connect MySQL With VB by ganesh On June 14, 2007
what is the connection string of VB with MySQL Database? i am new to mysql.
Reply | Email | Modify 
Re: How to connect MySQL With VB by Mahesh On June 18, 2007

Connection string is same for VB and C#. Try this:

Dim connectionString as string = "Driver= MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;"

Reply | Email | Modify 
connection Pooling occour with mysql by sami On July 11, 2007
connection Pooling occour with mysql in asp.net (C#)
Reply | Email | Modify 
mysql dataadapter by john On January 8, 2008
i want to know about mysql data adapter because i want to change my project database sql to mysql
Reply | Email | Modify 
How to backup MySql Database using C# by sh On January 23, 2008
I am trying to backup MySql Database in C#. I have written following code, but it does create only first around 9 lines of backup with meta information. String tmestr = backupTime.ToString(); tmestr = "C:\\"+year+"-"+month+"-"+day+"-"+hour+"-"+minute+".sql"; StreamWriter file = new StreamWriter(tmestr); ProcessStartInfo proc = new ProcessStartInfo(); string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} > {4};", "username", "{(password)}", "localhost", "database_tobe_backup", "backupfile.sql"); proc.FileName = "mysqldump";//"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump.exe"; proc.RedirectStandardInput = true; proc.RedirectStandardOutput = true; proc.Arguments = cmd;//"-u root -p smartdb > testdb.sql"; proc.UseShellExecute = false; Process p = Process.Start(proc); string res; res = p.StandardOutput.ReadToEnd(); file.WriteLine(res); p.WaitForExit(); file.Close();
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.