YOUR FEEDBACK
DataCore and Egenera Combination Delivers Next Generation Server and Storage Virtualization
Virtualization news for the channel community and you ! wrote: Trackback A...

SYS-CON.TV
TOP MICROSOFT .NET LINKS


An Introduction to MySQL, LAMP Stack and Microsoft Windows Development
Use a free alternative to SQL Server or Oracle

Digg This!

Page 2 of 2   « previous page

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.


Page 2 of 2   « previous page

About 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 wrote: 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.
read & respond »
.NET News Desk wrote: 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.
read & respond »
MICROSOFT .NET LATEST STORIES
Desktop Virtualization Market to be Worth at Least $1.8b by 2012 Up From Nothing
Pushing back against VMware, its chief rival, Tuesday, Citrix released its ballyhooed, on-demand XenDesktop, the widgetry that delivers custom, managed virtual Windows desktops from a data center server to a user over the network, and priced the stuff. Theres a free Express Edition for
Xenocode Introduces New Application Virtualization Technology
Xenocode launched its flagship offering, Xenocode Virtual Application Studio. Xenocode Virtual Application Studio is a next-generation application virtualization environment that allows Windows, .NET and Java-based desktop applications to be deployed in standalone executables that run
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
AJAX World - Deploying an ASP.NET AJAX RSS Reader on Linux
Have you ever wished you could run ASP.NET applications on Linux, without having to rewrite your code or leave the Visual Studio development environment? In this article, I show you how to port Steve Clements' AJAX ASP.NET RSS Reader to native Java and deploy it to Apache Tomcat on Lin
Citrix and Microsoft Unveil New Branch Office Application Delivery Solution
Citrix and Microsoft announced the availability of Citrix Branch Repeater , an innovative new line of branch office appliances developed and marketed as part of a strategic alliance between the two companies. By staging the delivery of applications and Windows services closer to branch
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING NEWS FROM THE WIRES
Juniper Gaining Ground Among Networking Pros as an Exciting Vendor -- New Research From TheInfoPro
TheInfoPro (TIP), an independent research network and leading supplier of market intelligence