You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CaiQie/DB/DbCommandInterceptor.cs

84 lines
2.6 KiB
C#

3 months ago
using System;
using System.Data;
3 months ago
using System.Diagnostics;
using System.Text;
using Chloe.Infrastructure.Interception;
3 months ago
namespace DB
{
3 months ago
3 months ago
public class DbCommandInterceptor : IDbCommandInterceptor
3 months ago
{
3 months ago
public void ReaderExecuting(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
3 months ago
3 months ago
public void ReaderExecuted(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
3 months ago
3 months ago
public void NonQueryExecuting(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
3 months ago
3 months ago
public void NonQueryExecuted(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
3 months ago
3 months ago
public void ScalarExecuting(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
3 months ago
3 months ago
public void ScalarExecuted(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
ToDb(command, interceptionContext.Exception);
}
private void ToDb(IDbCommand command, Exception exception)
{
3 months ago
#if DEBUG
3 months ago
Trace.WriteLine(command.CommandText);
Trace.WriteLine(AppendDbCommandInfo(command));
3 months ago
#endif
3 months ago
}
3 months ago
3 months ago
private string AppendDbCommandInfo(IDbCommand command)
3 months ago
{
3 months ago
var sb = new StringBuilder();
3 months ago
3 months ago
foreach (IDbDataParameter param in command.Parameters)
3 months ago
{
3 months ago
if (param == null)
continue;
object value;
if (param.Value == null || param.Value == DBNull.Value)
{
value = "NULL";
}
else
{
value = param.Value;
if (param.DbType == DbType.String || param.DbType == DbType.AnsiString ||
param.DbType == DbType.DateTime)
value = "'" + value + "'";
}
sb.AppendFormat("{3} {0} {1} = {2};", Enum.GetName(typeof(DbType), param.DbType), param.ParameterName,
value, Enum.GetName(typeof(ParameterDirection), param.Direction));
sb.AppendLine();
3 months ago
}
3 months ago
return sb.ToString();
3 months ago
}
}
}