Welcome!

.NET Authors: Liz McMillan, Yakov Werde, Matthew Pollicove , Kevin Benedict

Related Topics: .NET

.NET: Article

An Introduction to MySQL, LAMP Stack and Microsoft Windows Development

Use a free alternative to SQL Server or Oracle

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
The connection strings are also analogous to an SqlConnection connection string. Here is an example that connects to the same database as our ODBC example above.

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.

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.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
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.