First of all, I would like to thank @ busy chasing and forgetting the SQL text that can throw exceptions:
As follows:
RAISERROR (n'exception 1',) WITH NOWAIT
SELECT 1
RAISERROR (n'exception 2',) WITH NOWAIT
SELECT 2
RAISERROR (n'exception 3',) WITH NOWAIT
SELECT 3
The message for debugging in SSMS is as follows:
Message 50000, level 11, status 5, 3rd rows
Exception 1
(One row is affected)
Message 50000, level 11, status 5, 5th rows
Exception 2
(One row is affected)
Message 50000, level 11, status 5, 7th rows
Exception 3
(One row is affected)
Three exceptions are thrown ..
Then I captured it in this way, but I don't know how to behave properly.
Button + event:
/// <Summary>
/// Test the sequence or method of C # processing when multiple exceptions are thrown by SQL statements.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnMultiExceptions_Click (object sender, EventArgs e)
{
Try
{
StringBuilder _ sqlBuilder = new StringBuilder ();
_ SqlBuilder. AppendLine ("RAISERROR (n'exception 1', 11,5) with nowait ");
_ SqlBuilder. AppendLine ("SELECT 1 ");
_ SqlBuilder. AppendLine ("RAISERROR (n'exception 2', 11,5) with nowait ");
_ SqlBuilder. AppendLine ("SELECT 2 ");
_ SqlBuilder. AppendLine ("RAISERROR (n'exception 3', 11,5) with nowait ");
_ SqlBuilder. AppendLine ("SELECT 3 ");
* *****. MSSQL. SqlServerDataAccess. StaticExcuteNoQuery (_ sqlBuilder. ToString ());
}
Catch (Exception ex)
{
TxtShow. Text = ex. Message; // txtShow is a Text box. I will display the Message in it.
}
}
Now let's look at the results:
Multiple exceptions in MSSQL become one in C?
This is what cainiao guessed.
C # processes SQLException in units of one transaction. No matter how many SQL exceptions are thrown by this transaction, the C # side captures one.