C#控制台程序实现开启、关闭SQLServer服务的代码分享

内容摘要
用了近一天的时间研究了C#如何开启SqlServer数据库的服务,起先应用的是C#自带的System.ServiceProcess.ServiceContorller类,但个人认为它在win7下效果不佳,或许由于个人系
文章正文

用了近一天的时间研究了C#如何开启SqlServer数据库的服务,起先应用的是C#自带的System.ServiceProcess.ServiceContorller类,但个人认为它在win7下效果不佳,或许由于个人系统问题,最终决定放弃去选择应用C#的system.diagnostice.process.start方法执行cmd指令,下面我示范开启SqlServer最具代表的两个服务mssqlserver与mssqlserveragent,下面是本人练习写的控制台下的代码,经由多次测试均可成功

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (1 == 1)
            {
                Console.Write('\n\n 可选操作 : \n\n 1.启动数据库服务 2.停止数据库服务 \n\n 开始执行 : ');
                string strQueryState = Console.ReadLine();
                if (strQueryState.Trim() == '1')
                {
                    string strFilePath = GetFilePath('Start');
                    System.IO.File.AppendAllText(strFilePath, GetCmdStr('start'), System.Text.Encoding.GetEncoding('gb2312'));
                    System.Diagnostics.Process.Start(strFilePath);
                }
                else if (strQueryState.Trim() == '2')
                {
                    string strFilePath = GetFilePath('Stop');
                    System.IO.File.AppendAllText(strFilePath, GetCmdStr('stop'), System.Text.Encoding.GetEncoding('gb2312'));
                    System.Diagnostics.Process.Start(strFilePath);
                }
            }

        }

        /// <summary>
        /// 操作所需的Cmd指令
        /// </summary>
        /// <param name='StateStr'>操作</param>
        /// <returns>Cmd指令</returns>
        private static string GetCmdStr(string StateStr)
        {
            string cmdStr = '';
            if (StateStr.ToLower().Trim() == 'start')
            {
                cmdStr = @'@echo. & del %0 & @echo off & @echo.正在启动服务 MSSQLSERVER and MSSQLSERVERAGENT ...... & '
                        + '@echo. & @net start MSSQLSERVER & @net start SQLSERVERAGENT & @echo.服务启动完毕!';
            }
            else if (StateStr.ToLower().Trim() == 'stop')
            {
                cmdStr = @'@echo. & del %0 & @echo off & @echo.正在停止服务 MSSQLSERVER and MSSQLSERVERAGENT ...... & '
                        + '@echo. & @net stop sqlserveragent &  @net stop mssqlserver & @echo.服务停止完毕!';
            }
            return cmdStr;
        }

        /// <summary>
        /// 获取一个文件是否存在于磁盘
        /// </summary>
        /// <param name='PathStr'>文件名称</param>
        /// <returns>返回可创建文件路径</returns>
        private static string GetFilePath(string strFileName)
        {
            string strTem = '';//用于存储要保存文件路径
            string[] strPath = new string[] {@'C:\\',@'D:\\',@'E:\\',@'F:\\'};//检测的盘符
            strFileName += DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
                + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString().Trim()+'.cmd';//为防止重复为文件重新命名

            foreach (string pathStr in strPath)//循环判断文件目录存在性
            {
                if (!System.IO.Directory.Exists(pathStr))//判断目录存在
                {
                    continue;
                }
                else
                {
                    if (System.IO.File.Exists(pathStr + strFileName))//判断文件存在
                    {
                        continue;
                    }
                    else
                    {
                        strTem = pathStr + strFileName;
                        break;
                    }
                }
            }
            return strTem;//返回可建文件路径
        }
    }
}


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!