Searching with OLE DB
For both Automation clients using ActiveX Data Objects (ADO) and all non-Automation clients, ADSI supplies an OLE DB provider that supports a subset of OLE DB query interfaces. Client code that already uses OLE DB interfaces for queries can use the same interfaces to query directory services.
Under the OLE DB implementation, a directory service is exposed as a Data Source Object. Data source objects are factories for session objects and support IDBInitialize to connect to the directory, IDBCreateSession to create the session object, IDBProperties to supply authentication information to the underlying namespace and supply the query command and IPersist to save the information necessary to create the data source object to the underlying directory service.
To perform an Active Directory query using OLE DB
- Retrieve the IDBInitialize interface from the OLE DB provider. In the case of Active Directory, use the class id "CLSID_ADsDSOObject".
- Build a DBPROP array of connection information specifying user name and password.
- Query the IDBInitialize interface retrieved in Step 1 for the IDBProperties interface.
- Call the IDBProperties::Setproperties method passing in the DBPROP array created in Step 2.
- Call the IDBInitialize::Initialize method to establish the connection to the OLE DB provider (the Active Directory provider, in this case).
- Query the IDBInitialize interface for the IDBCreateSession interface.
- Call the IDBCreateSession::CreateSession method requesting a new interface of type IDBCreateCommand.
- Call the IDBCreateCommand::CreateCommand method to create an ICommandText interface.
- Call the ICommandText::SetCommandText method. Pass in the preferred dialect and the actual query command text in that dialect. Either DBGUID_LDAPDialect or DBGUID_DBSQL may be used as the dialect.
- Call ICommandText::Execute, an IRowset interface is returned which is the interface to the result set.
- Query the IRowset interface for the IColumnsInfo interface.
- Call the IColumnsInfo::GetColumnInfo method to retrieve column information about the result set.
- Populate an array of DBBINDING structures, describing to the OLE DB provider how to expose the data types on a per column basis to the application code. This step allows you to specify which TYPE is contained in a particular column. Also the offsets of the columns (relative to the returned row) are set here on a column-by-column basis.
- Query the IRowset interface for the IAccessor interface.
- Call the IAccessor::CreateAccessor method which returns an array of accessor handles. This array is then used to access the rows of the result set.
- Call IRowset::GetNextRows passing in the row handles, and number of rows to fetch.
- Call IRowset::GetData passing in a row handle (from the set returned in Step 16). A raw pointer to the row is returned.
To read the raw row data returned, use the DBBINDING structure (created in Step 13), compute the column offsets in the raw data pointer returned in Step 17. Read the status portion of the column for a retrieval result on that column.