guyl
guyl

Reputation: 2242

stop / start service or close the sql server process / service on a remote computer

I am trying to shutdown and start the sql server on a remote computer (on the same network), i have used this code

ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;

ManagementScope scope = 
         new ManagementScope(
                       string.Format(@"\\{0}\root\cimv2", serverFullName),
                       options);
scope.Connect();

ObjectQuery query = new ObjectQuery(
                        string.Format(@"SELECT * FROM Win32_Process WHERE Name='{0}'",
                                      processToTerminate));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject m in queryCollection)
{
     m.InvokeMethod("Terminate", null);
}
  1. is there another way of doing that ?
  2. how can i start the process (if Terminate close it)?

Thank you

Upvotes: 4

Views: 5820

Answers (4)

Jenish Zinzuvadiya
Jenish Zinzuvadiya

Reputation: 1085

you can do some like this to start and stop your sql server

System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.FileName = "net start \"Sql Server (SQLEXPRESS)\"";
    process.Start();

Upvotes: 0

Pilgerstorfer Franz
Pilgerstorfer Franz

Reputation: 8359

What about using the ServiceController class? (see MSDN)

// just replace the params with your values
ServiceController sc = new ServiceController("SERVICENAME", "MACHINENAME");
if (sc.Status.Equals(ServiceControllerStatus.Running))
    sc.Stop();

This should do the trick.

hth

Upvotes: 5

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239664

It would seem a lot easier to just use the ServiceController class, that you can give a service name and computer name to, and then call methods such as Start and Stop.

Upvotes: 4

vcsjones
vcsjones

Reputation: 141638

Killing a process and stoping a service are two different things. A service could spawn other processes that will remain lingering. Also, you are effectively pulling the plug on the process. It isn't being given any time to stop gracefully, write everything to disk, etc.

Instead you should use the Win32_Service WMI object to find your service. This has a StartService and StopService method, which will allow you to stop and start it as you need.

Mind you, this WMI object is about services, not processes, so you will have to tweak your code to stop it by the service name, not the process name. Something like this:

ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;

ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverFullName), options);
scope.Connect();

ObjectQuery query = new ObjectQuery(string.Format(@"SELECT * FROM Win32_Service WHERE Name='{0}'",serviceToStop));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject m in queryCollection)
{
   m.InvokeMethod("StopService", null);
}

Then later on you can use InvokeMethod on StartService.

Upvotes: 3

Related Questions