C#中怎么启动 SQL Server 服务

C#中怎么启动 SQL Server 服务,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

实例如下:

//首先要添加 System.ServiceProcess.dll 引用
   ServiceController sc = new ServiceController("MSSQLSERVER");

   //判断服务是否已经关闭
   if (sc.Status == ServiceControllerStatus.Stopped)
   {
    sc.Start();
    MessageBox.Show("SQL数据库服务启动成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }

   //判断服务是否已经开启
   if (sc.Status != ServiceControllerStatus.Stopped)
   {
    sc.Stop();
    MessageBox.Show("SQL数据库服务成功关闭!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }

看完上述内容,你们掌握C#中怎么启动 SQL Server 服务的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!