RAISERROR―How to distinguish with SqlException?

后端 未结 2 1158
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 10:27

I have some 3-4 stored procedures ― which I can modify if needed ― that use RAISERROR to inform my application of some fatal errors on the database side. Some

相关标签:
2条回答
  • 2020-12-30 11:00

    The RAISERROR command includes a msg_id parameter, which can be used to identify the type of error. This value is supplied to the application through the SqlException.Number property. In this way, you can identify any exception raised by a stored procedure that includes a custom error message that is defined in the system.

    If RAISERROR is called with a text string error message, then Number will be 50000.

    0 讨论(0)
  • 2020-12-30 11:07

    When you catch a SqlException, you can inspect its Errors collections which contains the detailed error messages.

    Those SqlError objects contained very detailed information - including error code, message and so forth.

    Using that information, you should be able to easily distinguish between connection-based errors, or errors that your raise yourself.

    0 讨论(0)
提交回复
热议问题