我对货币字段有限制,不能接受负值。约束一切正常,当违反约束时,我会收到错误,如下所示。
Unable to update row. Reason: The UPDATE statement conflicted with the CHECK constraint "CK_Product_StandardCost". The conflict occurred in database "AdventureWorksLT", table "SalesLT.Product", column 'StandardCost'. The statement has been terminated.
我在 catch block 中收到此错误,因为我正在更新表中的不正确数据。
我想要的是在特定的此约束触发时获取列名称。这里是:StandardCost。有什么方法可以在我的 C# 代码中找到列名称吗?
最佳答案
异常消息上带有 IndexOf
的简单 Substring
:
var message = ex.Message;
var column = message.Substring(message.IndexOf("column '") + 8, message.IndexOf("'", message.IndexOf("column '") + 9) - message.IndexOf("column '") - 8);
关于c# - 如何在 C# 中违反约束的异常中获取列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736821/