SQL Server on C# question

Hi,

I want to be able to run, pause or stop a SQL Service (as in SQL Server Service Manager) from within a C# application. How can I do this

The server I'm using is called DEANHPLAPTOP. I'm sure this can be done through the command line somehow in DOS but how can I do it in an application

The end result I want is to have it so that the first form in the app (login form) starts the service if it's not already running and then stops it when the application is closed.


Answer this question

SQL Server on C# question

  • odrejesus

    Thanks, I'll check that out soon. Smile

  • JMCD73

    So far I've got:

                // Determine if SQL Server is running
                ServiceController serviceControllerSQL = new ServiceController("MSSQLSERVER");

                if (serviceControllerSQL.Status != ServiceControllerStatus.Running |
                    serviceControllerSQL.Status == ServiceControllerStatus.Stopped |
                    serviceControllerSQL.Status == ServiceControllerStatus.Paused)
                {
                    // If SQL Server 2000 3a is not running, paused or stopped start the service
                    serviceControllerSQL.Start();
                }

    I'd like to now do all of this checking before each query is run on the database. Do I really need to do this I also want to run the SQL Server Service check everytime anything is done on a form (is this done by using the validate method ).

  • Evan2

    You don't want to perform this check before each query, do you This is extremely expensive, a SQL database server should be available whenever the clients need it to be available, it's not up to the end-user to stop or start the service anyway, they shouldn't even be allowed to do so.

    Willy.


  • emirac

     Dean Krause wrote:
    Thanks for the advice so far... Smile

    I have now got the reference to System.ServiceProcess.dll thanks that WAS obvious LOL...

    I have now got :
    "
    ServiceController serviceController = new ServiceController("MSSQLSERVER");
    serviceController.Start();
    serviceController.Pause();
    serviceController.Stop();
    "

    How can I do an if statement  e.g.

    if (
    serviceController is stopped)
    { START IT }

    if(serviceController is paused)
    { START IT }

    if (serviceController is running)
    { STOP IT }

    In my application i have a login form I have alredy designed and got working fine. Before I run each SQL query I have to determine if the service is running and my DB  exists. How can I do this.

    Also how can I get the state of the service E.g. Stopped, Running, Paused.
    Idea

    Dean,

       You can use the Status property on the ServiceController instance to figure out whether the service is started, stopped, etc, etc.

       Hope this helps.

              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard.caspershouse.com

  • Parikshit A. Bhinde

    Just a thought... Smile

    Thanks for your help.

  • BenWillett

    Whenever I use the System.ServiceProcess.ServiceController it shows this error.

    The type or namespace name 'ServiceController' does not exist in the namespace 'System.ServiceProcess' (are you missing an assembly reference )           


  • TITRA FILM

    Use System.ServiceProcess.ServiceController, create a new instance and set the ServiceName property to the short name of the service (viewable in services.msc).

    You can then call .Start() and .Stop() on that object.

    Incidentally in cmd, it's net start <shortservicename> and net stop <shortservicename>.

  • ruiefree

    Thanks for the advice so far... Smile

    I have now got the reference to System.ServiceProcess.dll thanks that WAS obvious LOL...

    I have now got :
    "
    ServiceController serviceController = new ServiceController("MSSQLSERVER");
    serviceController.Start();
    serviceController.Pause();
    serviceController.Stop();
    "

    How can I do an if statement  e.g.

    if (
    serviceController is stopped)
    { START IT }

    if(serviceController is paused)
    { START IT }

    if (serviceController is running)
    { STOP IT }

    In my application i have a login form I have alredy designed and got working fine. Before I run each SQL query I have to determine if the service is running and my DB  exists. How can I do this.

    Also how can I get the state of the service E.g. Stopped, Running, Paused.
    Idea

  • xizzy44

    Are you missing a reference to System.ServiceProcess.dll Idea

    Regards,

    DeepakKapoor.NET



  • SQL Server on C# question