6

How can I use C# to stop SQL Server?

0

6 Answers 6

11

From http://www.csharp-examples.net/restart-windows-service/

public static void StopService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  }
  catch
  {
    // ...
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - this has the advantage that you know whether it worked or not, and when
5

Execute from a SqlCommand: SHUTDOWN WITH NOWAIT;. The advantages over the other proposed solutions:

  • this requires DB priviledges (sysadmin/serveradmin), not OS privileges
  • works over any network firewall as long as T-SQL is let in (no extra ports needed)
  • you don't need to figure out the service name (MSSQL$instancename)
  • works if the service is started from console (sqlservr -c, not service start)

Comments

3

Try stopping the service.

using System.ServiceProcess;

...

ServiceController controller  = new ServiceController();

controller.MachineName = ".";
controller.ServiceName = "MySqlServerInstance";


controller.Stop();

Comments

3

Have you tried

Process.Start("net stop mssqlserver")

Comments

1

If you don't want to use ServiceController you can kill sqlservr process to kill the right process.

Process.GetProcesses().Where(x => x.ProcessName == "sqlservr").ToList().ForEach(x => x.Kill());

1 Comment

You should add WaitForExit for each process. Code should be like this: Process.GetProcesses().Where(x => x.ProcessName == "sqlservr").ToList().ForEach(x => { x.Kill(); x.WaitForExit(); });
1
using System.Diagnostics;

public static bool StopProcess(string name)
{
    try
    {
        var process = Process.GetProcesses().SingleOrDefault(p => p.ProcessName == name);
        process.Kill()
        process.WaitForExit(); 
        return true;
    }
    catch (Exception)
    {
        //throw;
        return false;
    }
}

6 Comments

I would think unexpected Kill is not a great idea for SQL Server integrity. Are you sure you would do it this way?
@Steve Townsend: if TerminateProcess triggered data corruption SQL Server would be out of business. This is why (almost) all RDBMSs use WAL: msdn.microsoft.com/en-us/library/ms186259.aspx Torn page protection additionally protects against power outages: msdn.microsoft.com/en-us/library/aa337560.aspx
@Remus, you are right but I still would not choose this method when clean service stop is available
@SteveTownsend it has been edited to use WaitForExit(). I guess that should be a more pleasant halt.
SingleOrDefault not found in c# ! is t require another using ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.