時(shí)間:2015-06-28 00:00:00 來(lái)源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(0)
public partial class DataClasses1DataContext
??? {
? public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
? {
????? //記錄日志(每天一個(gè)文件,記錄所有更改sql,日志會(huì)存在第一個(gè)盤的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ā)生時(shí)間:{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語(yǔ)句全部記錄到數(shù)據(jù)庫(kù)的代碼如下:
Codepublic partial class DataClasses1DataContext
??? {
? StringBuilder sb = new StringBuilder();
????? using (StringWriter sw = new StringWriter(sb))
????? {
??? sw.WriteLine("發(fā)生時(shí)間:{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)
??? {
? //記錄日志(每天一個(gè)文件,記錄所有更改sql,日志會(huì)存在第一個(gè)盤的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語(yǔ)句
相關(guān)閱讀
熱門文章 淺談JSP JDBC來(lái)連接SQL Server 2005的方法 SqlServer2005對(duì)現(xiàn)有數(shù)據(jù)進(jìn)行分區(qū)具體步驟 sql server系統(tǒng)表?yè)p壞的解決方法 MS-SQL2005服務(wù)器登錄名、角色、數(shù)據(jù)庫(kù)用戶、角色、架構(gòu)的關(guān)系
人氣排行 配置和注冊(cè)O(shè)DBC數(shù)據(jù)源-odbc數(shù)據(jù)源配置教程 如何遠(yuǎn)程備份(還原)SQL2000數(shù)據(jù)庫(kù) SQL2000數(shù)據(jù)庫(kù)遠(yuǎn)程導(dǎo)入(導(dǎo)出)數(shù)據(jù) SQL2000和SQL2005數(shù)據(jù)庫(kù)服務(wù)端口查看或修改 修改Sql Server唯一約束教程 SQL Server 2005降級(jí)到2000的正確操作步驟 sql server系統(tǒng)表?yè)p壞的解決方法 淺談JSP JDBC來(lái)連接SQL Server 2005的方法