使用 WMI 提供者管理服務和網路設定
WMI 提供者是 Microsoft Management Console (MMC) 用於管理 SQL Server 服務和網路通訊協定的已發行介面。在 SMO 中,ManagedComputer 物件表示 WMI 提供者。
ManagedComputer 物件可獨立操作,不受使用 Server 物件對 SQL Server 執行個體所建立連接的影響,並會使用 Windows 認證連接到 WMI 服務。
範例
如果要使用所提供的任何程式碼範例,您必須選擇建立應用程式用的程式設計環境、程式設計範本,及程式設計語言。如需詳細資訊,請參閱《SQL Server 線上叢書》中的<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>(英文) 或<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>(英文)。
如果程式使用 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.");
}
}