c# 启动关闭sql服务

 static void Main(string[] args)
        {
            ServiceController sc = new ServiceController("MSSQL$SQLEXPRESSR2");
            while (1 == 1)
            {
                Console.Write("\n\n 可选操作 : \n\n 1.启动数据库服务 2.停止数据库服务 \n\n 开始执行 :");
                string strQueryState = Console.ReadLine();
                if (strQueryState.Trim() == "1" && sc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    sc.Start();
                    Console.Write("SQL数据库服务启动成功!");
                }
                else if (strQueryState.Trim() == "2" && !sc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    sc.Stop();
                    Console.Write("SQL数据库服务停止成功!");
                }
                sc.Refresh();
            }
        }

 

posted @ 2017-12-23 17:02  三小  阅读(885)  评论(0编辑  收藏  举报