title: How to know from SqlException if connecting to SQL Server failed?
body: <p>I am calling a method from a third-party dll in my ASP.Net app. This method reads and writes to a SQL Server database.</p>
  
  <p>When the SQL Server service is not running, then the above method call throws a SqlException whose details are as below.</p>
  
  <p>My problem is how I can determine that the error occurred because connection to SQL Server could not be established. If the exception was called SqlConnectionFailedException or something more specific for failed connections then it would be easy since I could just check for type of Exception in the catch block. A SQLException is too generic an exception.</p>
  
  <p>One solution is that I could check if message of SqlException contains the string <code>A network-related or instance-specific error occurred while establishing a connection to SQL Server</code> but may be there is a better way to solve this type of problem.</p>
  
  <p><strong>Question</strong>:
  How can I know in catch block that error occurred because connection to SQL Server database could not be established?</p>
  
  <p><strong>Exception thrown when SQL Server service is not running</strong></p>
  
  <pre><code>  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: 
  </code></pre>
  
  <p><strong>C# code that fails when SQL Server service is not running</strong></p>
  
  <pre><code>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
  }
  </code></pre>