在 Configuration Manager 中,若要使用受控 SMS 提供者執行異步查詢,請使用 ProcessQuery 方法。
ProcessQuery 方法的第一個參數是 SmsBackgroundWorker 類別的實例,可提供兩個事件處理程式:
QueryProcessObjectReady。 查詢所傳回的每個物件都會呼叫此事件處理程式。 事件處理程式會提供代表 物件的IResultObject物件。
QueryProcessCompleted。 查詢完成時,會呼叫此事件處理程式。 它也會提供發生任何錯誤的相關信息。 如需詳細資訊,請參閱如需錯誤處理的相關信息,請參閱如何使用 Managed 程式代碼來處理 Configuration Manager 異步錯誤。
ProcessQuery 方法的第二個參數是查詢的 WQL 語句。
執行異步查詢
設定與SMS提供者的連線。 如需詳細資訊,請 參閱SMS提供者基本概念。
建立 SmsBackgroundWorker 物件,並以回呼方法名稱填入 QueryProcessorObjectReady 和 QueryProcessorCompleted 屬性。
從您在步驟一中取得 的 WqlConnectionManager 物件中,呼叫 QueryProcessor 物件 ProcessQuery 方法來啟動異步查詢。
範例
下列範例會查詢所有可用的SMS_Collection物件,而在事件處理程式中,此範例會將數個集合屬性寫入 Configuration Manager 控制台。
如需呼叫範例程式代碼的相關信息,請參閱呼叫 Configuration Manager 代碼段。
public void QueryCollections(WqlConnectionManager connection)
{
try
{
// Set up the query.
SmsBackgroundWorker bw1 = new SmsBackgroundWorker();
bw1.QueryProcessorObjectReady += new EventHandler<QueryProcessorObjectEventArgs>(bw1_QueryProcessorObjectReady);
bw1.QueryProcessorCompleted += new EventHandler<RunWorkerCompletedEventArgs>(bw1_QueryProcessorCompleted);
// Query for all collections.
connection.QueryProcessor.ProcessQuery(bw1, "select * from SMS_Collection");
// Pause while query runs.
Console.ReadLine();
}
catch (SmsException ex)
{
Console.WriteLine("Failed to start asynchronous query: ", ex.Message);
}
}
void bw1_QueryProcessorObjectReady(object sender, QueryProcessorObjectEventArgs e)
{
try
{
// Get the collection.
IResultObject collection = (IResultObject)e.ResultObject;
//Display properties.
Console.WriteLine(collection["CollectionID"].StringValue);
Console.WriteLine(collection["Name"].StringValue);
Console.WriteLine();
collection.Dispose();
}
catch (SmsQueryException eX)
{
Console.WriteLine("Query Error: " + eX.Message);
}
}
void bw1_QueryProcessorCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("Done...");
}
此範例方法具有下列參數:
| 參數 | Type | 描述 |
|---|---|---|
connection |
管理: WqlConnectionManager |
SMS 提供者的有效連線。 |
正在編譯程式碼
命名空間
系統
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
組件
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
健全的程式設計
可引發的 Configuration Manager 例外狀況為 SmsConnectionException 和 SmsQueryException。 這些可以與 SmsException一起攔截。
另請參閱
物件概觀Configuration Manager 延遲屬性如何使用 Managed 程式代碼呼叫 Configuration Manager 物件類別方法如何使用 Managed 程式代碼連接到 Configuration Manager 提供者如何建立Configuration Manager 物件使用 Managed 程式代碼如何使用 Managed 程式代碼修改 Configuration Manager 物件如何使用 Managed 程式代碼執行同步 Configuration Manager 查詢如何讀取 Configuration Manager使用 Managed 程式代碼的物件如何使用 Managed 程式代碼讀取延遲屬性如何使用 Managed 程式代碼執行同步 Configuration Manager 查詢Configuration Manager 擴充 WMI 查詢語言Configuration Manager 結果集Configuration Manager 關於查詢的特殊查詢