调用 sqlserver 存储过程时 SqlException.Message 重复

发布于 2024-09-19 08:44:15 字数 896 浏览 12 评论 0原文

我有一个存储过程,它提供了一个足够友好的错误,我想向用户显示该错误,但是当我从 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰ゝ天使的微笑 2024-09-26 08:44:15

请评论“PRINT @ErrorString;”陈述。
当SQL引擎抛出错误时,它会显示消息堆栈中可用的所有消息。

原因:

尝试运行下面的代码快照。

CREATE PROCEDURE ErrorHandler

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

   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;
        PRINT 'Stack overflow'
        RAISERROR(@ErrorString, 11 /*severity*/, 1 /*state*/);
    END CATCH
END
GO

EXEC ErrorHandler

SQL引擎生成3条消息;

1. 1st for PRINT @ErrorString;

2. 2nd for PRINT 'Stack overflow'

3. 3rd for RAISERROR(@ErrorString, 11 /*severity*/, 1 /*state*/);

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.

CREATE PROCEDURE ErrorHandler

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

   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;
        PRINT 'Stack overflow'
        RAISERROR(@ErrorString, 11 /*severity*/, 1 /*state*/);
    END CATCH
END
GO

EXEC ErrorHandler

SQL engine generates 3 messages;

1. 1st for PRINT @ErrorString;

2. 2nd for PRINT 'Stack overflow'

3. 3rd for RAISERROR(@ErrorString, 11 /*severity*/, 1 /*state*/);
稀香 2024-09-26 08:44:15

发布后,我立即删除了打印语句,结果发现打印语句作为异常消息的一部分被传递。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文