views:

181

answers:

3

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;
               }
        }
+1  A: 

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.

Ian Jacobs
How would I use that to implement dynamic checking? That is what I am trying to ask in my question(maybe poorly). I want the program to know when a connection is available or not while running not just on startup.
Refracted Paladin
Thank you for the responses. Is that a viable option with 200 disconnected users? Having our Production Server pinging 200 users constatly? Also, we use Dynamic DNS so the IP's change all the time though I suppose you can ping by HOSTNAME. I just can't believe there isn't a way for a local system to monitor a connection? Even if it is just to see if they are currently on our network. There must be an event raised when the connection state changes, I would have thought.
Refracted Paladin
As far as I know the only way anything internal checks to see if a connection is good is by pinging it and checking to see what happens. After that any events may be raised. Also, I'm not saying your production server should make 200 individual pings to each client machine. I'm thinking your server should make a single broadcast on an arbitrary port (54321 for example). Design your worker thread to connect to your server on port 54321. Then every connected client will see it in one shot.
Ian Jacobs
I see what you are saying. Thanks
Refracted Paladin
+2  A: 

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;
}
Alex_L
Thank you, I didn't know that. There is no way though to monitor this in real time? I would have to check this periodically, manually, in order to 'mimick' real time?
Refracted Paladin
As I think, simpliest way (and as I do in my projects) is to write the windows service. This little service checks availability of SQL Servers (using Timer class) and sends the messages to appropriate admins.
Alex_L
+1  A: 

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.

Remus Rusanu