I have read so much on the RASIERROR function and its requirements for making the exception raise in .NET, but I still can't get the exception to be rasied. Here is the SP snippet:
我已经阅读了很多关于RASIERROR函数及其在.NET中引发异常的要求,但我仍然无法得到异常的rasied。这是SP片段:
BEGIN
RAISERROR('This is a custom error message.', 16, 1);
RETURN;
END
And here is the .NET code snippet:
这是.NET代码片段:
cmd.Connection = conn
cmd.ExecuteNonQuery()
'Close the db connection, and local dispose
conn.Close()
Catch ex As SQLException
Dim msg As String = ex.Message
End Try
So I think I have covered the bases:
所以我想我已经涵盖了基础:
- Severity level above '10' (using 16)
- Catching SQLException type (although it inherits from Exception so shouldn't matter I would think
- Using 'ExecuteNonQuery'
- Within SQL Enterprise Manager, when I run the stored proc directly, I do get the error : "Msg 50000, Level 16, State 1, Line 16 This is a custom error message."
严重级别高于'10'(使用16)
捕获SQLException类型(尽管它继承自Exception,所以我认为无所谓
在SQL企业管理器中,当我直接运行存储过程时,我收到错误:“消息50000,级别16,状态1,行16这是一个自定义错误消息。”
So what am I missing? Any help is appreciated, thanks!
那么我错过了什么?任何帮助表示赞赏,谢谢!
2 个解决方案
#1
1
It works just fine for me:
它适用于我:
SqlConnection conn = new SqlConnection("...");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "errTest";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
cmd.ExecuteNonQuery();
CREATE PROCEDURE errTest
AS
RAISERROR('This is a custom error message.', 16, 1);
RETURN;
#2
0
I figured this out; all of the code I posted worked as is 100%. Silly mistake on my part. The severity level was set to '16' but the actual sp had not been executed (I was working off a 'debug' version created by EM that represented the sp; right-click 'debug') and was still set to '10'. Once the update occured .NET immdeatly trapped the exception. Thanks for helping anyways, I do appreciate it.
我想出来了;我发布的所有代码都是100%。愚蠢的错误对我而言。严重性级别设置为“16”但实际sp未执行(我正在处理由EM创建的代表sp的'debug'版本;右键单击'debug')并且仍然设置为'10' 。一旦更新发生.NET不可避免地陷入异常。无论如何,谢谢你的帮助,我很感激。
#1
1
It works just fine for me:
它适用于我:
SqlConnection conn = new SqlConnection("...");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "errTest";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
cmd.ExecuteNonQuery();
CREATE PROCEDURE errTest
AS
RAISERROR('This is a custom error message.', 16, 1);
RETURN;
#2
0
I figured this out; all of the code I posted worked as is 100%. Silly mistake on my part. The severity level was set to '16' but the actual sp had not been executed (I was working off a 'debug' version created by EM that represented the sp; right-click 'debug') and was still set to '10'. Once the update occured .NET immdeatly trapped the exception. Thanks for helping anyways, I do appreciate it.
我想出来了;我发布的所有代码都是100%。愚蠢的错误对我而言。严重性级别设置为“16”但实际sp未执行(我正在处理由EM创建的代表sp的'debug'版本;右键单击'debug')并且仍然设置为'10' 。一旦更新发生.NET不可避免地陷入异常。无论如何,谢谢你的帮助,我很感激。