How to start SQLEXPRESS 2016 service from console application in C #?

0

I have the following code of a console application in C # to start the SQLSERVER 2016 service, but I could not, here is the code and the code error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
namespace ConsoleApplication1
{
   class Program
   {
    static void Main(string[] args)
    {
        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;

    }
  }

}

and here is the result of the application.

Do you have any idea why I can not start that service? thanks in advance

    
asked by Luis Fernando 25.08.2017 в 03:36
source

2 answers

1

Everything was about permissions, I had to add a manifest file in my application so that the administrator ejecura, I leave some images for you to see.

And I had to change this line

<requestedexecutionlevel level="requireAdministrator" uiaccess="false">
</requestedexecutionlevel>

now the application does not throw an exception if the SQL service is stopped.

    
answered by 25.08.2017 / 05:05
source
0

It may be a permissions error, because to execute the SQL service your application must be running with elevation and as administrator, you can test it by executing the following command from the Windows command prompt:

sc start MSSQL$SQLEXPRESS

If the result is " Access Denied ", it is for permissions, in which case compile your application and try running the EXE as an administrator and it should work (right click "Run as administrator").

It may also be that the name of your instance is not correct, in which case the first command will send you a result like this:

  

[SC] StartService: OpenService ERROR 1060:

     

The specified service does not exist as an installed service.

You can check the name of your SQL instance using the following command:

sc query | findstr /I MSSQL
    
answered by 25.08.2017 в 04:12