码迷,mamicode.com
首页 > 数据库 > 详细

微软企业库5.0 支持 MySql

时间:2014-06-20 12:49:38      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:des   class   blog   code   com   get   

三步让 企业库支持 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 了,试下吧~~

微软企业库5.0 支持 MySql,布布扣,bubuko.com

微软企业库5.0 支持 MySql

标签:des   class   blog   code   com   get   

原文地址:http://blog.csdn.net/xxj_jing/article/details/28611215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!