如果连接到SQL Server失败,如何从SqlException知道?

时间:2017-05-04 05:30:46

标签: c# asp.net sql-server sqlexception

我在ASP.Net应用程序中调用来自第三方dll的方法。此方法读取和写入SQL Server数据库。

当SQL Server服务没有运行时,上面的方法调用会抛出一个SqlException,其详细信息如下所示。

我的问题是如何确定错误发生,因为无法建立与SQL Server的连接。如果异常被称为SqlConnectionFailedException或更具体的失败连接,那么它很容易,因为我可以检查catch块中的Exception类型。 SQLException太通用了。

一个解决方案是我可以检查SqlException的消息是否包含字符串A network-related or instance-specific error occurred while establishing a connection to SQL Server,但可能有更好的方法来解决这类问题。

问题: 如何在catch块中知道由于无法建立与SQL Server数据库的连接而发生错误?

SQL Server服务未运行时抛出异常

  System.Data.SqlClient.SqlException occurred
  _HResult=-2146232060
  _message=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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)
  HResult=-2146232060
  IsTransient=false
  Message=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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)
  Source=.Net SqlClient Data Provider
  ErrorCode=-2146232060
  _doNotReconnect=false
  Class=20
  LineNumber=0
  Number=2
  Server=""
  State=0
  StackTrace:
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  InnerException: System.ComponentModel.Win32Exception
       _HResult=-2147467259
       _message=The system cannot find the file specified
       HResult=-2147467259
       IsTransient=false
       Message=The system cannot find the file specified
       ErrorCode=-2147467259
       NativeErrorCode=2
       InnerException: 

SQL Server服务未运行时失败的C#代码

try {
 //call a method from third-party dll that reads/writes to a SQL Server database
 Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("Jobs");
}
catch(SqlException sqlEx) {
 //I want to know if error was due to not being to connect to SQL Server database
}
catch(Exception ex) {
 //some custom error handling logic
}

1 个答案:

答案 0 :(得分:1)

您可以查看error numbers,而不是检查字符串。 这可能是最好的解决方案。

您可以使用此SQL查询来获取所有可能的错误代码:

SELECT * FROM sysmessages

然后,您应该使用switch,case来获取正确的错误并以不同的方式处理它们。

try    
{
    // Your Hangfire / SQL Call
}
catch (SqlException ex)
{
     switch (ex.Number) 
     { 
          case 262: //%ls permission denied in database '%.*ls'.
                //Do something
                break;
          default:
                //Do something
                break;
     }
}

SqlException.NumberSqlException.Errors[1].Number的包装器,例如。 SqlException.Errors

中第一个错误的错误编号

同时确保在捕获异常时记录足够的数据,这可以为您节省大量时间。

您还应该检查以下问题:

SqlException catch and handling

Know when to retry or fail when calling SQL Server from C#?

这些应该让你走得很远。