SQL Server Raiserror不会在.NET客户端中导致异常

时间:2021-02-25 01:54:29

I have a stored procedure on a SQL Server 2005 database which has a statement like this:

我在SQL Server 2005数据库上有一个存储过程,它有一个这样的语句:

  IF @Condition = 0
    BEGIN
        RAISERROR('some error message',16,1)
        RETURN
    END

and it's called from a C# client like so:

它是从C#客户端调用的,如下所示:

 try
           {
                SomeVariable = SqlHelper.ExecuteScalar(GetConnectionString(), "MySP", new object[] { param1, param2});
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
            }

However there's no exception being raised. The condition in the SP is always true for testing. To verify this, I copied the call from SQL Server Profiler and executed it in a query window and the ErrorMessage was printed which means the error is raised.

然而,没有例外被提出。 SP中的条件始终适用于测试。为了验证这一点,我从SQL Server Profiler复制了调用并在查询窗口中执行了它,并且打印了ErrorMessage,这意味着引发了错误。

Not sure what's happening.

不确定发生了什么。

4 个解决方案

#1


6  

I went through the SQL Helper class and found out that ExecuteScalar eats the exception and returns null. I switched to ExecuteDataSet which doesn't do this. I expected the different Execute.. method to behave the same way. The other way is to use ExecuteScalar and when the SP detects an error, it does a SELECT some error number which can be handled in the client.

我浏览了SQL Helper类,发现ExecuteScalar吃了异常并返回null。我切换到不执行此操作的ExecuteDataSet。我期望不同的Execute ..方法的行为方式相同。另一种方法是使用ExecuteScalar,当SP检测到错误时,它会执行一些SELECT错误号,可以在客户端中处理。

#2


2  

According to SQL Books online severity of 16 "Indicates general errors that can be corrected by the user." - so that severity is OK.

根据SQL Books的在线严重性为16“表示可以由用户更正的一般错误。” - 这样严重性就可以了。

I only have SQL 2008 to work with, but I have tested the RAISERROR('some error message',16,1) and the error was caught in my c# app. Are you sure the error is not being handled in your "SqlHelper" class?

我只使用SQL 2008,但我已经测试了RAISERROR('一些错误消息',16,1)并且我的c#app中发现了错误。您确定在“SqlHelper”类中没有处理错误吗?

#3


1  

From the Books online

来自在线图书

Specify a severity of 10 or lower to use RAISERROR to return a message from a TRY block without invoking the CATCH block.

指定严重性为10或更低,以使用RAISERROR从TRY块返回消息而不调用CATCH块。

#4


0  

Is it something to do with the severity level? If you up severity to 19 does it raise the exception through to your code?

这与严重程度有关吗?如果您将严重性提高到19,它会将异常提升到您的代码吗?

#1


6  

I went through the SQL Helper class and found out that ExecuteScalar eats the exception and returns null. I switched to ExecuteDataSet which doesn't do this. I expected the different Execute.. method to behave the same way. The other way is to use ExecuteScalar and when the SP detects an error, it does a SELECT some error number which can be handled in the client.

我浏览了SQL Helper类,发现ExecuteScalar吃了异常并返回null。我切换到不执行此操作的ExecuteDataSet。我期望不同的Execute ..方法的行为方式相同。另一种方法是使用ExecuteScalar,当SP检测到错误时,它会执行一些SELECT错误号,可以在客户端中处理。

#2


2  

According to SQL Books online severity of 16 "Indicates general errors that can be corrected by the user." - so that severity is OK.

根据SQL Books的在线严重性为16“表示可以由用户更正的一般错误。” - 这样严重性就可以了。

I only have SQL 2008 to work with, but I have tested the RAISERROR('some error message',16,1) and the error was caught in my c# app. Are you sure the error is not being handled in your "SqlHelper" class?

我只使用SQL 2008,但我已经测试了RAISERROR('一些错误消息',16,1)并且我的c#app中发现了错误。您确定在“SqlHelper”类中没有处理错误吗?

#3


1  

From the Books online

来自在线图书

Specify a severity of 10 or lower to use RAISERROR to return a message from a TRY block without invoking the CATCH block.

指定严重性为10或更低,以使用RAISERROR从TRY块返回消息而不调用CATCH块。

#4


0  

Is it something to do with the severity level? If you up severity to 19 does it raise the exception through to your code?

这与严重程度有关吗?如果您将严重性提高到19,它会将异常提升到您的代码吗?