微软企业库5.0 支持 MySql

三步让 企业库支持 mysql 数据库

1.创建 MySqlDatabaseData 类

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation;
using System.Configuration;

namespace Grass.MySqlDal.Data
{
  /// <summary>
  /// Describes a <see cref="SqlDatabase"/> instance, aggregating information from a 
  /// <see cref="ConnectionStringSettings"/>.
  /// </summary>
  public class MySqlDatabaseData : DatabaseData
  {
    #region Public Methods
    public MySqlDatabaseData(ConnectionStringSettings connectionStringSettings, IConfigurationSource configurationSource)
      : base(connectionStringSettings, configurationSource)
    {
    }
    #endregion

    /// <summary>
    /// Creates a <see cref="TypeRegistration"/> instance describing the <see cref="SqlDatabase"/> represented by 
    /// this configuration object.
    /// </summary>
    /// <returns>A <see cref="TypeRegistration"/> instance describing a database.</returns>
    public override System.Collections.Generic.IEnumerable<Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration> GetRegistrations()
    {
      yield return new TypeRegistration<Database>(
          () => new MySqlDatabase(
              ConnectionString,
              Container.Resolved<IDataInstrumentationProvider>(Name)))
              {
                Name = Name,
                Lifetime = TypeRegistrationLifetime.Transient
              };
    }
  }
}

2.创建 MySqlDatabase 类 

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation;
using MySql.Data.MySqlClient;

namespace Grass.MySqlDal.Data
{
  /// <summary>
  /// MySql 数据库訪问基础
  /// </summary>
  [ConfigurationElementType(typeof(MySqlDatabaseData))]
  public class MySqlDatabase : Database
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="SqlDatabase"/> class with a connection string.
    /// </summary>
    /// <param name="connectionString">The connection string.</param>
    public MySqlDatabase(string connectionString)
      : base(connectionString, MySqlClientFactory.Instance)
    {
    }
    /// <summary>
    /// Initializes a new instance of the <see cref="SqlDatabase"/> class with a
    /// connection string and instrumentation provider.
    /// </summary>
    /// <param name="connectionString">The connection string.</param>
    /// <param name="instrumentationProvider">The instrumentation provider.</param>
    public MySqlDatabase(string connectionString, IDataInstrumentationProvider instrumentationProvider)
      : base(connectionString, MySqlClientFactory.Instance, instrumentationProvider)
    {
    }

    /// <summary>
    /// Retrieves parameter information from the stored procedure specified in the <see cref="DbCommand"/> and populates the Parameters collection of the specified <see cref="DbCommand"/> object. 
    /// </summary>
    /// <param name="discoveryCommand">The <see cref="DbCommand"/> to do the discovery.</param>
    /// <remarks>The <see cref="DbCommand"/> must be a <see cref="SqlCommand"/> instance.</remarks>
    protected override void DeriveParameters(System.Data.Common.DbCommand discoveryCommand)
    {
      MySqlCommandBuilder.DeriveParameters((MySqlCommand)discoveryCommand);
    }
  }
}



3.加入 config 支持

<?xml version="1.0"?>
<configuration>
  <!--MySql 企业库支持-begin-->
  <configSections>
    <!--01.声明一个数据库配置节-->
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,Microsoft.Practices.EnterpriseLibrary.Data" />
  </configSections>
  <dataConfiguration >
    <!--02.注冊MySql数据提供者-->
    <providerMappings>
      <add name="MySql.Data.MySqlClient"
      databaseType="Grass.MySqlDal.Data.MySqlDatabase, Grass.MySqlDal" />
    </providerMappings>
  </dataConfiguration>
  <system.data>
    <!--03.注冊MySql数据提供者工厂类-->
    <DbProviderFactories>
      <add
          name="MySql Data Provider Factory"
          invariant="MySql.Data.MySqlClient"
          description="MySql Data Provider"
          type="MySql.Data.MySqlClient.MySqlClientFactory" />
    </DbProviderFactories>
  </system.data>
  <!--MySql 企业库支持-end-->
  <connectionStrings>
    <!--04.数据库连接字符串-->
    <add name="test_MySql"
         connectionString="server=127.0.0.1;database=test;User Id=root;Password=123456;Persist Security Info=True;"
         providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
</configuration>


ok 了,试下吧~~

posted on 2017-08-01 16:26  ljbguanli  阅读(435)  评论(0编辑  收藏  举报