我怎样才能使用的ExecuteNonQuery()时发生的错误消息? [英] How can I get an error message that happens when using ExecuteNonQuery()?
问题描述
我是这样执行的命令:
var Command = new SqlCommand(cmdText, Connection, tr);
Command.ExecuteNonQuery();
在有一个错误的命令,但是.NET不会引发任何错误消息。我怎么知道该命令没有正确地执行,以及如何得到异常?
In the command there is an error, however .NET does not throw any error message. How could I know that the command did not executed properly, and how to get the exception?
推荐答案
您得到的就只是在C#中的异常,如果你的错误的严重性为16岁或以上。如果您使用的是打印件,你不会得到在.NET中的异常。
You'll only get an exception in C# if your error's severity is 16 or above. If you are using a PRINT, you won't get an exception in .NET.
如果您可以编辑引发错误code,这会导致在C#中的SQLException:
If you can edit the raise error code, this would cause a SqlException in C#:
RAISERROR('Some error message', 16, 1)
然后,您可以在SqlException.Errors收集各个错误。
You can then get to each individual error in the SqlException.Errors collection.
只是一个侧面说明 - SQL Server将继续运行后的 RAISERROR
如果你不这样做返回
直接算账。如果你不回,你可以得到多个错误了。
Just a side-note - SQL Server will continue to run commands after the RAISERROR
if you don't RETURN
directly afterwards. If you don't return, you can get multiple errors back.
这篇关于我怎样才能使用的ExecuteNonQuery()时发生的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!