[原]C#开启停止SqlServer服务
using System.Diagnostics;
using System.ServiceProcess;
using System.ServiceProcess;
/// <summary>
/// 程序载入时的状态,Author:Kennytian@msn.com
/// </summary>
private void LoatStatus()
{
ServiceController sc = new ServiceController("MSSQLSERVER");
string status = sc.Status.ToString();
sc.Close();
label1.Text = "Current status is:" + status;
if (status.Equals("Running"))
StartSql.Text = "Stop";
else
StartSql.Text = "Start";
}
/// 程序载入时的状态,Author:Kennytian@msn.com
/// </summary>
private void LoatStatus()
{
ServiceController sc = new ServiceController("MSSQLSERVER");
string status = sc.Status.ToString();
sc.Close();
label1.Text = "Current status is:" + status;
if (status.Equals("Running"))
StartSql.Text = "Stop";
else
StartSql.Text = "Start";
}
/// <summary>
/// 开启停止SqlServer服务,Author:Kennytian@msn.com
/// </summary>
/// <param name="text">按钮上的字</param>
private void StartOrStopSqlServerService(string text)
{
ServiceController sc = new ServiceController("MSSQLSERVER");
bool startText = text.Equals("Start");
bool stopText = text.Equals("Stop");
bool Runned = sc.Status.ToString().Equals("Running");
if (stopText && Runned)
{
label1.Text = "Current status is:Stopped";
sc.Stop();
StartSql.Text = "Start";
}
else if (startText && !Runned)
{
label1.Text = "Current status is:Running";
sc.Start();
StartSql.Text = "Stop";
}
else
{
MessageBox.Show("出现异常,请联系开发人员!", "警告", MessageBoxButtons.OK);
}
sc.Close();
}
/// 开启停止SqlServer服务,Author:Kennytian@msn.com
/// </summary>
/// <param name="text">按钮上的字</param>
private void StartOrStopSqlServerService(string text)
{
ServiceController sc = new ServiceController("MSSQLSERVER");
bool startText = text.Equals("Start");
bool stopText = text.Equals("Stop");
bool Runned = sc.Status.ToString().Equals("Running");
if (stopText && Runned)
{
label1.Text = "Current status is:Stopped";
sc.Stop();
StartSql.Text = "Start";
}
else if (startText && !Runned)
{
label1.Text = "Current status is:Running";
sc.Start();
StartSql.Text = "Stop";
}
else
{
MessageBox.Show("出现异常,请联系开发人员!", "警告", MessageBoxButtons.OK);
}
sc.Close();
}
注:StartSql是一个按钮,添加一个引用ServiceProcess