如何:停止并重新启动Microsoft SQL Server服务到SQL Server .NET的实例 [英] How to: Stop and Restart the Microsoft SQL Server Service to the Instance of SQL Server .NET

查看:99
本文介绍了如何:停止并重新启动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的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆