共用方式為


How to Perform a Synchronous Configuration Manager Query by Using Managed Code

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

To perform a synchronous query by using the managed SMS Provider, you use WqlConnectionManager.QueryProcessor.ExecuteQuery method.

The ExecuteQuery method takes a WQL query string and optional context information for the call. An IResultObject is returned containing the objects found in the query.

To perform a synchronous query

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Using the WqlConnectionManager object you obtain in step one, call the QueryProcessor object ExecuteQuery method to query SMS Provider and get an IResultObject containing a collection of query results.

Example

The following code example shows how to make a synchronous query for the available packages by using ExecuteQuery.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

public void QueryPackages(WqlConnectionManager connection)
{
    try
    {
        IResultObject query = connection.QueryProcessor.ExecuteQuery("Select * from SMS_Package");
        foreach (IResultObject o in query)
        {
            Console.WriteLine(o["Name"].StringValue);
            o.Dispose();
        }
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to query packages: " + ex.Message);
        throw;
    }
}

This example method has the following parameters:

Parameter Type Description

connection

Managed: WqlConnectionManager

A valid connection to the SMS Provider.

Compiling the Code

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.

See Also

Concepts

About Configuration Manager Objects
Configuration Manager Lazy Properties
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Create a Configuration Manager Object by Using Managed Code
How to Modify a Configuration Manager Object by Using Managed Code
How to Perform an Asynchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code
How to Read Lazy Properties by Using Managed Code
How to Use Configuration Manager Objects with Managed Code
Configuration Manager Extended WMI Query Language
Configuration Manager Result Sets
Configuration Manager Special Queries
Configuration Manager Queries