When I run the following snippet
try
{
using (SqlConnection conn = new SqlConnection("I'm shy"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "PRINT 'A';PRINT 'B';PRINT 'C';RAISERROR('SQL_Error', 18, 1)";
cmd.ExecuteNonQuery();
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
I get the following message:
SQL_Error
A
B
C
and ex.Errorshas 4 entries (3 SqlErrorcorresponding to fingerprints, have SqlError.Class0 (versus 18 for a real error)
However, if I replaced ExecuteNonQuerywith ExecuteScalar, I get the expected result:
Message SQL_Error, and I have only one entry in ex.Errors...
Is there any way to avoid weird behavior cmd.ExecuteNonQuery??
source
share