You must Sign In to post a response.
  • Category: .NET

    How to start Sql server Service

    How to start all stop Sql Server Service using C#.net.
    I am using below code but not working.

    1.

    private void ReStartService()
    {
    string strService = "MSSQL$SQLEXPRESS";
    ServiceController serv = new ServiceController(strService);
    if (serv != null)
    {
    serv.Stop();
    serv.WaitForStatus(ServiceControllerStatus.Stopped);
    serv.Start();
    serv.WaitForStatus(ServiceControllerStatus.Running);
    }
    }



    2.

    public string RestartService(string serviceName, int timeoutMilliseconds)
    {
    string status= string.Empty;
    ServiceController service = new ServiceController(serviceName);
    try
    {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    //service.Stop();
    //service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    status = service.Status.ToString();
    }
    catch (Exception ex)
    {
    status = ex.Message;
    }
    return status;
    }
  • #763839
    Using 'System.ServiceProcess' namespace you can interact with services.
    look at below code
    string myServiceName = "MSSQL$SQLEXPRESS"; //service name of SQL Server Express
    string status; //service status (For example, Running or Stopped)

    Console.WriteLine("Service: " + myServiceName);

    //display service status: For example, Running, Stopped, or Paused
    ServiceController mySC = new ServiceController(myServiceName);

    try
    {
    status = mySC.Status.ToString();
    }
    catch (Exception ex)
    {
    Console.WriteLine("Service not found. It is probably not installed. [exception=" + ex.Message + "]");
    Console.ReadLine();

    return;

    }

    //display service status: For example, Running, Stopped, or Paused
    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) | mySC.Status.Equals(ServiceControllerStatus.StopPending))
    {
    try
    {
    Console.WriteLine("Starting the service...");
    mySC.Start();
    mySC.WaitForStatus(ServiceControllerStatus.Running);
    Console.WriteLine("The service is now " + mySC.Status.ToString());
    }
    catch (Exception ex)
    {
    Console.WriteLine("Error in starting the service: " + ex.Message);

    }

    }

    Console.WriteLine("Press a key to end the application...");
    Console.ReadLine();

    return;

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763841
    already use but not working

  • #763842
    Handle the logic properly

    if ServiceControllerStatus.Running
    {
    then < stop the service >
    }

    Now you start the service again

    if ServiceControllerStatus.Stopped
    {
    start the service
    }

    By Nathan
    Direction is important than speed

  • #763848
    Hai Gaurav,
    What do you mean by not working? Are you getting any exception or error?
    Make sure that the Visual Studio is in Administrator mode and then try to debug and run it. check for any error and if there is no error, check for the event log to get any warning message.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments