我有幾個函數需要一個SQL查詢(已經添加了參數)並在數據庫上運行它。當SQL查詢失敗時,我想記錄完整的查詢,包括參數,以便準確查看導致失敗的原因。 query.ToString()返回IBM.Data.Informix.IfxCommand,所以目前我只是捕獲query.CommandText,但如果參數是什麼引起的問題,這並不能告訴我我正在處理的是什麼。記錄帶參數的參數化SQL查詢
這裏的查詢功能,我使用一個:
public DataTable CallDtQuery(IfxCommand query)
{
DataTable dt = new DataTable();
using (IBM.Data.Informix.IfxConnection conn = new IfxConnection(sqlConnection))
{
try
{
IBM.Data.Informix.IfxDataAdapter adapter = new IfxDataAdapter();
query.Connection = conn;
conn.Open();
adapter.SelectCommand = new IfxCommand("SET ISOLATION TO DIRTY READ", conn);
adapter.SelectCommand.ExecuteNonQuery(); //Tells the program to wait in case of a lock.
adapter.SelectCommand = query;
adapter.Fill(dt);
conn.Close();
adapter.Dispose();
}
catch (IBM.Data.Informix.IfxException ex)
{
LogError(ex, query.CommandText);
SendErrorEmail(ex, query.CommandText);
DisplayError();
}
}
return dt;
}
這裏的記錄功能:
private void LogError(IfxException ex, string query)
{ //Logs the error.
string filename = HttpContext.Current.Server.MapPath("~") + "/Logs/sqlErrors.txt";
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.WriteLine("=======BEGIN ERROR LOG=======");
sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString());
sw.WriteLine("Query = " + query);
sw.WriteLine("User = " + HttpContext.Current.Session["UserID"]);
sw.WriteLine("Error Message = " + ex.Message);
sw.WriteLine("Message Source:");
sw.WriteLine(ex.Source);
sw.WriteLine("=============================");
sw.WriteLine("Message Target:");
sw.WriteLine(ex.TargetSite);
sw.WriteLine("=============================");
sw.WriteLine("Stack Trace:");
sw.WriteLine(ex.StackTrace);
sw.WriteLine("========END ERROR LOG========");
sw.WriteLine("");
sw.Close();
fs.Close();
}
有沒有辦法通過整個字符串,參數包括,用於記錄作爲我在這裏?我想到的唯一方法應該是將查詢傳遞給日誌記錄功能,並構建一個for循環來將每個參數記錄爲一個單獨的項目。由於其中一些查詢有許多參數,並且我不會用一個簡單的字符串獲得完整的查詢,所以這不是最理想的解決方案。
雖然我工作,但我改變了一下。我將這些更改添加爲答案,以便您可以看到我所做的。謝謝! – Ben
不錯,我沒有測試過這個代碼=) –
我遇到了一個場景,我必須記錄所有的查詢,並使用了它的值,並找到了答案。好一個!!正是我想要的:http://i.imgur.com/Xqgl3et.png感謝魯本斯:) – Latheesan