Possible to view T-SQL syntax of a stored proc-based SqlCommand?

Posted by mconnley on Stack Overflow See other posts from Stack Overflow or by mconnley
Published on 2010-03-18T20:40:10Z Indexed on 2010/03/18 20:41 UTC
Read the original article Hit count: 345

Filed under:
|
|
|
|

Hello! I was wondering if anybody knows of a way to retrieve the actual T-SQL that is to be executed by a SqlCommand object (with a CommandType of StoredProcedure) before it executes...

My scenario involves optionally saving DB operations to a file or MSMQ before the command is actually executed. My assumption is that if you create a SqlCommand like the following:

Using oCommand As New SqlCommand("sp_Foo")
    oCommand.CommandType = CommandType.StoredProcedure
    oCommand.Parameters.Add(New SqlParameter("@Param1", "value1"))
    oCommand.ExecuteNonQuery()
End Using

It winds up executing some T-SQL like:

EXEC sp_Foo @Param1 = 'value1'

Is that assumption correct? If so, is it possible to retrieve that actual T-SQL somehow? My goal here is to get the parsing, etc. benefits of using the SqlCommand class since I'm going to be using it anyway. Is this possible? Am I going about this the wrong way? Thanks in advance for any input!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net