C # capture the custom exception (raiserror) thrown by SQL Server in the Catch Block)

Source: Internet
Author: User
I am basically illiterate in terms of data Literacy. Today, I saw someone else's Article I think it's okay. I am not very familiar with the principle, but I 'd like to explain it first.
The original article is as follows: (the replacement part is replaced by its own replacement)

In the stored procedure of SQL Server, according to the requirements of the business logic, sometimes a custom exception needs to be thrown #ProgramCapture and perform corresponding processing. SQL Server throws a custom exception and is simple, just like this: raiserror ('rais error1 ', 16, 1) with Nowait, but it is very particular about the error levels, otherwise, the Catch Block in C # may not be captured.

The method for SQL servr to throw a custom exception is generally written as follows: raiserror ('rais error1', 16, 1) with Nowait
The number 16 indicates the error level: the error level ranges from 0 to 25, 19 to 25 indicates the major error level.
Note: A value smaller than 0 is interpreted as 0, and a value greater than 25 is interpreted as 25.

Any user can specify the error level between 0 and 18.
Errors ranging from 19 to 25 can only be specified by members of the SysAdmin role using the with log option.
Errors of levels 19 to 25 are recorded in the error log and Application Log.
Errors of 20 to 25 levels are considered fatal. In case of a fatal level error, the client connection will terminate after receiving the message.

The Catch Block in C # can capture exceptions with error levels 11 to 19.
Errors of 0 to 10 levels are not caught at all. Exceptions of 20 to 25 levels are considered fatal and will be disconnected from the database. Therefore, the C # cath block can accept this exception, but the exception content is not a real exception content. It may be something like this: "A transmission-level error occurs when receiving results from the server ".

Public   Void Getsqlerror ()
{
Try
{
String Connstring =   " Data Source = ccm02 \ sqlexpress; initial catalog = northwind; user id = sa; Password = sh2_123 " ;
String SQL =   " Select * from products " ;
String Raiserror =   " Raiserror ('rais error1', 16, 1) with Nowait " ;
SQL = SQL +   " ; "   + Raiserror;
Sqlconnection con =   New Sqlconnection (connstring );
Sqlcommand cmd =   New Sqlcommand (SQL, con );
Con. open ();
Cmd. executenonquery ();
Con. Close ();
}
Catch (Exception ex)
{
Rtbvalue. Text = Ex. Message. tostring ();
}
}

Result:
Rtbvalue. Text: "Rais error1"

From: Http://www.cnblogs.com/anjou/archive/2007/11/17/962846.html
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.