zoukankan      html  css  js  c++  java
  • 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());
    }
    }

  • 相关阅读:
    Asp.Net Web API 2第八课——Web API 2中的属性路由
    Asp.Net Web API 2第七课——Web API异常处理
    Asp.Net Web API 2第六课——Web API路由和动作选择
    Asp.Net Web API 2第五课——Web API路由
    开始学习python
    BMI 小程序 购物车
    深浅copy 文件操作
    字典 dict 集合set
    基本数据类型 (str,int,bool,tuple,)
    python 运算符
  • 原文地址:https://www.cnblogs.com/wlming/p/5113494.html
Copyright © 2011-2022 走看看