Catch SQL raise error in C#

后端 未结 3 561
既然无缘
既然无缘 2020-12-25 11:50

I generate the raise error in SQL procedure:

RAISERROR(\'Already exist\',-10,-10)

but I can not catch it using the following co

3条回答
  •  执念已碎
    2020-12-25 12:25

    RAISERRORs with a SEVERITY value under or equal to 10 are not caught on the C# side because I suppose they are considered just warnings as you can see from the list at Database Engine Error Severity

    SEVERITY values between 11 and 16 are errors that can be corrected by the user, so, for example, you can try with:

    RAISERROR('Already exist',16,1)
    

    Otherwise you could choose another error code from from the list above or, if you really need it, prepare your own custom error message using sp_addmessage.

提交回复
热议问题