2

如何使用 c# 代码识别从 sql 存储过程引发的自定义错误消息?

存储过程会像这样引发错误

RAISERROR (N'This is message %s %d.', -- Message text.
       10, -- Severity,
       1, -- State,
       N'number', -- First argument.
       5); -- Second argument.

从上面,如何识别错误是自定义错误消息。(即)像这样的东西

try{
   --actual code here
}
catch(SqlException ex)
{
    --how to check here that the exception is custom one
} 
4

1 回答 1

6

当您提出错误时,您可以提供 MessageId 而不是 Message 文本。此编号将在NumberException 的属性中找到:

SQL:

    RAISERROR(50001, 12, 1) 

C#:

    if (sqlException.Number == 50001)
    {
        throw new CustomSQLException(//whatever);
    }
于 2013-07-19T06:03:18.340 回答