Can I get the SQL command from a SqlException object?

  • Thread starter Thread starter Big Daddy
  • Start date Start date
B

Big Daddy

Let's say I have some code like this:

try
{
using (SqlConnection conn = new SqlConnection(strConnect))
using (SqlCommand cmd = new SqlCommand("", conn))
{
conn.Open();
cmd.CommandText = "select * from SomeTable";
cmd.ExecuteScalar();
}
}
catch (SqlException ex)
{
// Do logging
}

In my exception handler, when I'm trying to log the error, I'd like to
be able to get the command text that caused the exception to be thrown
(in the example above, it would be "select * from SomeTable"). The
SqlException class has a collection of SqlErrors, but they don't
appear to have the text I want. Is there a way for me to get this?

thanks in advance,
John
 
Not likely because now you do it dynamicaly.

As you use stored procedures the text is not even in your code.

Cor
 
Back
Top