使用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();
相关推荐
冷雨夜中漫步6 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
暖馒8 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
m0_736919108 小时前
C++代码风格检查工具
开发语言·c++·算法
2501_944934738 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
黎雁·泠崖9 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_7634724610 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
TechWJ10 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
lly20240611 小时前
C++ 文件和流
开发语言
m0_7066532311 小时前
分布式系统安全通信
开发语言·c++·算法
寻寻觅觅☆11 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++