RAISERROR―如何与SqlException区分? [英] RAISERROR―How to distinguish with SqlException?

查看:57
本文介绍了RAISERROR―如何与SqlException区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约3-4个存储过程(可以根据需要进行修改),它们使用 RAISERROR 通知我的应用程序数据库方面的一些致命错误.其中一些存储过程从C#端使用 ExecuteNonQuery 执行,而其他存储过程则通过 ExecuteReader 执行.目前,我将这些命令包装在 try {...} catch(SqlException ThisSqlException){...} 块中,但问题在于,至少在两个情况下会抛出此异常我必须单独处理的情况:

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 of these stored procedures are executed from the C# side with ExecuteNonQuery, while other are executed with ExecuteReader. At the moment, I am wrapping these command in a try { ... } catch (SqlException ThisSqlException) { ... } block, but the problem is that this exception will be thrown in at least two scenarios I must deal with separately:

1)连接本身或参数错误或类型不匹配的错误;和

1) Errors with the connection itself, or with faulted or type-mismatched parameters; and

2)每当我明确使用 RAISERROR 时都会发生错误.

2) Errors that occur whenever I use RAISERROR explicitly.

由于这是WCF应用程序,因此我必须根据异常的性质(无论是否由于 RAISERROR 命令而导致的异常)返回给客户端应用程序不同的反馈.那么,我该如何区分这两种情况?

Since this is a WCF application, I must return to the client application different feedback based on the nature of the exception (whether it was due to a RAISERROR command or not). How can I, then, distinguish between both situations?

推荐答案

code> RAISERROR 命令包含msg_id参数,该参数可用于识别错误的类型.此值通过>代码提供给应用程序> SqlException.Number 属性.这样,您可以识别存储过程引发的任何异常,其中包括系统中定义的自定义错误消息.

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.

如果通过文本字符串错误消息调用 RAISERROR ,则 Number 将为50000.

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

这篇关于RAISERROR―如何与SqlException区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆