提交数据时生成的SQL脚本便于实时跟踪
作者:C/S框架网  发布日期:2013-01-16 16:52:00
  提交数据时生成的SQL脚本便于实时跟踪



绑定SqlDataAdapter.RowUpdating事件:

SqlDataAdapter adp = new SqlDataAdapter();
adp.RowUpdating += new SqlRowUpdatingEventHandler(OnAdapterRowUpdating);

dalBase基类增加下面代码:

C# Code:


/// <summary>
/// 提交记录时生成的SQL脚本
/// </summary>
protected string _LastRowSQLText = "";

/// <summary>
/// 生成带参数的SQL脚本
/// </summary>
protected bool _GenerateLastRowSQLText = false;


/// <summary>
/// 适配器提交每条记录时触发的事件
/// </summary>
protected virtual void OnAdapterRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
   if (_GenerateLastRowSQLText)
   _LastRowSQLText = BuildSQLText(e.Command.Parameters);
}


/// <summary>
/// 生成带参数的SQL脚本
/// </summary>
/// <param name="parameters">SqlCommand参数</param>
/// <returns></returns>
private string BuildSQLText(SqlParameterCollection parameters)
{
   StringBuilder sb = new StringBuilder();
   foreach (SqlParameter p in parameters)
   {
      sb.AppendLine(p.ParameterName "=" (((p.Value == null) || (p.Value == DBNull.Value) || (p.Value.ToString()=="")) ?
      
      "null" : p.Value.ToString()) ", ");
   }
   return sb.ToString();
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇