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.

SQL error codes are stored in the Master.sys.messages table

After the breakpoint tracking exception, it is concluded that the number of the exception is corresponding to the message_id here,

Therefore, we can write the extension method:

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

/// <summary>        ///See if a constraint conflict/// </summary>        /// <param name= "Exception" ></param>        /// <returns></returns>         Public Static BOOLIssqlreferenceconstraintexception ( ThisException Exception) {            varBaseex =exception.            GetBaseException (); if(Baseex isSqlException) {                if((Baseex asSqlException). Number = =547)                {                    return true; }                Else                {                    return false; }            }            Else if(Baseex! =NULL&&!Object. ReferenceEquals (Exception, Baseex))//if the base class is not empty and does not equal the exception itself, proceed back to the investigation to find            {                returnissqlreferenceconstraintexception (Baseex); }            return false; }


The calling code is as follows:

Try{base_module C=NewBase_module () {RowID =RowID}; Entities. Entry (c). State=System.Data.EntityState.Deleted; Entities.                SaveChanges (); returngzapisuccess (); }            Catch(Dbupdateexception ex) {if(true==Ex. Issqlreferenceconstraintexception ()) {returnGzapibadrequest ("Delete failed with associated data", Enumresponsecode.errsqlreferenceconstraintexception, Sqloot.delete); }                Else Throwex; }


Reference: https://www.cnblogs.com/zuowj/p/4414031.html

Reference: http://www.cnblogs.com/liuzhixian/p/3839787.html

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.