捕获 SQL 连接错误并重试

Catching SQL Connection Error and retrying

我有一个 Web 应用程序,它工作正常并且没有 运行 出现任何问题。但是,如果 SQL 服务器正在重新启动,或者如果它失去连接,则应用程序会因标准错误而失败:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我已经在 try / catch 块中完成了所有初始化,但是,它似乎并没有阻止这个错误,我想要它做的就是捕获错误,等待 10 秒,然后然后再试一次——我已经整理好了重试代码;它只是不会捕获此错误。

谢谢

您能否在 Global.asax 文件中的 Application_Error 中全局捕获此内容:

void Application_Error(object sender, EventArgs e) 
{ 
    var error = Server.GetLastError();
    //... do your handling here ...
}