Click here to Skip to main content
15,913,098 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
Hi all visitors
i want to know my sql server service start or not in my pc.
In my pc, i have installed 2 sql server version (sql server 2005 with instance name="Admin" and sql server 2008 with instance name="sql08server")
for my database, i created in sql server 2008.
If i want to know my sql server 2008 service start or stop how i can know about this.

This is my code in vb.net
Private Sub StartStop()

        'Dim server As System.ServiceProcess.ServiceController
        Dim service As ServiceController = New ServiceController("SQL08SERVER")

        If ((service.Status.Equals(ServiceControllerStatus.Stopped)) Or (service.Status.Equals(ServiceControllerStatus.StopPending))) Then

            'service.Start()
            MsgBox("Stop")

        Else

            'service.Stop()
            MsgBox("Start")

        End If

    End Sub


After i run, it display a error message
Service SQL08SERVER was not found on computer '.'.

Please help me how can i control my sql server 2008 Service start or stop?

Best Regards,
Posted
Updated 8-Dec-17 8:26am
Comments
walterhevedeich 22-Jul-11 1:14am    
The error is pretty clear. What you need to do now is to verify if it really exist. Go to Control Panel->Administrative Tools->Services and verify if that is really the name of the service.
Suresh Suthar 22-Jul-11 1:26am    
Go to SQL Server Configuration Manager and check the service name. Or you can use Services.msc to check the service name.
soeun tony 22-Jul-11 21:12pm    
Thanks for your idea,
For that idea, it is very good and i'll follow with your idea.
but for example, the user is using my application than the sql server service is stop by itself. in that case, i want to show message to user that "Service of Server has problem" to let the user know that the server has problem than they'll go to service.msc to start it again.

Best Regards,

I told you not to do this for the very reason you're looking at right now. Also, YOU may have the permissions to see the service state, but your users probably won't, so this code isn't going to work when they run your app.
 
Share this answer
 
v2
Comments
soeun tony 22-Jul-11 21:13pm    
Thanks for you reply
my user is Built-in User (Administrator).
Best Regards,
Dave Kreskowiak 22-Jul-11 23:30pm    
Yeah, right. That just says "disaster waiting to happen".
I'm with Dave. Your code should just try to connect to the server and deal gracefully with any exceptions thrown. What you're trying to do is - well - stupid.
 
Share this answer
 
v2
Comments
soeun tony 22-Jul-11 21:14pm    
Thanks for your reply
Yes of course my code is no exceptions because i want to test to see the result.later i'll use exception. it is not difficult to use exception.
Thanks
Click Start --> Run and type services.msc, search your SQL Server 2008 service, right click it and click Properties. In the Properties dialog, you will see the actual service name next to the 'Service Name:' label. Use that name in the code.
 
Share this answer
 
Comments
soeun tony 22-Jul-11 21:17pm    
Thanks for your reply,
For this way, it is used for start or stop service. but my purpose, when the service stop, i want to display message to let user to know that "Server has been stopped". this is my purpose (Just want to display message to let user to know only).
Best Regards,
[no name] 23-Jul-11 1:28am    
Read my answer properly, all I said was to find out the correct service name and then use that in your statement like this:
Dim service As ServiceController = New ServiceController("MSSQL$SQL08SERVER")
soeun tony 25-Jul-11 21:42pm    
Thanks for your reply
This is what i want to know.
Thank you so much for your help
VB
Imports System
Imports System.ServiceProcess

Module Module1

    Sub Main()

        Dim myServiceName As String = "MSSQL$SQLEXPRESS" 'service name of SQL Server Express
        Dim status As String  'service status (For example, Running or Stopped)
        Dim mySC As ServiceController

        Console.WriteLine("Service: " & myServiceName)

        'display service status: For example, Running, Stopped, or Paused
        mySC = New ServiceController(myServiceName)
        Try
            status = mySC.Status.ToString
        Catch ex As Exception
            Console.WriteLine("Service not found. It is probably not installed. [exception=" & ex.Message & "]")
            Console.ReadLine()
            End
        End Try
        Console.WriteLine("Service status : " & status)

        'if service is Stopped or StopPending, you can run it with the following code.
        If mySC.Status.Equals(ServiceControllerStatus.Stopped) Or mySC.Status.Equals(ServiceControllerStatus.StopPending) Then
            Try
                Console.WriteLine("Starting the service...")
                mySC.Start()
                mySC.WaitForStatus(ServiceControllerStatus.Running)
                Console.WriteLine("The service is now " & mySC.Status.ToString)

            Catch ex As Exception
                Console.WriteLine("Error in starting the service: " & ex.Message)
            End Try
        End If

        Console.WriteLine("Press a key to end the application...")
        Console.ReadLine()
        End
    End Sub
End Module
 
Share this answer
 
v2
Comments
Richard Deeming 8-Dec-17 15:37pm    
Asked and answered SIX AND A HALF YEARS AGO. And your solution adds nothing to the existing answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900