gpt4 book ai didi

c# - 处理 SQL 自定义异常

转载 作者:行者123 更新时间:2023-12-02 17:43:29 25 4
gpt4 key购买 nike

如何用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com