如何捕获特定的 SqlException 错误?
问:有没有更好的方法来处理SqlExceptions?
以下示例依赖于对消息中文本的解释。
Eg1:我有一个现有的 try catch 来处理表不存在的情况。
忽略我可以首先检查表是否存在的事实。
try
{
//code
}
catch(SqlException sqlEx)
{
if (sqlEx.Message.StartsWith("Invalid object name"))
{
//code
}
else
throw;
}
Eg2:没有 try catch 显示重复的键异常
if (sqlEx.Message.StartsWith("Cannot insert duplicate key row in object"))
解决方案:我的 SqlExceptionHelper 的开始
//-- to see list of error messages: select * from sys.messages where language_id = 1033 order by message_id
public static class SqlExceptionHelper
{
//-- rule: Add error messages in numeric order and prefix the number above the method
//-- 208: Invalid object name '%.*ls'.
public static bool IsInvalidObjectName(SqlException sex)
{ return (sex.Number == 208); }
//-- 2601: Cannot insert duplicate key row in object '%.*ls' with unique index '%.*ls'. The duplicate key value is %ls.
public static bool IsDuplicateKey(SqlException sex)
{ return (sex.Number == 2601); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
SqlException 有一个 Number 属性你可以检查一下。对于重复错误,编号为 2601。
要从服务器获取所有 SQL 错误的列表,请尝试以下操作:
更新
现在可以在 C# 6.0 中简化此操作
The SqlException has a Number property that you can check. For duplicate error the number is 2601.
To get a list of all SQL errors from you server, try this:
Update
This can now be simplified in C# 6.0
有点,有点。请参阅数据库引擎错误的原因和解决
方法 好的 DAL 层将在 T-SQL(存储过程)中
TRY/CATCH
进行操作,其模式类似于 异常处理和嵌套事务。唉,T-SQLTRY/CATCH
块无法引发原始错误代码,必须引发一个新错误,代码超过 50000。这使得客户端处理出现问题。在 SQL Server 的下一个版本中,有一个新的 THROW 构造允许从 T-SQL catch 块重新引发原始异常。Sort of, kind of. See Cause and Resolution of Database Engine Errors
The problem though is that a good DAL layer would us
TRY/CATCH
inside the T-SQL (stored procedures), with a pattern like Exception handling and nested transactions. Alas a T-SQLTRY/CATCH
block cannot raise the original error code, will have to raise a new error, with code above 50000. This makes client side handling a problem. In the next version of SQL Server there is a new THROW construct that allow to re-raise the original exception from T-SQL catch blocks.最好使用错误代码,不必解析。
如何找出应该使用208:
It is better to use error codes, you don't have to parse.
How to find out that 208 should be used:
如果您想要 Sql 服务器中遇到的错误消息列表,您可以查看
If you want list of error messages met in Sql server, you can see with
您可以基于严重性类型进行评估。
请注意,要使用此功能,您必须订阅 OnInfoMessage
那么您的 OnInfoMessage 将包含:
这样您就可以根据严重性而不是错误编号进行评估,并且更加有效。您可以在此处找到有关严重性的更多信息。
You can evaluate based on severity type.
Note to use this you must be subscribed to OnInfoMessage
Then your OnInfoMessage would contain:
This way you can evaluate on severity rather than on error number and be more effective. You can find more information on severity here.
使用 MS SQL 2008,我们可以在表 sys.messages 中列出支持的错误消息
With MS SQL 2008, we can list supported error messages in the table sys.messages
如果您正在寻找更好的方法来处理 SQLException,您可以采取以下措施。首先,Spring.NET 做了与您正在寻找的类似的事情(我认为)。这是他们正在做的事情的链接:
http://springframework.net /docs/1.2.0/reference/html/dao.html
另外,您可以检查错误代码 (
sqlEx.Number
),而不是查看消息。这似乎是识别发生了哪个错误的更好方法。唯一的问题是每个数据库提供程序返回的错误号可能不同。如果您打算更换提供商,您将回到原来的处理方式或创建一个抽象层来为您转换此信息。以下是一个使用错误代码和配置文件来翻译和本地化用户友好的错误消息的示例:
https://web.archive.org/web/20130731181042/http://weblogs.asp.net/guys/archive/2005/05/20/408142.aspx
If you are looking for a better way to handle SQLException, there are a couple things you could do. First, Spring.NET does something similar to what you are looking for (I think). Here is a link to what they are doing:
http://springframework.net/docs/1.2.0/reference/html/dao.html
Also, instead of looking at the message, you could check the error code (
sqlEx.Number
). That would seem to be a better way of identifying which error occurred. The only problem is that the error number returned might be different for each database provider. If you plan to switch providers, you will be back to handling it the way you are or creating an abstraction layer that translates this information for you.Here is an example of a guy who used the error code and a config file to translate and localize user-friendly error messages:
https://web.archive.org/web/20130731181042/http://weblogs.asp.net/guys/archive/2005/05/20/408142.aspx
我首先使用代码、C# 7 和实体框架 6.0.0.0。它对我有用
注意:我将项目添加到数据库的查询位于另一个项目(层)中
I am working with code first, C# 7 and entity framework 6.0.0.0. it works for me
N.B: my query for add item to db is in another project(layer)
对于那些在从另一台机器连接到数据库时(例如,在表单加载时)可能会抛出 SQL 错误的新手,您会发现当您第一次在 C# 中设置一个指向 SQL Server 数据库的数据表时,它将设置如下连接:
您可能需要删除此行并将其替换为其他内容,例如 MSDN 等中提到的传统连接字符串。
http://www.connectionstrings.com/sql-server-2008
For those of you rookies out there who may throw a SQL error when connecting to the DB from another machine(For example, at form load), you will find that when you first setup a datatable in C# which points to a SQL server database that it will setup a connection like this:
You may need to remove this line and replace it with something else like a traditional connection string as mentioned on MSDN, etc.
http://www.connectionstrings.com/sql-server-2008