Creating and Executing a Query

Note

Indexing Service is no longer supported as of Windows XP and is unavailable for use as of Windows 8. Instead, use Windows Search for client side search and Microsoft Search Server Express for server side search.

 

The following code segments from the IssueQuery function of the sample create and execute a query. The function first declares an ICommand interface and uses the CIMakeICommand function of the OLE DB Helper API to get a pointer to an ICommand interface for the specified scope, catalog, and machine properties.

...
    XInterface<ICommand> xICommand;
    HRESULT hr = CIMakeICommand( xICommand.GetPPointer(),  // result
                                 1,                        // 1 scope
                                 &amp;dwScopeFlags,            // scope flags
                                 &amp;pwcQueryScope,           // scope path
                                 &amp;pwcQueryCatalog,         // catalog
                                 &amp;pwcQueryMachine );       // machine
...

The function next declares an ICommand interface and queries the ICommand interface for a pointer to an ICommandTree interface.

...
    XInterface<ICommandTree> xICommandTree;
    hr = xICommand->QueryInterface( IID_ICommandTree,
                                    xICommandTree.GetQIPointer() );
...

Then the function declares a DBCOMMANDTREE structure for the command tree of the query and uses the CITextToFullTreeEx function to create the command tree structure using previously set query properties.

...
    DBCOMMANDTREE * pTree;
    hr = CITextToFullTreeEx( pwcQueryRestriction, // the query itself
                             ulDialect,           // query dialect
                             pwcColumns,          // columns to return
                             pwcSort,             // sort order, may be 0
                             0,                   // reserved
                             &amp;pTree,              // resulting tree
                             0,                   // no custom properties
                             0,                   // no custom properties
                             lcid );              // default locale
...

The function next uses the SetCommandTree method of the ICommandTree interface to set the command tree in the ICommandTree interface.

...
    hr = xICommandTree->SetCommandTree( &amp;pTree,
                                        DBCOMMANDREUSE_NONE,
                                        FALSE );
...

The function finally declares an IRowset interface to represent the results of the query and uses it with the Execute method of the ICommand interface to execute the query.

...
    XInterface<IRowset> xIRowset;
    hr = xICommand->Execute( 0,            // no aggregating IUnknown
                             IID_IRowset,  // IID for interface to return
                             0,            // no DBPARAMs
                             0,            // no rows affected
                             xIRowset.GetIUPointer() ); // result
...