日期:2014-05-18  浏览次数:20867 次

怎样才能知道SQL服务是否有启动?
我是指在连接服务器之前知道它的状态,如果要连接的话要等太久才能知道.

------解决方案--------------------
Process.GetProcesses获取一下进程有没有运行SQL
------解决方案--------------------
获取状态也要链接一下才知道啊。
我一般就抓异常了
SqlConnection con=new SqlConnection(strCon);
try
{
con.Open();
}
catch
{
MessageBox.Show( "Sql Server stopped now! ");
}
不过也看到有人使用sc的,参考一下。
using System.ServiceProcess;
...
private void button1_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController( "MSSQLSERVER ", "sunny ");
if (sc.Status == ServiceControllerStatus.Running)
{
MessageBox.Show( "Running ");
}
else if (sc.Status == ServiceControllerStatus.Paused)
{
MessageBox.Show( "Paused ");
}
else if (sc.Status == ServiceControllerStatus.Stopped)
{
MessageBox.Show( "Stopped ");
}
}