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("日志內容為:");
??? 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("日志內容為:");
??? 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))
?
關鍵詞標簽:SQL語句
相關閱讀
熱門文章 淺談JSP JDBC來連接SQL Server 2005的方法 SqlServer2005對現(xiàn)有數(shù)據(jù)進行分區(qū)具體步驟 sql server系統(tǒng)表損壞的解決方法 MS-SQL2005服務器登錄名、角色、數(shù)據(jù)庫用戶、角色、架構的關系
人氣排行 配置和注冊ODBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程 如何遠程備份(還原)SQL2000數(shù)據(jù)庫 SQL2000數(shù)據(jù)庫遠程導入(導出)數(shù)據(jù) SQL2000和SQL2005數(shù)據(jù)庫服務端口查看或修改 修改Sql Server唯一約束教程 SQL Server 2005降級到2000的正確操作步驟 sql server系統(tǒng)表損壞的解決方法 淺談JSP JDBC來連接SQL Server 2005的方法