欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 09:44:22
用了近一天的时间研究了c#如何开启sqlserver数据库的服务,起先应用的是c#自带的system.serviceprocess.servicecontorller类,但...

用了近一天的时间研究了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;//返回可建文件路径
        }
    }
}