views:

521

answers:

4

I have a SqlCommand object, and I execute its ExecuteNonQuery method. The query fails, but I can't see how to obtain the query to debug it. Could you help me?

Update I am currently using Wireshark (tcpdump) to read the command, but I think that its not the best solution

Update The CommandText property only provides the Stored Procedure name. I want the whole command, with the parameters.

+3  A: 

You can use SQL Profiler to see what is the query/call to stored procedure being sent along with parameterrs.

shahkalpesh
and also connection settings, such as NOCOUNT, ARITHABORT, etc.
Russ Cam
A: 

You can get the value of the CommandText property of your SqlCommand ? If an exception is thrown, you can catch it, and access the CommandText property which will give you the SQL command like you've set it. Offcourse, if it contains parameters, the parameters won't be replaced by the appropriate values.

Frederik Gheysels
The CommandText property only provides the Stored Procedure name. I want the whole command, with the parameters.
Jader Dias
A: 

If you also want to see the parameters then you have to look at the Parameters property of the command object (not just the CommandText), and look at each parameter in this collection to extract the value and datatype, if that's what you're interested in.

Now, it sounds like what you're trying to debug is the stored procedure itself, not the C# code, is that right? If that's the case you need to attach your visual studio debugger to the SQL server to debug the T-SQL code. I know you can do this with Oracle, so I'm assuming you should be able to do this right out of the box with SQL server.

Ricardo Villamil
A: 

You should probably be using a try/catch block around your SQL anyway but the upside of doing this is you can easily return your SQL command string along with the error.

Something like this should do it:

try
{
    ***Execute your SQL query here***
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
    Console.WriteLine(***My SQL command string***);

}