简体   繁体   English

过程(SQL Server 2014)中的Raiserror不能捕获客户端(C#)

[英]Raiserror in procedure (SQL Server 2014) does not catch client side(c#)

Stored procedure Catch block code. 存储过程捕获块代码。

alter PROCEDURE [dbo].[TESTError]      
(

)
AS
BEGIN

    SET NOCOUNT ON;

      BEGIN TRY
        SELECT 5/0
      END TRY 
    BEGIN CATCH

        DECLARE @ErrorNumber INT

           SELECT @ErrorNumber = ERROR_NUMBER() 

           RAISERROR
        (N'The error code is: %d',
             16, -- Severity.
              1, -- State.
             @ErrorNumber,     
             '');
END CATCH
END

The above stored procedure throw and show error when runs using SSMS. 使用SSMS运行时,以上存储过程引发并显示错误。

.Net Client code only exception portion. .Net客户端代码仅例外部分。

    catch (SqlException ex)
            {      
                string msg = string.Format("Error number: {0} / Message: {1}", ex.Number, ex.Message);
            }

When application called stored procedure it does not catch in exception block. 当应用程序调用存储过程时,它不会捕获异常块。

Any kind of hint or idea would help me. 任何暗示或想法都会对我有所帮助。

catch (SqlException ex)
        {      
            string msg = string.Format("Error number: {0} / Message: {1}", ex.Number, ex.Message);
        }

What are you expecting to happen, even if this catch-block was entered? 即使输入了该捕获块,您期望发生什么? A string variable would get a value which is gone the next micro second... 字符串变量将获得一个值,该值将在下一毫秒内消失。

Anyway: Whenever you deal with specialised (typed) catch blocks, you should have a good reason not to have a general catch-block additionally (as last of your specialised ones): 无论如何:每当您处理专用(类型化)捕获块时,您都应该有充分的理由不额外使用通用捕获块(这是您专用的捕获块的最后一项):

catch (System.Exception sysex)
        {      
            string msg = string.Format("Error number: {0} / Message: {1}", sysex.Number, sysex.Message);
            //Do something with this information!
        }

Or just 要不就

catch{throw;}

This depends on your needs and tools / habits of debugging... 这取决于您的需求和工具/调试习惯...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 客户端(C#)的更新过程未捕获过程(SQL Server 2005)中的Raiserror - Raiserror in procedure (SQL Server 2005) is not caught in update process in client side(c#) 如何从SQL Server的RAISERROR中捕获C#中的错误? - How can i catch error in C# from RAISERROR of sql server? 使用 ExecuteScalar 在客户端(C# Winform)的 SQL 服务器中从 try/catch 中捕获错误 - Catch an error from try/catch in SQL Server at client side (C# Winform) using ExecuteScalar 使用存储过程或C#同步SQL Server 2014和SQL Server 2014 Express数据库 - Sync SQL Server 2014 and SQL Server 2014 Express database using stored procedure or C# 从 .NET 中的 SQL 服务器捕获 RAISERROR - Catch RAISERROR from SQL Server in .NET DpoI SQL Server 2014 CTP 2 OLTP在C#中使用存储过程的内存选择语句中执行吗? - DpoI do SQL Server 2014 CTP 2 OLTP in memory select statement with store procedure in C#? c#asp.net:从SQL存储过程中捕获raiserror()消息 - c# asp.net: capture the raiserror() message from an SQL stored procedure 将 SQL `raiserror` 与 C# 中的 `select` 组合 - Combining SQL `raiserror` with `select` in C# 使用存储过程更新不起作用C#和SQL Server - update using the stored procedure does not work c# and SQL server 在C#中捕获RAISERROR - Capturing RAISERROR in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM