C console program to open and close the SQLServer service code sharing
- 2020-06-23 01:44:28
- OfStack
Took nearly C # 1 day time studied how to unlock SqlServer database service, the first application is the System C #. The ServiceProcess. ServiceContorller classes, but personally think it under win7 the effect not beautiful, perhaps because of personal system, finally decided to give up to choose C # system. diagnostice. process. start method to execute commands cmd, below I open SqlServer demonstration the two most representative service mssqlserver and mssqlserveragent, here is my practice writing control The code produced under the platform can be successfully tested for many times
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 Optional operation : \n\n 1. Start the database service 2. Stop database service \n\n Start to perform : ');
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>
/// Operationally required Cmd instruction
/// </summary>
/// <param name='StateStr'> operation </param>
/// <returns>Cmd instruction </returns>
private static string GetCmdStr(string StateStr)
{
string cmdStr = '';
if (StateStr.ToLower().Trim() == 'start')
{
cmdStr = @'@echo. & del %0 & @echo off & @echo. Starting a service MSSQLSERVER and MSSQLSERVERAGENT ...... & '
+ '@echo. & @net start MSSQLSERVER & @net start SQLSERVERAGENT & @echo. Service started !';
}
else if (StateStr.ToLower().Trim() == 'stop')
{
cmdStr = @'@echo. & del %0 & @echo off & @echo. Out of service MSSQLSERVER and MSSQLSERVERAGENT ...... & '
+ '@echo. & @net stop sqlserveragent & @net stop mssqlserver & @echo. Service stop complete !';
}
return cmdStr;
}
/// <summary>
/// To obtain 1 Whether the file exists on disk
/// </summary>
/// <param name='PathStr'> The file name </param>
/// <returns> Returns the path to the creatable file </returns>
private static string GetFilePath(string strFileName)
{
string strTem = '';// Used to store the path to the saved file
string[] strPath = new string[] {@'C:\\',@'D:\\',@'E:\\',@'F:\\'};// The disk symbol detected
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';// Rename the file to prevent duplication
foreach (string pathStr in strPath)// Loop to determine file directory existence
{
if (!System.IO.Directory.Exists(pathStr))// Determine directory existence
{
continue;
}
else
{
if (System.IO.File.Exists(pathStr + strFileName))// Determine the existence of file
{
continue;
}
else
{
strTem = pathStr + strFileName;
break;
}
}
}
return strTem;// Returns the buildable file path
}
}
}