1

我有一个 Azure 辅助角色在 SQL Azure 数据库上执行简单的选择。很少会引发以下 SqlException。

日志

基础提供程序在打开时失败。内部异常:超时已过期。在操作完成之前超时时间已过或服务器没有响应。异常类型:System.Data.SqlClient.SqlException

异常不会作为 SqlException 捕获。它被通用异常处理程序捕获。关于为什么会这样的任何建议?

try{
}
catch(System.Data.SqlClient.SqlException sqlExcep)
{
}
catch(Exception genericExcep)
{
    **//The exception is caught as a generic exception**
}
4

3 回答 3

1

The SQL Database environment is a layer of routers and proxies that handle network load balancing and resource management. If SQL Database itself didn't timeout, then something else in the middle could have (although that's typically rare).

I usually handle IOException errors as well and treat some of them as a form of transient error. What exception type are you actually receiving?

Did you try implementing the Transient Fault Handling Application Block? http://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50)

Herve

于 2012-09-06T21:31:42.173 回答
1

看起来这是内部异常,而不是您捕获的实际异常。外部异常的类型是什么?您唯一能做的就是捕获捕获的异常类型,然后检查内部异常:

try
{
    // Stuff
}
catch (Exception exc)
{
    if (exc.InnerException is System.Data.SqlClient.SqlException)
    {
        var sqlException = exc.InnerException as System.Data.SqlClient.SqlException;

        // Do stuff with the error.
    }
}

这个故事的寓意是你不能明确地捕捉到内部异常:(

于 2012-09-06T21:38:20.603 回答
1

尝试Microsoft.Data.SqlClient.SqlException而不是 System.Data.SqlClient.SqlException

        catch (Microsoft.Data.SqlClient.SqlException ex)
        {

        }
于 2020-07-10T14:46:19.660 回答