Windows Service Dependant on SQL Server

envo

Expert Member
Joined
Jan 14, 2014
Messages
3,265
Reaction score
437
So I know how to get the installer to check for SQL Server dependancy, I'm trying to figure out how to make my service (after it's started) check if sql server is still running.

Or would a windows service that's dependant on sql server stop when the sql server service stops?

This is my code currently:

Code:
Dim blnContinue = False 

        While blnContinue = False
            Try
                Dim conn As New SqlConnection(connStr)
                conn.Open()
                conn.Close()                
                blnContinue = True
            Catch ex As System.Exception
                blnContinue = False
                Thread.Sleep(5000)
            End Try

        End While

...'further down in my code in the main While loop of the thread I check if the write to sql server was successful, if not I wait for a response from sql server before continuing

Is that okay? Or overkill? Or do I approach it differently?

Thanks for any input :)
 
If your code works for you, then use it. Do you constantly need to check if it's up? Like a watchdog service? Personally I'd use threading and events rather than sleeping the main (I assume) thread?

You can also try checking with WMI.

If I remember correctly, making your service dependent on SQL Server will make it stop when SQL Server does.

What?
 
That's not the main thread, just the thread that needs SQL Server. I've modified some of my original code to make it more effecient as well as write to the Event Log etc

What stevenv was referring to was what I was asking soft of. You can make a service dependant on another. So the one won't work without the other. My question was if this will stop my service when the sql service stops too, but after thinking about it, it's better to catch the error as soon as possible and not wait the 20seconds or so it takes for my thread to spool down and end the process
 
Top
Sign up to the MyBroadband newsletter
X