简体   繁体   English

从SqlException中提取结构化信息

[英]Extract structured information from SqlException

Let's say I executed an SQL statement from a C# application that caused an SqlException with the following error message to be thrown: 假设我从C#应用程序执行了一条SQL语句,该语句导致SqlException并抛出以下错误消息:

Violation of UNIQUE KEY constraint 'MyUniqueConstraint'. 违反UNIQUE KEY约束“ MyUniqueConstraint”。 Cannot insert duplicate key in object 'MyTable'. 无法在对象“ MyTable”中插入重复键。 The duplicate key value is (MyValue). 重复的键值为(MyValue)。

I understand that the entire string comes from the database. 我知道整个字符串都来自数据库。 I would like to extract the relevant parts from the message - similar to this question , but I want to actually extract data such as MyUniqueConstraint , MyTable and MyValue , not just the SQL error number. 我想从消息中提取相关部分-类似于此问题 ,但我实际上想提取数据,例如MyUniqueConstraintMyTableMyValue ,而不仅仅是SQL错误号。

Actually parsing the message is not an option as it is error-prone: you have to do it for every possible error, and if the error text changes from one version of SQL server to another, there would be serious problems . 实际上,解析消息不是一个选项,因为它容易出错:您必须为每个可能的错误执行此操作,并且如果错误文本从一个版本的SQL Server更改为另一个版本, 则将存在严重问题

Is it possible to reasonably obtain such structured information from the application when a database-level error occurs (ideally from the exception)? 当发生数据库级错误时(理想情况下是从异常中),是否可以从应用程序中合理地获取此类结构化信息?

Not long ago I faced a similar problem , and I found out that even filtering SqlExceptions based on their numbers is problematic. 不久前,我遇到了类似的问题 ,并且发现即使根据其数量过滤SqlExceptions也是有问题的。

Other than that, I haven't yet encountered a solution that could get data details from SqlException. 除此之外,我还没有遇到可以从SqlException获取数据详细信息的解决方案。 That is, other than parsing the message. 也就是说,除了解析消息。 One of the problems here will be that text of server message is different for different languages and server versions 这里的问题之一是服务器消息的文本对于不同的语言和服务器版本是不同的

Actually I recall there is a system table/view in the mssql master database that contains all error messages for different languages. 实际上,我还记得mssql主数据库中有一个系统表/视图,其中包含所有不同语言的错误消息。 You can try parsing the messages based on that, but some SqlExceptions aren't even from the server side of things... 您可以尝试基于此解析消息,但是某些SqlException甚至不是从服务器端开始的...

I want to say at least one remotely helpful thing here, so: 我想在这里至少说一点有用的事情,所以:

In the namespace System.Data there is DBConcurrencyException which seems to be usefull to an extent (meaning: in a single case that is not entirely related to this question). 在名称空间System.Data中,存在DBConcurrencyException ,它在某种程度上似乎很有用(意味着:在一种情况下,与该问题并不完全相关)。 It has Row property (row that caused the exception to be thrown, you can use it to get the table name and the data). 它具有Row属性(导致引发异常的行,您可以使用它来获取表名和数据)。

Wishfull thinking. 一厢情愿。

I understand that the entire string comes from the database 我知道整个字符串都来自数据库

Yes, it does. 是的,它确实。 It is a string. 它是一个字符串。

Actually parsing the message is not an option as it is error-prone 实际上,解析消息不是一个选项,因为它容易出错

Parsing is the ONLY option as there are only 2 items provided by the database: The sql error number and the string. 解析是唯一选项,因为数据库仅提供2个项目:sql错误号和字符串。 So, either you come up with a generic crystal ball or you work with what you have - which means parsing. 因此,您要么想出一个通用的水晶球,要么就用自己拥有的东西工作-这就是解析。

No database that I Know of provides more information mysteriously hidden in the exception - and if it would do so, it would not be a standard approach across databases anyway. 据我所知,没有一个数据库能够提供神秘地隐藏在异常中的更多信息-如果这样做,反正不是跨数据库的标准方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM