Retrieve SQL statement on Exception

  • Thread starter Thread starter GS
  • Start date Start date
G

GS

I would like to output the exact command including parameters that was sent
to SQL when an exception occurs, is this possible?
 
It looks to me like the only way to do that is make sure that you have your SqlCommand in scope for your exception catching block. Then you have access to the SqlCommand's CommandText and Parameters collection.
 
Yes I can do that, but if I am updating an array of rows or an entire table
it is difficult to figure out which row failed, in the array or the table,
and I do not want to loop through each row in the table and update
individually, but if I have to do that I guess it would be OK.

Blake said:
It looks to me like the only way to do that is make sure that you have
your SqlCommand in scope for your exception catching block. Then you have
access to the SqlCommand's CommandText and Parameters collection.
 
If you're updating an array of rows, aren't you already at the level where you can catch exceptions for each row update

If you're using a DataSet, you should be able to use the DataTable's GetErrors() method to retrieve an array of DataRows that have errors. I guess at that point, though, you don't really have access to the SqlCommand's exact state at the time of the failure.
 
Ah not really. This info is not exposed, but you can use the Profiler to see
what was sent to the server.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top