2010-04-20 30 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')

相关问题