How can I get an error message that happens when using ExecuteNonQuery()?(如何在使用 ExecuteNonQuery() 时收到错误消息?)
问题描述
我正在以这种方式执行命令:
I am executing a command in this way :
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?
推荐答案
如果你的错误的严重性为 16 或更高,你只会在 C# 中得到一个异常.如果您使用的是 PRINT,则在 .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.
如果您可以编辑 raise 错误代码,这将导致 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.
附带说明 - 如果您之后不直接 RETURN
,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() 时收到错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在使用 ExecuteNonQuery() 时收到错误消息?


- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 使用 rss + c# 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01