• 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());
    }
    }

  • 相关阅读:
    指针类型
    集合类型
    VMware打开虚拟机没反应的解决方案(全面汇总)
    redis主从|哨兵|集群模式
    MYSQL一次千万级连表查询优化
    StackExchange.Redis通用封装类分享
    Redis-五种数据类型解析
    Redis并发问题
    C#委托和事件
    Invoke 和 BeginInvoke 的区别
  • 原文地址:https://www.cnblogs.com/wlming/p/5113494.html
Copyright © 2020-2023  润新知