相关疑难解决方法(0)

SQL Server:使用原始异常编号的Rethrow异常

我在存储过程中使用TRY CATCH块,其中有两个INSERT指令.

如果出现问题,CATCH块会负责回滚所做的所有更改并且工作正常,除了一件事!

我的ASP.NET应用程序捕获的异常是一个数字为50000的SqlException.这不是原始数字!(我期待的数字是2627)

在异常的Message属性中,我可以看到原始的异常编号和格式化的消息.

我怎样才能获得原始的例外号码?

try
{
    // ... code
}
catch
(SqlException sqlException)
{
    switch (sqlException.Number)
    {
        // Name already exists
        case 2627:
            throw new ItemTypeNameAlreadyExistsException();

        // Some other error
        // As the exception number is 50000 it always ends here!!!!!!
        default:
            throw new ItemTypeException();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,返回值已被使用.我想我可以使用输出参数来获取异常编号,但这是个好主意吗?

我该怎么做才能获得例外号码?谢谢

PS:这是必需的,因为我有两个INSERT指令.

c# sql asp.net exception-handling sql-server-2008

8
推荐指数
3
解决办法
8905
查看次数

标签 统计

asp.net ×1

c# ×1

exception-handling ×1

sql ×1

sql-server-2008 ×1