使用 WMI 提供程序管理服务和网络设置

WMI 提供程序是可供 Microsoft 管理控制台 (MMC) 用来管理 SQL Server 服务和网络协议的已发布接口。在 SMO 中,ManagedComputer 对象表示 WMI 提供程序。

ManagedComputer 对象的运行与通过 Server 对象建立的与 SQL Server 实例的连接无关,而是使用 Windows 凭据连接到 WMI 服务。

示例

若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。 有关详细信息,请参阅 SQL Server 联机丛书中的“How to: Create a Visual Basic SMO Project in Visual Studio .NET”或“How to: Create a Visual C# SMO Project in Visual Studio .NET”。

对于使用 SQL Server WMI 提供程序的程序来说,必须包括 Imports 语句才能限定 WMI 命名空间。请在应用程序的其他 Imports 语句之后、任何声明之前插入该语句,例如:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Wmi

在 Visual Basic 中停止并重新启动 SQL Server 实例的 Microsoft SQL Server 服务

该代码示例演示了如何通过使用 SMO ManagedComputer 对象停止和启动服务。该示例为用于配置管理的 WMI 提供程序提供了接口。

'Declare and create an instance of the ManagedComputer object that represents the WMI Provider services.
Dim mc As ManagedComputer
mc = New ManagedComputer()
'Iterate through each service registered with the WMI Provider.
Dim svc As Service
For Each svc In mc.Services
    Console.WriteLine(svc.Name)
Next
'Reference the Microsoft SQL Server service.
svc = mc.Services("MSSQLSERVER")
'Stop the service if it is running and report on the status continuously until it has stopped.
If svc.ServiceState = ServiceState.Running Then
    svc.Stop()

    Console.WriteLine(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
    Do Until String.Format("{0}", svc.ServiceState) = "Stopped"
        Console.WriteLine(String.Format("{0}", svc.ServiceState))
        svc.Refresh()
    Loop
    Console.WriteLine(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))
    'Start the service and report on the status continuously until it has started.
    svc.Start()
    Do Until String.Format("{0}", svc.ServiceState) = "Running"
        Console.WriteLine(String.Format("{0}", svc.ServiceState))
        svc.Refresh()
    Loop
    Console.WriteLine(String.Format("{0} service state is {1}", svc.Name, svc.ServiceState))

Else
    Console.WriteLine("SQL Server service is not running.")
End If

在 Visual Basic 中使用 URN 字符串启用服务器协议

此代码示例演示如何使用 URN 对象标识服务器协议并启用该协议。

'Declare the ManagedComputer WMI interface.
Dim mc As New ManagedComputer()
'Create a URN object that represents the TCP server protocol.
Dim u As New Urn("ManagedComputer[@Name=MYPC']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
Declare the serverProtocol variable and return the ServerProtocol object.
Dim sp As ServerProtocol
sp = mc.GetSmoObject(u)
'Enable the protocol.
sp.IsEnabled = True
sp.Alter()

在 Visual C# 中使用 URN 字符串启用服务器协议

此代码示例演示如何使用 URN 对象标识服务器协议并启用该协议。

{ 
   //Declare and create an instance of the ManagedComputer 
   //object that represents the WMI Provider services. 
   ManagedComputer mc; 
   mc = new ManagedComputer(); 
   //Iterate through each service registered with the WMI Provider. 
   Service svc; 
   foreach ( svc in mc.Services) { 
      Console.WriteLine(svc.Name); 
   } 
//Reference the Microsoft SQL Server service. 
   svc = mc.Services("MSSQLSERVER"); 
//Stop the service if it is running and report on the status
// continuously until it has stopped. 
   if (svc.ServiceState == ServiceState.Running) { 
      svc.Stop(); 
      Console.WriteLine(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState)); 
      while (!(string.Format("{0}", svc.ServiceState) == "Stopped")) { 
         Console.WriteLine(string.Format("{0}", svc.ServiceState)); 
          svc.Refresh(); 
      } 
      Console.WriteLine(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState)); 
//Start the service and report on the status continuously 
//until it has started. 
      svc.Start(); 
      while (!(string.Format("{0}", svc.ServiceState) == "Running")) { 
         Console.WriteLine(string.Format("{0}", svc.ServiceState)); 
         svc.Refresh(); 
      } 
      Console.WriteLine(string.Format("{0} service state is {1}", svc.Name, svc.ServiceState)); 
   } 
   else { 
      Console.WriteLine("SQL Server service is not running."); 
   } 
}