To check if a SQL Server instance exists/is running during startup of my application I want to establish a connection to the required instance.
I do it like that:
SqlConnection conn = null;
conn = new SqlConnection("Data Source=AES0020\\ESQLSRV;Initial Catalog=MaterialData;Integrated Security=true;");
try
{
conn.Open();
}
catch (SqlException ex)
{
MessageBox.Show("Sql exception");
}
catch (Exception ex)
{
MessageBox.Show("Exception");
}
So my understanding is that if it is not possible to establish a connection my try - catch block should catch an exception. But it does not. When running the application I always get an exception at
conn.Open();
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: 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)
But it never hits the catch block.
Why does this happen?
In general, is there a better way to test if a SQL Server, on the same machine, is running?
Yes, I have also checked this post No exception when opening SqlConnection in C#. This does not solve my problem.
EDIT:
For those who don't believe that the exception does not get caught: