Aracılığıyla paylaş


WMI sağlayıcı'nı kullanarak, hizmetleri ve ağ ayarları yönetme

WMI sağlayıcı tarafından kullanılan bir yayımlanmış arabirim Microsoft Yönetim Konsolu'nu (yönetmek için MMC) SQL Server Hizmet ve ağ iletişim kuralları. Içinde SMO, ManagedComputer Nesne, WMI sağlayıcı temsil eder.

The ManagedComputer object operates independently of the connection established with the Server object to an örnek of SQL Server, and uses Windows kimlik bilgileri to connect to the WMI hizmet.

Örnek

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.

Için kullanan programlar SQL Server WMI sağlayıcı, içermeli Alır ekstresi WMI ad nitelendirin.Sonra diğer deyim ekleyin Imports uygulamada, tüm bildirimlerden önce ifadeleri, örneğin:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Wmi

Durdurma ve SQL Server örnek için Microsoft SQL Sunucu hizmet, Visual Basic'te yeniden başlatılıyor

Bu kod örneği, SMO'ni kullanarak hizmetleri başlatmak gösterilmiştir ManagedComputer nesne. Bu arabirim için WMI sağlayıcı, yapılandırma yönetimi için sağlar.

Bir sunucu Visual Basic'te URN bir dize kullanarak protokol'nı etkinleştirme

Kod örneği, bir URN nesnesini kullanarak bir sunucu protokol tanımlamak ve sonra iletişim kuralını etkinleştirmeniz gösterilmiştir.

'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# [NULL]'URN bir dize kullanarak özel bir sunucu protokolü etkinleştirme

Kod örneği, bir URN nesnesini kullanarak bir sunucu protokol tanımlamak ve sonra iletişim kuralını etkinleştirmeniz gösterilmiştir.

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