2010-04-20 26 views
0

有沒有辦法從ADO.NET調用中獲取發送到SQL Server的原始文本(如SQL Profiler中所示)?原始SQL從存儲過程調用從.NET發送到SQL Server

using(SqlConnection conn = new SqlConnection(connString)) { 
    SqlCommand cmd = conn.CreateCommand(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "GetSomeData"; 
    cmd.Parameters.Add("@id").Value = someId; 
    cmd.Parameters.Add("@someOtherParam").Value = "hello"; 

    conn.Open(); 
    SqlDataReader dr = cmd.ExecuteReader(); 

    // this sends up the call: exec GetSomeData @id=24, @someOtherParam='hello' 
    // how can I capture that and write it to debug? 

    Debug.Write("exec GetSomeData @id=24, @someOtherParam='hello'"); 

} 

回答

2

我不認爲有一種方法可以從單個屬性或方法中獲得想要的結果。我們曾經編寫一個簡單的靜態函數來轉儲文本,然後遍歷參數集合以轉儲出參數值。現在,您甚至可以將其設置爲擴展方法,使其看起來像SqlCommand對象的方法。

1

不在您的C#程序中 - 您必須連接SQL Profiler並監視發送到SQL Server的內容。

0

您可能會爲此創建另一個存儲過程。您的存儲過程會將另一個存儲過程的名稱作爲參數,然後在syscomments表中查找其定義。

或者您可以直接從您的ASP.NET應用程序查詢syscomments

select text from syscomments where id = OBJECT_ID('NameOfSproc')

相關問題