调用 sqlserver 存储过程时 SqlException.Message 重复
我有一个存储过程,它提供了一个足够友好的错误,我想向用户显示该错误,但是当我从 .net 调用它时,它会出现两次。当我从 sql server management studio 调用 proc 时,它只出现一次。
下面是存储过程的简化版本:
ALTER PROC [Production].[spDoSomething] (
@PassedID int)
AS
BEGIN
DECLARE @ErrorString nvarchar(500);
BEGIN TRY
...
RAISERROR('Bad things are happening.', 11 /*severity*/, 1 /*state*/);
...
END TRY
BEGIN CATCH
SET @ErrorString = 'Error found:' + ERROR_MESSAGE();
PRINT @ErrorString;
RAISERROR(@ErrorString, 11 /*severity*/, 1 /*state*/);
END CATCH
END
我在一些 c# 代码中使用 System.Data.SqlClient.SQLCommand
对象的 ExecuteNonQuery()
来调用它,然后捕获 < code>System.Data.SqlClient.SQLException 但消息包含
“错误:发现错误的事情正在发生。\n发现错误:错误的事情正在发生。”
有谁知道为什么会出现两次?
这是在 sql server 2008 和 .net 3.5 上
I have a stored procedure that gives a friendly enough error that I want to show to the users but when I call it from .net it comes out twice. When I call the proc from sql server management studio it only comes out once.
Here is a cutdown version of the stored proc:
ALTER PROC [Production].[spDoSomething] (
@PassedID int)
AS
BEGIN
DECLARE @ErrorString nvarchar(500);
BEGIN TRY
...
RAISERROR('Bad things are happening.', 11 /*severity*/, 1 /*state*/);
...
END TRY
BEGIN CATCH
SET @ErrorString = 'Error found:' + ERROR_MESSAGE();
PRINT @ErrorString;
RAISERROR(@ErrorString, 11 /*severity*/, 1 /*state*/);
END CATCH
END
I call this in some c# code using ExecuteNonQuery()
of a System.Data.SqlClient.SQLCommand
object then I catch a System.Data.SqlClient.SQLException
but the Message contains
"Error: Found Bad things are happening.\nError Found: Bad things are happening."
Does anyone know a reason why it comes out twice?
This is on sql server 2008 and .net 3.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请评论“PRINT @ErrorString;”陈述。
当SQL引擎抛出错误时,它会显示消息堆栈中可用的所有消息。
原因:
尝试运行下面的代码快照。
SQL引擎生成3条消息;
Please comment the "PRINT @ErrorString;" statement.
When SQL Engine throws an error, it would displays all the messages are available in the message stack.
Reason:
Try to run below code snap.
SQL engine generates 3 messages;
发布后,我立即删除了打印语句,结果发现打印语句作为异常消息的一部分被传递。
Immediately after posting I deleted the print statement and it turns out that the print statements get passed through as part of the exception message.