Zarządzanie ustawienia sieci i usług za korzystanie z dostawca WMI

Dostawca WMI jest opublikowanego interfejs, który jest używany przez Microsoft Zarządzanie Console (MMC) do zarządzania SQL Server usługi i protokoły sieciowe. W przypadku obiektów SMO ManagedComputer obiekt reprezentuje dostawca WMI.

The ManagedComputer object operates independently of the connection established with the Server object to an wystąpienie of SQL Server, and uses Windows poświadczenia to connect to the WMI usługa.

Przykład

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see "How to: Create a Visual Basic SMO Project in Visual Studio .NET" or "How to: Create a Visual C# SMO Project in Visual Studio .NET" in SQL Server Books Online.

Programy, dla których używane są SQL Server Dostawca WMI, należy dołączyć Przywóz instrukcja, aby móc skorzystać z obszaru nazw WMI.Wstawić instrukcję po drugim Imports instrukcje przed wszelkimi deklaracjami w aplikacji, takich jak:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Wmi

Zatrzymywanie i ponowne uruchamianie usługa Microsoft SQL Server w wystąpieniu programu SQL Server w języku Visual Basic

W tym przykładzie kodu pokazano, jak zatrzymywania i uruchamiania usług przy użyciu obiektów SMO ManagedComputer obiekt. Ten interfejs do dostawca WMI konfiguracja zarządzania.

Włączenie protokół Server za pomocą ciąg nazwy URN w języku Visual Basic

W przykładzie kodu pokazano, jak zidentyfikować protokół serwera za pomocą obiektu nazwy URN, a następnie włączyć protokół.

'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()

Włączenie protokół Server za pomocą ciąg nazwy URN w środowisku Visual C#

W przykładzie kodu pokazano, jak zidentyfikować protokół serwera za pomocą obiektu nazwy URN, a następnie włączyć protokół.

{ 
   //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."); 
   } 
}