如何:停止并重新启动Microsoft SQL Server服务到SQL Server .NET的实例

编程入门 行业动态 更新时间:2024-10-26 10:33:17
本文介绍了如何:停止并重新启动Microsoft SQL Server服务到SQL Server .NET的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从visual studio 2012重新启动SQL Server 2012.根据MSDN,我定义了一个类并在其上实现了此代码。但它不起作用。事实上,该程序陷入循环。

I want to restart SQL server 2012 from visual studio 2012. According to MSDN I define a class and implemented this code on it. But it doesn''t work. In fact, the program entrap on loop.

Microsoft.SqlServer.ConnectionInfo.dll Microsoft.SqlServer.Smo.dll Microsoft.SqlServer.SqlEnum.dll Microsoft.SqlServer.Smo.WmiEnum Microsoft.SqlServer.Management.Sdk.Sfc.dll public static class SQLServer { public static void Restart() { ManagedComputer mc = new ManagedComputer(); Service svc = new Service(); svc = mc.Services["MSSQLSERVER"]; if (svc.ServiceState== ServiceState.Running) { svc.Stop(); } while (svc.ServiceState== ServiceState.Running ) { svc.Refresh(); } svc.Start(); } }

推荐答案

使用以下方法处理C#中的服务 Use the following Method for handling Services in C# using System.ServiceProcess; public static class SQLServer { public static void Restart() { string strSVCName = "MSSQLSERVER"; ServiceController mySC = new ServiceController(strSVCName); if (mySC.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { mySC.Refresh(); } else if (mySC.Status == System.ServiceProcess.ServiceControllerStatus.Running) { mySC.Refresh(); } mySC.Start(); } }

希望这会对你有所帮助。

Hope this helps you.

更多推荐

如何:停止并重新启动Microsoft SQL Server服务到SQL Server .NET的实例

本文发布于:2023-11-08 11:01:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重新启动   实例   Microsoft   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!