如何使用C#启动所有停止SQL Server服务?
我正在使用此代码,但它无法正常运行并显示以下错误消息:
无法在计算机上打开MSSQL $ SQLEXPRESS服务'。'。
代码:
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;
}
我想在窗口服务中使用SQL Server服务来检查SQL Server是否正在运行。如果它没有运行,那么我想先启动服务器然后再做下一步工作。