Background

Microsoft's Enterprise Library for .NET Framework 2.0 is a framework to base your code around, it's flexible and extendable. The Data Access Block proves a database independent implementation layer for you to add into your code. Although it's database independent, there are only two existing providers supplied with the data access block, SQL Server and Oracle. This article shows you how to add MySql to this list, infact using the ideas from the article it should be possible to implement any .NET database connector as a Data Access Block (well, that's the idea... But actually doing it might be quite tricky).

Before We Start...

Just a quick warning before we start, this code works for me and my uses; connecting to MySql and running simple stored procedures (passing in parameters). I've not done a huge amount of work testing all the features of the MySql Data block work.... To be honest, they don't!! The more observant of you may notice that there are some references to the MSSQL objects in the MySQL class - I'll get around to fixing it sometime or someone else might do it for me!! :-)

Getting Down and Dirty...

We have to go through a number of steps. They involve adding a couple of new classes (MySql Data Blocks) and some modifications to the Data Blocks themselves (which I have supplied in the source zip file). From looking through the code, I don't think I needed to do this, but the integration is much tighter.

Good luck!!

Start Hacking

Create a sub-folder called 'MySql' in the directory containing the Data Access block source, for me it is C:\Program Files\Microsoft Enterprise Library January 2006\Src\Data\Download the source, and copy them into the 'mysql' folder.

Add a reference to the MySQL.Data DLL in references.

Time to hack the Data Access block. In the method (in the file DatabaseConfigurationView.cs):

///<item>For provider name "System.Data.SqlClient",
/// or for a provider of type <seecref="System.Data.SqlClient"/>, the
///<seecref="Microsoft.Practices.EnterpriseLibrary.Data.MySql.MySqlDatabase"/>/// will be used.</item>

It's quite clear where to add it, it's at the top of the file, just under the namespace declaration (with the rest of the XML).

You will have to add a using MySql.Data.MySqlClient; to DatabaseConfigurationView.cs and DbProviderMappings.cs to get it to compile.

Bob's Your Uncle

Well almost, I've tested normal SQL statements, they work, and stored procs work too. Well, in the limited way, I've tested them. Now you give it a go...

I've create a simple demo project to go with the code. Have fun and let me know how you get on! I've not supplied any SQL code (hopefully you should be able to get it working with your own!)

Points of Interest

As well as the MySql classes, I've included the two from the Data Access Block that I changed, so you don't have to do the typing yourselves. :-)

I found using the Application Blocks very easy, much easier than I expected, so go on, give it a try.

I've been doing some playing since I wrote this, and I've had some issues with MySql .NET provider having not being (fully) CLS-compliant [MySqlDbType]. Klaus Frederiksen has already answered this on the MySql Forums.

The Test App

In the test app, the first line which sets up the database connection is: