5,276,406 members and growing! (15,814 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Intergrating MySql with Enterprise Library for .NET Framework 2.0

By Mark Pryce-Maher

An article on adding a MySql connector into Enterprise Library for .NET Framework 2.0
C#, Windows, .NET, Visual Studio, MySQL, DBA, Dev

Posted: 13 Sep 2006
Updated: 7 May 2007
Views: 25,475
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.53 Rating: 3.53 out of 5
0 votes, 0.0%
1
4 votes, 40.0%
2
1 vote, 10.0%
3
2 votes, 20.0%
4
3 votes, 30.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - mysql.gif

Introduction

IntroductionI have been looking at Microsoft's Enterprise Library for .NET Framework 2.0[^] and I wondered how difficult it would be to extend the Data Access Application Block to include MySQL as one of the databases.

Standing on the Shoulders...

I found Benjamin Mayrargue's article: MySQL connector for Microsoft Enterprise Library (January 2005 version). I followed the steps he took, just to see if I could get it to work.
It didn't because it was written for a previous version of the Enterprise Library, but his article gave me all the information I needed to have a stab at doing it myself.

I also looked at the way the Oracle and SQL Server blocks were built, copied how they were built.

Assumptions

I have made a few assumptions;

  • Have Visual Studio 2005 installed. (I have the Full version)
  • Install the Enterprise Blocks for .Net 2[^] (check they compile before you start messing with them!!)
  • Install the MySQL Connector/Net 5.0[^] (I used the Alpha release, I don't know if it will work with the 1.0 connector)
  • Finally... Have a rough idea what you are doing. :-)

    Background

    Microsoft's Enterprise Library for .NET Framework 2.0 is a framework to base your code around, its flexible and extendable. The Data Access Block proves a database independant implementation layer for you to add into your code, although its database independant, 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 thats 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 dont!! The more observant of you may notice 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 dont think I needed to do this, but the intergration is much tighter.

    Good luck!!

    Start hacking

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

    2) Add a reference to the MySQL.Data Dll in references.

    3) Time to hack the Data Access block.
    In the method (in the file DatabaseConfigurationView.cs)
    private DbProviderMapping GetDefaultMapping(string name, string dbProviderName)

    add the following code
    if (DbProviderMapping.DefaultMySqlProviderName.Equals(dbProviderName))                
                return defaultMySqlMapping;
    


    It should be obvious where it goes.

    4) In the file DatabaseConfigurationView.cs add the following line.
       
            private static readonly DbProviderMapping defaultMySqlMapping = new DbProviderMapping(DbProviderMapping.DefaultMySqlProviderName, typeof(MySqlDatabase));
            private IConfigurationSource configurationSource;
            

    So the top of the file reads;
        public class DatabaseConfigurationView
    
        {
    
            private static readonly DbProviderMapping defaultSqlMapping = new DbProviderMapping(DbProviderMapping.DefaultSqlProviderName, typeof(SqlDatabase));
    
            private static readonly DbProviderMapping defaultOracleMapping = new DbProviderMapping(DbProviderMapping.DefaultOracleProviderName, typeof(OracleDatabase));
    
            private static readonly DbProviderMapping defaultGenericMapping = new DbProviderMapping(DbProviderMapping.DefaultGenericProviderName, typeof(GenericDatabase));
    
                    private static readonly DbProviderMapping defaultMySqlMapping = new DbProviderMapping(DbProviderMapping.DefaultMySqlProviderName, typeof(MySqlDatabase));
    
            private IConfigurationSource configurationSource;
            


    5) In DbProviderMappings.cs add the following xml/code
      
        /// <item>For provider name "System.Data.SqlClient", or for a provider of type <see cref="System.Data.SqlClient"/>, the
        /// <see cref="Microsoft.Practices.EnterpriseLibrary.Data.MySql.MySqlDatabase"/> will be used.</item>
        

    Its quite clere where to add it, its at the top of the file, just under the namespace declaration (with the rest of the XML)

    6) Paste this code

            
            /// <summary>
            /// Default name for the MySQL managed provider.
            /// </summary>
            public const string DefaultMySqlProviderName = "MySql.Data.MySqlClient";
    
            
    under the

    public const string DefaultOracleProviderName = "System.Data.OracleClient";
    

    line.
    7) 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 statments, 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 dont 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
     Database db = DatabaseFactory.CreateDatabase("MySql");
    

    The MySql bit refers to name in the following line in the app.config
    <add name="MySql" connectionString="Data Source=YourServerName;Database=YourDataBase;User ID=YourUserID;Password=YourPassword;"
          providerName="MySql.Data.MySqlClient" />
    

    History

    V1.0 First Release
  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Mark Pryce-Maher


    I've been a programmer for longer than I care to remember. It all started with those BBC (model B's) at school, it all went down hill from there.....

    This year, I am mostly coding in.. C# (WPF & WCF)

    Blog Address http://markpm.blogspot.com


    Occupation: Web Developer
    Location: United Kingdom United Kingdom

    Other popular Database articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
    Subject  Author Date 
    GeneralIt has it errors but...memberMario Serrano (makadown)8:17 8 Jan '08  
    Generalbuild errorsmemberstephan restocity0:43 17 Nov '07  
    GeneralHot to add encode utf-8 or gb2312 to the block?memberwishsky1:31 20 Oct '07  
    Generalwhat can i do for it to make GB2312 work?memberwishsky1:27 20 Oct '07  
    GeneralEnterprise 3.1memberRicardo Luceac3:44 16 Oct '07  
    GeneralRe: Enterprise 3.1memberMark Pryce-Maher4:14 16 Oct '07  
    GeneralRe: Enterprise 3.1memberexmarouane22:13 26 Mar '08  
    QuestionError while running the demo applicationmemberRajendranK2:39 6 Oct '07  
    AnswerRe: Error while running the demo applicationmemberMark Pryce-Maher23:03 8 Oct '07  
    GeneralGood and Bad PointsmemberRicardo Casquete0:59 8 May '07  
    QuestionProblems hacking...membereduardo_g1:58 7 May '07  
    AnswerRe: Problems hacking...memberMark Pryce-Maher23:18 7 May '07  
    GeneralRe: Problems hacking...memberMark Pryce-Maher0:05 8 May '07  
    GeneralRe: Problems hacking...memberdilipakumara2:37 9 May '07  
    Generaltesting demomemberlenkry7:21 25 Oct '06  
    GeneralRe: testing demomemberlenkry7:37 26 Oct '06  
    GeneralLink errormembertheDiver4:37 13 Sep '06  
    GeneralRe: Link errormemberMark Pryce-Maher7:30 14 Sep '06  
    AnswerRe: Link errormemberMark Pryce-Maher5:49 15 Sep '06  
    GeneralRe: Link errormembertheDiver9:23 15 Sep '06  
    AnswerRe: Link error - drag drop tablesmemberMark Pryce-Maher6:58 15 Sep '06  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 7 May 2007
    Editor:
    Copyright 2006 by Mark Pryce-Maher
    Everything else Copyright © CodeProject, 1999-2008
    Web11 | Advertise on the Code Project