時間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)
public partial class DataClasses1DataContext
{
public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{
//記錄日志(每天一個文件,記錄所有更改sql,日志會存在第一個盤的log文件夾下)
string directory = Path.Combine(Directory.GetLogicalDrives().First(), "log");
Directory.CreateDirectory(directory);
string logFile = Path.Combine(directory,
"log" + DateTime.Now.ToLongDateString() + ".txt");
using (StreamWriter w = File.AppendText(logFile))
{
w.WriteLine("發(fā)生時間:{0}", DateTime.Now.ToString());
w.WriteLine("日志內(nèi)容為:");
this.Log = w;
try
{
base.SubmitChanges(failureMode);
}
catch (Exception e)
{
w.WriteLine("異常:" + e.Message + e.StackTrace);
w.WriteLine("--------------------------------------------------------------");
throw;
}
finally
{
this.Log = null;
}
w.WriteLine("--------------------------------------------------------------");
}
}
}
如果想把sql語句全部記錄到數(shù)據(jù)庫的代碼如下:
Codepublic partial class DataClasses1DataContext
{
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
sw.WriteLine("發(fā)生時間:{0}", DateTime.Now.ToString());
sw.WriteLine("日志內(nèi)容為:");
this.Log = sw;
try
{
base.SubmitChanges(failureMode);
string sqlStr = "insert into logTable(Content)values( '"+sb.ToString()+"')";
//SqlConnection con=
using (SqlConnection con=new SqlConnection(this.Connection.ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlStr, con);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
//記錄日志(每天一個文件,記錄所有更改sql,日志會存在第一個盤的log文件夾下)
string directory = Path.Combine(Directory.GetLogicalDrives().First(), "log");
Directory.CreateDirectory(directory);
string logFile = Path.Combine(directory,
"log" + DateTime.Now.ToLongDateString() + ".txt");
using (StreamWriter w = File.AppendText(logFile))
關(guān)鍵詞標(biāo)簽:SQL語句
相關(guān)閱讀
熱門文章 SqlServer2005對現(xiàn)有數(shù)據(jù)進行分區(qū)具體步驟sql server系統(tǒng)表損壞的解決方法MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫用戶Access、SQL Server、Oracle常見應(yīng)用的區(qū)別
人氣排行 如何遠程備份(還原)SQL2000數(shù)據(jù)庫SQL2000數(shù)據(jù)庫遠程導(dǎo)入(導(dǎo)出)數(shù)據(jù)配置和注冊O(shè)DBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程SQL2000和SQL2005數(shù)據(jù)庫服務(wù)端口查看或修改SQL Server 2005降級到2000的正確操作步驟修改Sql Server唯一約束教程淺談JSP JDBC來連接SQL Server 2005的方法SQL Server創(chuàng)建表語句介紹