C# 启动停止SQLServer数据库服务器

C#启动停止SQL数据库服务方法之一:

在命令行里填写命令:net start/stop mssqlserver

C#启动停止SQL数据库服务方法之二:

通过C#代码实现;

class Program : Form
{
public Program()
{
Button btn1 = new Button();
btn1.Parent = this;
btn1.Text = "启动 SQL SERVER";
btn1.Tag = "START";
btn1.Top = 50;
btn1.Left = 20;
btn1.Width = 120;
btn1.Click += new EventHandler(Button_Click);

Label la = new Label();
la.Text = "C#启动SQL服务";
la.ForeColor = System.Drawing.Color.Green;
la.Left = 100;
la.Top = 20;
la.Parent = this;

Button btn2 = new Button();
btn2.Parent = this;
btn2.Text = "停止 SQL SERVER";
btn2.Tag = "STOP";
btn2.Top = 50;
btn2.Left = btn1.Right + 10;
btn2.Width = 120;
btn2.Click += new EventHandler(Button_Click);

this.Text = "C#启动SQL服务";
}

void Button_Click(object sender, EventArgs e)
{
string s = (sender as Button).Tag.ToString();
ServiceController sc = new ServiceController("MSSQLSERVER");
if (s == "START" && sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
MessageBox.Show("SQL数据库服务启动成功!", "提示信息");
}
if (s == "STOP" && !sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Stop();
MessageBox.Show("SQL数据库服务已经关闭!", "提示信息");
}
}

static void Main(string[] args)
{
Application.Run(new Program());
}
}

上一篇:【一起来烧脑】一步Sass学会体系


下一篇:项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案