使用C#的winform控制数据库实例服务的运行状态

一、得到sqlserver的实例名

二、引用对应的程序集和命名空间

using System.ServiceProcess;

C#操作服务要用的类

ServiceController

声明类

cs 复制代码
private ServiceController serviceController = new ServiceController("MSSQLSERVER");

三、判断服务状态

cs 复制代码
 serviceController.Refresh();
 switch (serviceController.Status)
 {
     case ServiceControllerStatus.ContinuePending:
         AddText("正在挂起. . .");
         break;
     case ServiceControllerStatus.Paused:
         AddText("服务已暂停");
         break;
     case ServiceControllerStatus.PausePending:
         AddText("正在暂停. . .");
         break;
     case ServiceControllerStatus.Running:
         serviceStatus = true;
         AddText("正在运行");
         break;
     case ServiceControllerStatus.StartPending:
         AddText("正在启动. . .");
         break;
     case ServiceControllerStatus.Stopped:
         AddText("停止运行");
         serviceStatus = false;
         break;
     case ServiceControllerStatus.StopPending:
         AddText("正在停止. . .");
         break;
     default:
         break;
 }

四、启动服务和停止服务

cs 复制代码
  serviceController.Start();

  serviceController.Stop();
相关推荐
tobebetter95272 小时前
How to manage python versions on windows
开发语言·windows·python
9***P3343 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger3 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz3 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
多多*3 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
p***43483 小时前
Rust网络编程模型
开发语言·网络·rust
ᐇ9594 小时前
Java集合框架深度实战:构建智能教育管理与娱乐系统
java·开发语言·娱乐
梁正雄4 小时前
1、python基础语法
开发语言·python
强化学习与机器人控制仿真5 小时前
RSL-RL:开源人形机器人强化学习控制研究库
开发语言·人工智能·stm32·神经网络·机器人·强化学习·模仿学习
百***48075 小时前
【Golang】slice切片
开发语言·算法·golang