微软企业库5.0 支持 MySql

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xxj_jing/article/details/28611215

三步让 企业库支持 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异常处理模块问题

07-20

在用配置工具的时候 配置 Exception Handling+Logging模块rnrnlogging settings 中的 loggintg target listeners 选项中,配置CustomTraceListener 里面的 add from file 时候,我选中了自己写的类,为什么 选中后没有反映,列表里并没有出来!rn[img=http://img.my.csdn.net/uploads/201207/20/1342754129_4800.png][/img]rnrn这是我类的代码rnrn[code=C#]rnusing System;rnusing System.Data;rnusing System.Diagnostics;rnusing System.Text;rnusing Microsoft.Practices.EnterpriseLibrary.Common.Configuration;rnusing Microsoft.Practices.EnterpriseLibrary.Data;rnusing Microsoft.Practices.EnterpriseLibrary.Logging;rnusing Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;rnusing Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;rnusing System.Data.Common;rnrnrnrnnamespace Helperrnrn [ConfigurationElementType(typeof(CustomTraceListenerData))]rn public class ExceptionCustomerListener : CustomTraceListenerrn rn /// rn /// 数据库连接rn /// rn private Database db;rnrn public ExceptionCustomerListener ( )rn rn db = DBHelper.CreateDataBase ( );rn rnrnrn #region Overrides of TraceListenerrn public override void TraceData ( TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data )rn rn if (this.Filter == null || this.Filter.ShouldTrace ( eventCache, source, eventType, id, null, null, data, null ))rn rn if (data is LogEntry)rn rn ExecuteWriteLogSQL ( data as LogEntry );rnrn rn else if (data is String)rn rn Write ( data );rn rn elsern rn base.TraceData ( eventCache, source, eventType, id, data );rn rn rn rnrn public override void Write ( string message )rn rn String messing = Utils.GetBetweenString ( message, "Message :", "Source :", 9 );rn string exceptionInfo = Utils.GetBetweenString ( message, "Stack Trace :", "Additional Info:", 13 );rn StringBuilder sb = new StringBuilder ( );rn sb.Append ( "insert into ExceptionLog values (" );rn sb.Append ( "(SELECT max(id)+1 FROM ExceptionLog)," );rn sb.Append ( "@Message" );rn sb.Append ( "@LogDate" );rn sb.Append ( "@ExceptionLevel" );rn sb.Append ( "@Exception" );rn DbCommand com = db.GetSqlStringCommand ( sb.ToString ( ) );rn //添加参数rn db.AddInParameter ( com, "@Message", DbType.String, messing );rn db.AddInParameter ( com, "@LogDate", DbType.String, DateTime.Now );rn db.AddInParameter ( com, "@ExceptionLevel", DbType.String, message );rn db.AddInParameter ( com, "@Exception", DbType.String, exceptionInfo );rn int x = db.ExecuteNonQuery ( com );rn rnrn public override void WriteLine ( string message )rn rn Write ( message );rn rnrnrnrnrn #endregionrn /// rn /// 日志写入数据库rn /// rn /// rn private void ExecuteWriteLogSQL ( LogEntry log )rn rn String messing = Utils.GetBetweenString ( log.Message, "Message :", "Source :", 9 );rn string exceptionInfo = Utils.GetBetweenString ( log.Message, "Stack Trace :", "Additional Info:", 13 );rn StringBuilder sb = new StringBuilder ( );rn sb.Append ( "insert into ExceptionLog values (" );rn sb.Append ( "(SELECT max(id)+1 FROM ExceptionLog)," );rn sb.Append ( "@Message" );rn sb.Append ( "@LogDate" );rn sb.Append ( "@ExceptionLevel" );rn sb.Append ( "@Exception" );rn DbCommand com = db.GetSqlStringCommand ( sb.ToString ( ) );rn //添加参数rn db.AddInParameter ( com, "@Message", DbType.String, messing );rn db.AddInParameter ( com, "@LogDate", DbType.String, log.TimeStamp );rn db.AddInParameter ( com, "@ExceptionLevel", DbType.String, log.LoggedSeverity );rn db.AddInParameter ( com, "@Exception", DbType.String, exceptionInfo );rn int x = db.ExecuteNonQuery ( com );rn rnrn rnrnrn[/code] 论坛

没有更多推荐了,返回首页