2

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: enter image description here

Community
  • 1
  • 1
ck84vi
  • 1,556
  • 7
  • 27
  • 49
  • 4
    A "first chance exception" is an indication of the exception _before_ it can be caught in your try/catch block. This is just a debugging feature - see for instance http://stackoverflow.com/questions/58380/avoiding-first-chance-exception-messages-when-the-exception-is-safely-handled/60765#60765 – C.Evenhuis Dec 16 '15 at 09:05
  • @C.Evenhuis: Yes, but if I ignore it or deactivate it in the exception settings this will not help me. Because the exception will still not get caught, therefore I cant check if the server is running or not, which is my goal. – ck84vi Dec 16 '15 at 09:08
  • In general, there's no *point* in checking whether the server exists during startup because such a check offers you *no* guarantee that the server will still be up even a nanosecond after your check has completed, and before you attempt to use it for any real purpose. You need to write code that copes with it not being available at *any* point in time, since it is independent from *your* code. – Damien_The_Unbeliever Dec 16 '15 at 09:10
  • 3
    @ck84vi first chance exceptions that don't end up in your catch block are handled in a lower level catch block (honestly though, I don't believe the claim that the exception does not end up in either `catch` block, sorry) – C.Evenhuis Dec 16 '15 at 09:13
  • 1
    put `conn = new SqlConnection("Data Source=AES0020\\ESQLSRV;Initial Catalog=MaterialData;Integrated Security=true;");` in try block – sarath Dec 16 '15 at 09:14
  • @ Damien_The_Unbeliever: Yes I agree. BUT I don't have an better idea how to do it since I call log4net.Config.XmlConfigurator.Configure(); and one of my apenders is a ADO.NET apender. If the server is not running I get problems there. So it would be nice to know exactly this nano second before i call Configure() to know if the server is running or not. Since I'm logging my application logs to this server during startup also. If you have a better solution please let me know. – ck84vi Dec 16 '15 at 09:15
  • Just confirming: Debug -> Windows -> Exception Settings -> Common Language Runtime Exceptions is UNChecked? – Kris Dec 16 '15 at 09:32
  • @Damien_The_Unbeliever: I do not agree at all regarding the check. The point is not to get a contract saying that the server will be there forever. The check is made to detect configuration errors (mistyped host name or mistyped credentials). Better to get it directly than later when the application is running. – jgauffin Dec 16 '15 at 11:37
  • @ck84vi: I too say that you are wrong. If you press F5 when you get the first chance exception it will go to the catch block. – jgauffin Dec 16 '15 at 11:39
  • @jgauffin - the reason I take such an absolute (apparent) position is because of the number of times I've encountered code which *assumes* it can pre-check success for resources outside of its own control. People *forget* to write the code that copes with servers going down, etc, and it's that code that they absolutely do need to write. – Damien_The_Unbeliever Dec 16 '15 at 11:45
  • Do you or do you not have an Exception Settings - "Break When Thrown" set? – Kris Dec 17 '15 at 03:07

0 Answers0