none
Starting and Stoping SQL services from c# RRS feed

  • Question

  • Hi 

        i have 17  SQL instances. i creating a software to Backup MDF and LDF every night. for this i need to start and stop SQL services automatocally. or on a Button click. how i mention the Instance name in c# . if i select server A - automatically the service should stop, if i select server b automatically the service of server b should stop.  can anybody help me on this .


    maxwin

    Tuesday, June 13, 2017 10:03 AM

Answers

  • Hi all 

             for doing this I have created a table in one of my database( not on production database) . and put  the Name of service in that particular Table. And i called that name from .net. 

    Below is the code that i used 

     variable servicename= SQL service from table

       private void Startservice()
            {

                ServiceController service = new ServiceController(Servicename);
                TimeSpan timeout = TimeSpan.FromSeconds(30);
                if (service.Status != ServiceControllerStatus.Running)
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);

                }
                else { 
                
                }
            
            }


            private void StopService()
            {
                ServiceController service = new ServiceController(Servicename);
                TimeSpan timeout = TimeSpan.FromSeconds(5);
                if (service.Status != ServiceControllerStatus.Stopped)
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                }

                else { 
                }


    maxwin

    Thursday, June 15, 2017 3:27 AM

All replies

  • Antony,

    You're for sure not the first one who tries this with C#, it means you should create a Windows Service which is running on the SQL service all the time. And what you have then, I service which by using some powershell instructions is stopping the SQL service, it would be a hard job to get the correct user to do this as SQL has if it is well its own administrator who's authentication you don't want so easy to be seen. 

    There are better ways to do this. A simple search on Internet gives many answers. 

    https://www.google.com/?hl=en&gws_rd=cr#hl=en&q=how+to+stop+an+sql+server+before+backup


    Success
    Cor

    Tuesday, June 13, 2017 10:22 AM
  • Years ago I wrote a script that might help; see Starting and Stopping a Service. The script uses WMI. In .Net we have the System.Management Namespace for WMI. So probably you can use the script as a sample and convert it to C#. You should be able to find other samples by searching.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, June 13, 2017 4:51 PM
  • Hi all 

             for doing this I have created a table in one of my database( not on production database) . and put  the Name of service in that particular Table. And i called that name from .net. 

    Below is the code that i used 

     variable servicename= SQL service from table

       private void Startservice()
            {

                ServiceController service = new ServiceController(Servicename);
                TimeSpan timeout = TimeSpan.FromSeconds(30);
                if (service.Status != ServiceControllerStatus.Running)
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running);

                }
                else { 
                
                }
            
            }


            private void StopService()
            {
                ServiceController service = new ServiceController(Servicename);
                TimeSpan timeout = TimeSpan.FromSeconds(5);
                if (service.Status != ServiceControllerStatus.Stopped)
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped);
                }

                else { 
                }


    maxwin

    Thursday, June 15, 2017 3:27 AM
  • Hi maxwin,

    Thank you for your update.

    Does the code you pasted solved this issue? If so, you could mark your reply as answer, It will benefit others who faced similar issue.

    Or if this problem persists, could you please specify more about this issue? It will help for further troubleshooting.

    Best Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 15, 2017 5:59 AM
    Moderator
  • Does the code you pasted solved this issue? If so, you could mark your reply as answer, It will benefit others who faced similar issue.

    Except if it is based on my response and sample then I think it deserves something.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, June 15, 2017 6:03 AM