Question

I have a winform app that needs to be aware of when a SQL server is available so as to allow the Merge Syncing functionality. I have tried variations of the below, which work on a one time basis, but I would like to come up with something a little more dynamic.

The below funcion(Thank you Alex_L) is checking for the 1433 port that SQL runs on and works great. What I am trying to accomplish is a way to monitor this in Real-Time. Would I have to create a Windows Service? What about any other network events like the Network Connection changing?

The usage of this sometimes connected app is simple. If they are on our network then the Merge Replication portion is available. If they are not on our network then it is not. The SQL Server should never be available of our network and it SHOULD always be available when on.

That being said, I would be open to solutions involing detecting our current network in lieu of the SQL instance.

     private static bool IsSqlAvailable()
        {
            TcpClient tcpc = new TcpClient();
             try
               {
                 tcpc.Connect("10.250.50.30", 1433);
                 return true;
               }
             catch
               {
                 return false;
               }
        }
Was it helpful?

Solution

You can check port 1433 of SQL Server (or another, if used):

TcpClient tcpc = new TcpClient();
try
{
    tcpc.Connect("10.250.50.30", 1433);
    return true;
}
catch
{
    return false;
}

OTHER TIPS

I have an entry on my blog showing how to detect network availability changes. It uses the NotifyAddrChange windows API to raise a managed event whenever there is a change in the IP interface address mapping table. Since such changes are associated with network availability changes (cable plug in, WiFi join, VPN connect, dial-up, 3G dial-up etc) you can use the event to test for the server availability and start your synchronization.

I would probably go with the 2nd method, adding in checks the various Database properties to make sure it is safe to write to.

http://msdn.microsoft.com/en-us/library/ms176049.aspx

Ok Plan B in response to the comment below: Have your SQL server send some kind of 'Heartbeat' to your main app (Ping or whatever; up to you). You can have your app run a separate thread to watch for that heartbeat every X seconds. If you stop receiving that heartbeat, then you're not connected to your server to run the merge.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top