Note: MSSQL throws multiple exceptions. How can I capture C #?

Source: Internet
Author: User

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.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.