If I try and call a stored procedure and there is a database error, will that raise as an exception in my C# code? Or do I need to check the result of the stored procedure and raise an exception myself?
Eg:
using (SqlCommand cmd = new SqlCommand("prc_InsertSomething", conn))
{
if (cmd.ExecuteNonQuery() != 1) // should I be doing this bit or not?
{
throw new DataException("Could not insert something");
}
}
Thanks
For
ExecuteNonQuery, you will generally be OK as long as the severity is high enough; however, be careful if you are reading data; for example, if you have a stored procedure that returns multiple grids and throws an exception (for example) just before the penultimate result-set, then if you don’t iterate over the data you may never see the error.This is because the error is injected into the TDS stream (not out-of-band). You need to consume the TDS at least as far as the error for your code to become aware of it.
Something as simple as (in the case of
IDataReader):after your consuming code will ensure you consume the entire inbound TDS stream.