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.
When the SQL Server service is not running, then the above method call throws a SqlException whose details are as below.
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.
One solution is that I could check if message of SqlException contains the string A network-related or instance-specific error occurred while establishing a connection to SQL Server
but may be there is a better way to solve this type of problem.
Question: How can I know in catch block that error occurred because connection to SQL Server database could not be established?
Exception thrown when SQL Server service is not running
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:
C# code that fails when SQL Server service is not running
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
}