tags:

views:

134

answers:

6

Here is the problem. User can enter server name and connection string for database. If server is not accessible (wrong address, firewalls or any other issue), I want to be aware of this as quickly as possible.

If I'm using sqlConnection and try to connect to a non-accessible server this takes very long (I think more than 1 minute!). This has nothing to do with connection timeout btw so setting this property won't help.

My idea is to first try to ping server and if I get response (which means server is accessible from the point of application), than proceed with sqlConnection. If there is no response from ping, operation is aborted and user is properly notified.

Is there any better way of doing this? Any idea would be welcome.

Forgot to point out, I'm using NHibernate so I can't use any MSSQL specific library. Also, target server can have Linux as OS, not just Windows.

+3  A: 

This previous question's answer has a decent approach that is more elegant that just a ping, using WMI will give you more info overall

curtisk
This would assume that there is Windows OS on targeting system, right? If that's the case than I cannot use this approach.
buhtla
that would be very true, this is wholly windows centric...as an approach for mysql on linux would most likely be wholly different than oracle on windows or any other combination
curtisk
+1  A: 

I think its worth noting that the only really good way to check a generic database's existence and accessibility is to access it. A machine could be configured with a perfectly operational database, and be refusing pings, for example. Similarly, a machine could be running a DB instance and be configured to ignore/refuse all WMI queries. If you can assume for your environments that these are not true (i.e., you know that all your companies machines will always answer pings) then you can continue; otherwise, you might have to just take the 'attempting to connect' hit.

GWLlosa
At this moment I'm close to accept ping as a good enough solution. Of course, I'm aware that this is not bullet proof solution so I'm hoping that there is some other way to achieve my goal.I can't make any assumptions regarding deployment environment, server OS or database so I need some generic method for determining server presence.
buhtla
A: 

could you use the SqlDataSourceEnumerator to get a DataTable with all available SQLServers, and then check the users input against the list. Something like this. See MSDN.

System.Data.DataTable table = VisibleServerList.GetVisibleServers();

public static class VisibleServerList
{
    public static System.Data.DataTable GetVisibleServers()
    {
        System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance;
        return instance.GetDataSources();
    }
}
Tester101
In this application that I have I'm using NHibernate for ORM, so I'm not interested in only MSSQL servers.
buhtla
A: 

If you're using SQL Server you could use the SQLDMO library, which contains a method that will enumerate available servers on the network.

Phil
A: 

When creating the connection to the server, set the connection timeout to something like 2 or 3 seconds. This way it will bail out much faster if the server isn't actually there.

The only downside is if the server does exist, and it's being hammered, then you might get a false negative. But that's rarely a problem.

Chris Lively
As I have written in my original post, setting connection timeout does help only if server exists. If application has no access to server (for any reason - e.g. invalid server name or firewall), connection timeout has no influence on the duration of connect trying.
buhtla
A: 

At the end, I have decided to take hybrid approach. When user hits "Connect" button, ping will be performed on the server user has entered. If there is no response from the server (which can be detected very fast), user will get the message that server seems to be unaccessible, and a question if he wants to continue and try to connect to that server.

Basically, user will decide wheather he will wait for "attempting to connect".

buhtla