您的位置:首页 > 数据库

MS DAAB 记录所有sql 语句

2007-08-20 15:26 435 查看
看到ms datasource access application block 是微软开源的,封装好很多数据库操作的方法,也支持多种数据库类型,而且调用也比较方便,所以在项目中采用了它.

因为项目的sql语句是各个开发人员根据文档实现什么样功能去写sql,sql出错或效率不好时不方便调试,所以就有必要把每次数据库操作的sql语句记下来.也想到有时候把这个功能打开,有时关闭.这样就有直接修改DAAB源码了.具体做法如下:

1.下载并安装MS Enterprise Library .

2.开始修改源码,安装路径下找到并打开Data.sln,根目录下有个database.cs,

再打开并搜索PrepareCommand,可以找两个重载方法,后一个方法有调用前一个方法.

3.根据前面说的项目要求,开始动工.直接贴代码出来,可能更好理解


protected static void PrepareCommand(DbCommand command, DbConnection connection)




...{


if (command == null) throw new ArgumentNullException("command");


if (connection == null) throw new ArgumentNullException("connection");




command.Connection = connection;




//----------修改开始---------------//




if (string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["CommandTimeout"].ToString()))




...{


command.CommandTimeout = int.Parse(System.Configuration.ConfigurationManager.AppSettings["CommandTimeout"].ToString());


}


else




...{


command.CommandTimeout = connection.ConnectionTimeout;


}




string IsLogSQL = System.Configuration.ConfigurationManager.AppSettings["SQLLog"].ToString();


if (IsLogSQL.ToUpper() == "YES")




...{


System.Text.StringBuilder sb = new System.Text.StringBuilder();


sb.Append(" --DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


sb.Append(" --CommandType:" + command.CommandType.ToString());


sb.Append(" --CommandText:" + command.CommandText);


sb.Append(" --CommandParameters:");


foreach (DbParameter param in command.Parameters)




...{


sb.Append(" Name:"+param.ParameterName);


sb.Append(" DbType:" + param.DbType);


sb.Append(" Size:" + param.Size);


sb.Append(" Value:" + param.Value);


}


System.Diagnostics.Trace.WriteLine(sb.ToString());


}


//----------------结束-------------------------------//


}

3.重新编译Data.sln,生成一个新的Microsoft.Practices.EnterpriseLibrary.Data.dll

4.应用

A.web.config

<!--用于实现记录sql这个功能打开与关闭-->

<appSettings><add key="SQLLog" value="YES" /></appSettings>

<!--数据库操作执行超时时间,默认是60秒

加上这个的原因是,有时执行select查询时,记录量有几百万以上,60秒可能还会超时(没有合适的索引或其他),时间变长,让用户更好理解"超时"

-->

<appSettings><add key="CommandTimeout" value="300" /></appSettings>

B.global.asax

void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码

string path = HttpContext.Current.Request.ApplicationPath;
path = Server.MapPath(path);
path += "//Log";
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
path += "//Log_"+DateTime.Now.ToString("yyyy-MM")+".log";
System.IO.File.Create(path);
}
else
{
path += "//Log_" + DateTime.Now.ToString("yyyy-MM") + ".log";
if (!System.IO.File.Exists(path))
{
System.IO.File.Create(path);
}
}

System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.AutoFlush = true;
System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(path));
}

到这里,我在程序中每有一个数据库执行的操作都会记录一个文本文档去了.搞掂!

至于怎么使用DAAB到web程序中,请再搜索相关资料,这里就不说了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: