Extend exception to determine if exception is a SQL Reference constraint exception Method!

Source: Internet
Author: User
Tags sql error

When designing a data table, if you set some columns to relate to other table foreign keys, then if you add to it, modify the operation, its associated table if there is no matching record error, or in its associated table to delete, will also error, this is the role of foreign key constraints, of course, in addition to foreign keys there are many constraints, not discussed here, This article is mainly about how to determine whether the SQL Reference constraint exception, so that the SQL complex error can be better converted to the user can understand the friendly hints.

Extend exception to determine whether exception is a SQL Reference constraint exception method (Issqlreferenceconstraintexception):

 Public Static BOOLIssqlreferenceconstraintexception ( ThisException except) {            varBaseex =except.            GetBaseException (); if(Baseex isSqlException) {                stringMessage = (Baseex asSqlException).                Message; if(Message. ToLower (). Contains ("reference Constraint"))                {                    return true; }                Else                {                    return false; }            }            Else if(Baseex! =NULL&&!Object. ReferenceEquals (except, Baseex))//If the base class is not empty and does not equal the exception itself, proceed back to the survey to find {returnissqlreferenceconstraintexception (Baseex); }            return false; }

The native SQL error message is as follows:

The DELETE statement conflicted with the REFERENCE constraint "fk_dbo. Category_dbo. User_lasteditorid ". The conflict occurred in database "Mtem", table "dbo. Category ", column ' Lasteditorid '.
The statement has been terminated.

The extension method shows our predefined error message, which is called as follows:

 Public voidRemove (intUserID) {User User= This. Repository. Get (t=>t.id==UserID); if(User! =NULL)            {                Try                {                     This. Repository. Remove (User,true); }                Catch(Exception ex) {if (ex. Issqlreferenceconstraintexception ())//judged to be a referential constraint error, then directly throws our custom error {Throw NewException ("the account has other business records associated with it and is prohibited from deletion! If you confirm that the account is stopped, you can set it to disabled. "); }                }            }            Else            {                Throw NewException ("The account information does not exist or has been deleted! "); }        }

Extend exception to determine if exception is a SQL Reference constraint exception Method!

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.