| By Ben Reichelt | Article Rating: |
|
| November 7, 2005 02:45 PM EST | Reads: |
22,453 |
Let's use that connection string and fill a dataset with some data. I have re-created the sample Northwind database that comes with SQL Server on my MySQL database and I will be referencing the "customers" table from Northwind in my queries.
OdbcConnection connection = new OdbcConnection(connectionString);
OdbcCommand command = new OdbcCommand();
command.Connection = connection;
command.CommandText = "SELECT * FROM customers";
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
DataSet ds = new DataSet();
connection.Open();
adapter.Fill(ds);
connection.Dispose();
That's all there is to it! Does it look familiar? It should, be-cause this is no different from how you can fill a dataset regardless of whether the data comes from MySQL, SQL Server, Access, or Oracle. The only thing that's different is the class names that we are using: OdbcConnection instead of SqlConnection, OdbcCommand instead of SqlCommand, etc. At this point, you should be able to use MySQL using ODBC and completely forget what kind of database you are connecting to. Since MySQL supports the SQL standard, you don't need to learn anything new, un-less you want to use the MySQL extensions to the SQL language, which is beyond the scope of this article.
Using ODBC is a great way to easily connect to a MySQL database in your .NET code. However, by using ODBC, which is designed to connect to a multitude of data sources, you sacrifice speed. One way to boost performance is by using a data provider that is designed explicitly for your particular data source. In our case, MySQL offers a data provider for .NET that specifically targets its database, called "MySQL Connector/Net," and formerly called "ByteFX.Data" (http://dev.mysql.com/downloads/connector/net/1.0.html). This data provider is open source and developed entirely in managed code. I recommend to anyone who is using this data provider that they download the source code as well, and simply browse through the source. It's an interesting experience (at least to me) to see how a data provider is actually implemented. The cool thing about MySQL Connector/Net's being open source and having been written in managed code is that you can make changes to suit your needs if need be. I wanted to use the connector in my ASP.NET 2.0 projects, so I downloaded the source and compiled it against .NET 2.0, and I was ready to go! Just because there isn't a released version for .NET 2.0 doesn't mean you can't make your own.
MySQL Connector/Net implements all of the same ADO.NET provider interfaces that you would expect:
- MySqlConnection
- MySqlCommand
- MySqlDataReader
- MySqlDataAdapter
- MySqlCommandBuilder
string connectionString = "Server=localhost;Database=northwind;Uid=user;Pwd=pass;";
So using our new connection string and our new set of provider classes, let's get the same customer dataset that we got above.
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandText = "SELECT * FROM customers";
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
DataSet ds = new DataSet();
connection.Open();
adapter.Fill(ds);
connection.Dispose();
Since the MySQL Connector/Net implements the same interfaces as the other ADO.NET interfaces, data access is as simple as changing the class names. While the changes only seem cosmetic, the underlying changes are significant. MySQL Connector/Net speaks the MySQL database protocol natively, so there is no need to install the MySQL ODBC Driver as we had to do in order to use the System.Data.Odbc namespace above. Because we don't need this separate library, we can attain better performance results as we are eliminating a logical layer from our data access.
With the advent of .NET and the ever-improving MySQL database server, developing applications with open source components is becoming easier. With several class libraries that support connecting to a MySQL database from our .NET code, and with the suite of applications and tools available directly from MySQL, a strong case can be made to lower your business's TCO by using an open source database over more expensive commercial options.
Published November 7, 2005 Reads 22,453
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Ben Reichelt
Ben Reichelt is a software developer for Magenic Technologies Inc., a software consulting company based in Minneapolis that specializes in Microsoft solutions. He has been working in software for three years and lives in Saint Paul, MN with his wife Erin. You can catch up with Ben by reading his blog at http://codebetter.com/blogs/ben.reichelt.
![]() |
Trevor 11/08/05 11:10:08 AM EST | |||
Don't forget about EMS MySQL Manager Lite Edition (completely free for *commercial* use as well!) which can be found at www.sqlmanager.net. Nice article though for beginners. |
||||
![]() |
.NET News Desk 11/07/05 03:47:43 PM EST | |||
An Introduction to MySQL, LAMP Stack and Microsoft Windows Development. When open source software is mentioned, one of the first thoughts that comes to mind is the LAMP stack (Linux, Apache, MySQL, and PHP/Perl/Python). While these products are not generally associated with Windows development, at least one of them is certainly well suited for Windows development, particularly when using .NET. The MySQL database server is an open source relational database developed by MySQL AB. MySQL is available for Windows, Linux, UNIX, and the Macintosh operating system. Using MySQL with Windows has never been easier with .NET and ADO.NET. |
||||
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications




























