Note: This article is now outdated due to changes in the Enterprise Library.
Introduction
This code adds support for MySQL database access to the Data Access Application Block of the Microsoft Enterprise Library.
Background
The Microsoft Enterprise Library defines and implements "Application Blocks". Application blocks are reusable software components designed to assist developers with common enterprise (or not) development challenges.
This code uses the native MySQL .NET Connector assembly.
Rebuilding the Data Application Block
In order to get MySQL support, you need to rebuild the Data Application Block. Under the default install path (C:\Program Files\Microsoft Enterprise Library\src\Data), create a MySql subfolder and extract the source files (MySqlDatabase.cs and MySqlCommandWrapper.cs). Open the Data.sln project and add these two files to the Data project. Add also a reference to the MySql.Data.dll assembly. Rebuild.
Adding a new connector to the Data Access block
A new connector is a class which derives from the abstract Database
class. It must at least override all abstract functions. A new command wrapper class MySqlCommandWrapper
which implements the DBCommandWrapper
class helps limiting the functionalities (not supported stored procedure access, ...). See the files in C:\Program Files\Microsoft Enterprise Library\src\Data\Sql\ folder to go further.
Using the code
Here is a simple and standard database access using the Data Application Block which selects columns "name" and "value" from the table "test":
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "SELECT name, value FROM test LIMIT 1,20";
DBCommandWrapper dbCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand);
StringBuilder readerData = new StringBuilder();
using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
while (dataReader.Read())
{
readerData.Append(dataReader["name"]);
readerData.Append( " = " );
readerData.Append(dataReader["value"]);
readerData.Append("<br>");
}
}
Improvements
- This connector is based on the release version of MySQL (not the beta) available at the time it is written. This version does not support stored procedures, but you can implement them easily.
- The database independence provided by the Data Access Block is somewhat useless when using direct SQL syntax, because MySQL syntax is not T-SQL compatible. There is an open-source project (can't remember its name) which can be used to create database independent SQL queries. It would be nice to integrate it also.
Learn more
History
- 2005-06-09 - First release.