Can I get the SQL command from a SqlException object?

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
 
C

Cor Ligthert[MVP]

Not likely because now you do it dynamicaly.

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

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top