How can I use C# to stop SQL Server?
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
{
// ...
}
}
Execute from a SqlCommand: SHUTDOWN WITH NOWAIT;
. The advantages over the other proposed solutions:
sqlservr -c
, not service start)Try stopping the service.
using System.ServiceProcess;
...
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "MySqlServerInstance";
controller.Stop();
Have you tried
Process.Start("net stop mssqlserver")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With