ITKeyword,专注技术干货聚合推荐

注册 | 登录

扩展Enterprise Libary 中MySql Block 

ycl111 分享于 2005-10-15

推荐:利用Oracle Enterprise Manager的扩展监控功能开发MySQL Plugin用于MySQL数据库监控

 4月2日,中国首届数据库技术大会在北京盛大召开。在这一面向数据库及商业智能技术专业人群的技术盛会上,各路DBA高手云集,悉数分享各自在数据库技术领域深入研

2019阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html

MySqlDatabase

using System;
using System.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
using System.Xml;
using Microsoft.Practices.EnterpriseLibrary.Common;

namespace Microsoft.Practices.EnterpriseLibrary.Data.MySql
{
 /// <summary>
 /// MySqlDatabase 的摘要说明。
 /// </summary>
 public class MySqlDatabase : Database
 {
  public MySqlDatabase():base()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public override DBCommandWrapper GetSqlStringCommandWrapper(string query)
  {
   ArgumentValidation.CheckForNullReference(query, "query");
   ArgumentValidation.CheckForEmptyString(query, "query");
   return new MySqlCommandWrapper(query);
  }
  public override DataSet ExecuteDataSet(DBCommandWrapper command)
  {
   return base.ExecuteDataSet (command);
  }
  public override void ExecuteNonQuery(DBCommandWrapper command)
  {
   base.ExecuteNonQuery (command);
  }
  public override IDataReader ExecuteReader(DBCommandWrapper command)
  {
   return base.ExecuteReader (command);
  }
  public override object ExecuteScalar(DBCommandWrapper command)
  {
   return base.ExecuteScalar (command);

  }
  public override void LoadDataSet(DBCommandWrapper command, DataSet dataSet, string tableName)
  {
   base.LoadDataSet (command, dataSet, tableName);
  }
  public override int UpdateDataSet(DataSet dataSet, string tableName, DBCommandWrapper insertCommand, DBCommandWrapper updateCommand, DBCommandWrapper deleteCommand, UpdateBehavior updateBehavior)
  {
   return base.UpdateDataSet (dataSet, tableName, insertCommand, updateCommand, deleteCommand, updateBehavior);
  }
  public override int UpdateDataSet(DataSet dataSet, string tableName, DBCommandWrapper insertCommand, DBCommandWrapper updateCommand, DBCommandWrapper deleteCommand, IDbTransaction transaction)
  {
   return base.UpdateDataSet (dataSet, tableName, insertCommand, updateCommand, deleteCommand, transaction);
  }
 
  public override IDbConnection GetConnection()
  {  
   return new MySqlConnection(ConnectionString);
  }
  public override DBCommandWrapper GetStoredProcCommandWrapper(string storedProcedureName)
  {
   ArgumentValidation.CheckForNullReference(storedProcedureName, "storedProcedureName");
   ArgumentValidation.CheckForEmptyString(storedProcedureName, "storedProcedureName");

   return new MySqlCommandWrapper(storedProcedureName, CommandType.StoredProcedure, ParameterToken);
  }
  public override DBCommandWrapper GetStoredProcCommandWrapper(string storedProcedureName, params object[] parameterValues)
  {
   return null;
  }
  protected override DbDataAdapter GetDataAdapter(UpdateBehavior behavior, IDbConnection connection)
  {
   string queryStringToBeFilledInLater = String.Empty;
   MySqlDataAdapter adapter =new MySqlDataAdapter(queryStringToBeFilledInLater, (MySqlConnection)connection);
   //   if (updateBehavior == UpdateBehavior.Continue)
   //   {
   //    adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnSqlRowUpdated);
   //   }
   return adapter;
  }

  public override char ParameterToken
  {
   get {return '?';}
  }


 }
}

MySqlCommandWrapper

using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Microsoft.Practices.EnterpriseLibrary.Data.MySql
{
 /// <summary>
 /// MySqlCommandWrapper 的摘要说明。
 /// </summary>
 public class MySqlCommandWrapper:DBCommandWrapper
 {
  private MySqlCommand command;
  private int rowsAffected;
  private object[] parameterValues;
  private bool needsParameters = false;
  private char parameterToken = '?';

  internal MySqlCommandWrapper()
  {
   //
   // TODO: Add constructor logic here
   //
   this.command=new MySqlCommand();
   this.command.CommandType=CommandType.Text;

  }
  internal MySqlCommandWrapper(string commandText)
  {
   //
   // TODO: Add constructor logic here
   //
  
   this.command = CreateCommand(commandText, CommandType.Text);

  }
  internal MySqlCommandWrapper(string commandText, CommandType commandType, char parameterToken)
  {
   this.parameterToken = parameterToken;
   this.command = CreateCommand(commandText, commandType);
  }
  internal MySqlCommandWrapper(string commandText, CommandType commandType, char parameterToken, object[] parameterValues) : this(commandText, commandType, parameterToken)
  {
   //this.command = CreateCommand(commandText, commandType);
   this.parameterValues = parameterValues;
   if (commandType == CommandType.StoredProcedure)
   {
    this.needsParameters = true;
   }
  }

 

  public override IDbCommand Command
  {
   get
   {
    return this.command ;
   }
  }
  public override int RowsAffected
  {
   get { return this.rowsAffected; }
   set { this.rowsAffected = value; }
  }
  public override int CommandTimeout
  {
   get { return this.command.CommandTimeout; }
   set { this.command.CommandTimeout = value; }
  }
  public override void AddParameter(string name, DbType dbType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  {
   this.command.Parameters.Add(CreateParameter(name, dbType, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value));
  }
  public override void AddOutParameter(string name, DbType dbType, int size)
  {
   AddParameter(name, dbType, size, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, DBNull.Value);
  }
  public override void AddInParameter(string name, DbType dbType, object value)
  {
   AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, value);
  }
  public override void AddInParameter(string name, DbType dbType)
  {
   AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, null);
  }
  public override void AddInParameter(string name, DbType dbType, string sourceColumn, DataRowVersion sourceVersion)
  {
   AddParameter(name, dbType, 0, ParameterDirection.Input, true, 0, 0, sourceColumn, sourceVersion, null);
  }
  public override void AddParameter(string name, DbType dbType, ParameterDirection direction, string sourceColumn, DataRowVersion sourceVersion, object value)
  {
   MySqlParameter param = CreateParameter(name, dbType, 0, direction, false, 0, 0, sourceColumn, sourceVersion, value);
   this.command.Parameters.Add(param);
  }
//  public void AddParameter(string name, MySqlDbType mySqlDbType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
//  {
//   MySqlParameter param = CreateParameter(name, DbType.String, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value);
//   param.MySqlDbType = mySqlDbType;
//   this.command.Parameters.Add(param);
//  }
  public override object GetParameterValue(string name)
  {
   return this.command.Parameters[BuildParameterName(name)].Value;
  }

 
  public override void SetParameterValue(string name, object value)
  {
   this.command.Parameters[BuildParameterName(name)].Value = (value == null) ? DBNull.Value : value;
  }
  protected override void DoAssignParameterValues()
  {
   if (SameNumberOfParametersAndValues() == false)
   {
    throw new InvalidOperationException(SR.ExceptionMessageParameterMatchFailure);
   }

   int returnParameter = 1;
   for (int i = 0; i < this.parameterValues.Length; i++)
   {
    IDataParameter parameter = this.command.Parameters[i + returnParameter];

    // There used to be code here that checked to see if the parameter was input or input/output
    // before assigning the value to it. We took it out because of an operational bug with
    // deriving parameters for a stored procedure. It turns out that output parameters are set
    // to input/output after discovery, so any direction checking was unneeded. Should it ever
    // be needed, it should go here, and check that a parameter is input or input/output before
    // assigning a value to it.
    SetParameterValue(parameter.ParameterName, this.parameterValues[i]);
   }
  }


  protected override bool DoIsFurtherPreparationNeeded()
  {
   return this.needsParameters;
  }
  protected override void DoDiscoverParameters(char parameterToken)
  {
   this.parameterToken = parameterToken;
   using (MySqlCommand newCommand = CreateNewCommandAndConnectionForDiscovery())
   {
   
    MySqlCommandBuilder.DeriveParameters(newCommand);

    foreach (IDataParameter parameter in newCommand.Parameters)
    {
     IDataParameter cloneParameter = (IDataParameter)((ICloneable)parameter).Clone();
     cloneParameter.ParameterName = BuildParameterName(cloneParameter.ParameterName);
     this.command.Parameters.Add(cloneParameter);
    }
    newCommand.Connection.Close();
   }
  }
 
  public override void Dispose()
  {
   this.command.Dispose();
  }
  private static MySqlCommand CreateCommand(string commandText, CommandType commandType)
  {
   MySqlCommand newCommand = new MySqlCommand();
   newCommand.CommandText = commandText;
   newCommand.CommandType = commandType;

   return newCommand;
  }
  private MySqlCommand CreateNewCommandAndConnectionForDiscovery()
  {
   MySqlConnection clonedConnection = (MySqlConnection)((ICloneable)this.command.Connection).Clone();
   clonedConnection.Open();
   MySqlCommand newCommand = CreateCommand(this.command.CommandText, this.command.CommandType);
   newCommand.Connection = clonedConnection;

   return newCommand;
  }
  private string BuildParameterName(string name)
  {
   //System.Diagnostics.Debug.Assert(parameterToken != 0x0000);
   if (name[0] != this.parameterToken)
   {
    return name.Insert(0, new string(this.parameterToken, 1));
   }
   return name;
  }
  private MySqlParameter CreateParameter(string name, DbType type, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  {
   MySqlParameter param = this.command.CreateParameter();
   param.ParameterName = BuildParameterName(name);

   if ((type.Equals(DbType.Object)) && (value is byte[]))
   {
    //myby there i an error
    param.MySqlDbType = MySqlDbType.Blob;
   
   }
   else
   {
    param.DbType = type;
   }

   param.Size = size;
   param.Direction = direction;
   param.IsNullable = nullable;
   param.Precision = precision;
   param.Scale = scale;
   param.SourceColumn = sourceColumn;
   param.SourceVersion = sourceVersion;
   param.Value = (value == null) ? DBNull.Value : value;

   return param;
  }
  private bool SameNumberOfParametersAndValues()
  {
   int returnParameterCount = 1;
   int numberOfParametersToStoredProcedure = this.command.Parameters.Count - returnParameterCount;
   int numberOfValuesProvidedForStoredProcedure = this.parameterValues.Length;
   return numberOfParametersToStoredProcedure == numberOfValuesProvidedForStoredProcedure;
  }
 }
}

另外需要添加MySql.Data.dll引用

MySqlDatabase using System; using System.Data; using System.Data.Common; using MySql.Data.MySqlClient; using System.Xml; using Microsoft.Practices.EnterpriseLibrary.Common; namespa

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。