当前位置:数据库 > SQLServer >>

SQLServer服务管理器

using System;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
using System.ServiceProcess;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace 启动或者停止Windows服务
{
    public partial class Form1 : Form
    {
        string tempServer;
        string tempProcess;
        public Form1()
        {
            InitializeComponent();
            try
            {
                tempServer = System.Configuration.ConfigurationManager.AppSettings["servername"];
                tempProcess = System.Configuration.ConfigurationManager.AppSettings["processname"];//用来从配置文件XML中读取配置数据
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 
        private void btnSartSQLServer_Click(object sender, EventArgs e)//启动服务
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                if (sc.Status.Equals(ServiceControllerStatus.Running))//判断服务是否运行
                {
                    MessageBox.Show("服务正在运行中,不用手动启动", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    label1.Text = "启动中,请稍候......";
                    Thread t = new Thread(Start);//开一个新线程,用来启动服务,这样窗口不会假死
                    t.Start();
                }
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void Start()//启动服务的函数
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                sc.Start();
                string name = sc.ServiceName;//获取服务运行时名称
                Thread.Sleep(4000);//休眠四秒,等待服务启动
                label1.Text = " 服务成功启动";
                Process[] process = Process.GetProcessesByName(tempProcess);//开进程用来获取进程的PID
                Process p = process[0];
                int num = p.Id;
                label2.Text = "服务名为 " + name + " ,PID为" + num;
                MessageBox.Show("启动成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void btnStopSQLServer_Click(object sender, EventArgs e)//停止服务
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                if (sc.Status.Equals(ServiceControllerStatus.Stopped))//判断服务是否停止
                {
                    MessageBox.Show("服务本身停止,不用手动停止", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
             &

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,