如何用c#代码识别sql存储过程抛出的自定义错误信息?
存储过程会像这样引发错误
RAISERROR (N'This is message %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
从上面,如何判断错误是自定义的错误信息。 (ie) 像这样的东西
try{
--actual code here
}
catch(SqlException ex)
{
--how to check here that the exception is custom one
}
最佳答案
当您引发错误时,您可以提供 MessageId 而不是消息文本。该数字将在异常的 Number
属性中找到:
SQL:
RAISERROR(50001, 12, 1)
C#:
if (sqlException.Number == 50001)
{
throw new CustomSQLException(//whatever);
}
关于c# - 处理 SQL 自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738963/