This example illustrates a simple OLE DB program that reads a table through a rowset, printing each row along the way. After an instance of a data source object for an e-mail data provider is created, initialized and its properties are set, the IDBCreateSession interface is requested. This returns a pointer to its IOpenRowset interface. The rowset is opened corresponding to the e-mail file. (First a property set "TablePropSet" is initialized for the properties that specify the e-mail file, then the rowset is opened.) Column type information is retrieved and bindings established. The IAccessor interface is requested from the rowset. An accessor handle is returned after creating the accessor. In the while loop, 100 rows at a time are read into the rowset. GetData copies the rows into the local buffers, performing the type conversions specified in the binding structures associated with the accessor. After rows are printed from the rowset they are released, ending the loop. The accessor handle, rowset, session and data source object are released.
Note Code to check status and errors after each call is omitted for clarity.
#include<oledb.h>
extern GUID CLSID_MailProvider
IDBInitialize *pIDBInit;
IDBProperties *pIDBProps;
OLECHAR *szColNames;
HRESULT hr;
HACCESSOR hAccessor;
IDBCreateSession *pIDBCS;
IOpenRowset *pIOpenRowset;
IRowset *pIRowset;
IColumnsInfo *pIColsInfo;
IAccessor *pIAccessor;
DBCOLUMNINFO *rgColInfo;
int irow;
DBID TableID;
DBBINDSTATUS rgStatus[10];
HROW *rghRows;
ULONG cCol, cBindings, cRowsObtained;
DBBINDING rgBindings[10];
DBPROPSET TablePropSet;
void *rgData;
void PrintData(void*);
void CreateBindingsFromInfo(struct tagDBCOLUMNINFO*, ULONG*,
struct tagDBBINDING(*)[10], void**);
int main() {
// Initialize OLE
CoInitialize(NULL);
// Create an instance of a data source object for an e-mail data provider.
CoCreateInstance(CLSID_MailProvider, 0, CLSCTX_LOCAL_SERVER, IID_IDBInitialize,
(void**) &pIDBInit);
// Initialize the data source object for e-mail data provider.
DBPROP rgProps[4];
// Initialize the VARIANTs and the options in rgProps.
for (ULONG i = 0; i <= 3; i++) {
VariantInit(&rgProps[i].vValue);
rgProps[i].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProps[i].vValue.vt = VT_BSTR;
};
rgProps[0].dwPropertyID = DBPROP_INIT_LOCATION;
rgProps[0].vValue.bstrVal =
SysAllocStringLen(OLESTR("email_server "), wcslen(OLESTR("email_server ")));
rgProps[1].dwPropertyID = DBPROP_INIT_DATASOURCE;
rgProps[1].vValue.bstrVal =
SysAllocStringLen(OLESTR("c:\\mail\\smith.mmf"),
wcslen(OLESTR("c:\\mail\\smith.mmf")));
rgProps[2].dwPropertyID = DBPROP_AUTH_PASSWORD;
rgProps[2].vValue.bstrVal =
SysAllocStringLen(OLESTR("password"), wcslen(OLESTR("password")));
rgProps[3].dwPropertyID = DBPROP_AUTH_USERID;
rgProps[3].vValue.bstrVal =
SysAllocStringLen(OLESTR("Smith"), wcslen(OLESTR("Smith")));
// Create the initialization structure.
DBPROPSET PropSet;
PropSet.rgProperties = rgProps;
PropSet.cProperties = 4;
PropSet.guidPropertySet = DBPROPSET_DBINIT;
// Set the initialization properties.
pIDBInit->QueryInterface(IID_IDBProperties, (void**) &pIDBProps);
pIDBProps->SetProperties(1, &PropSet);
pIDBProps->Release();
// Initialize the data source object.
hr = pIDBInit->Initialize();
// Request the IDBCreateSession interface
pIDBInit->QueryInterface(IID_IDBCreateSession, (void**) &pIDBCS);
// Create a session returning a pointer to its
// IOpenRowset interface
pIDBCS->CreateSession(NULL, IID_IOpenRowset, (IUnknown**) &pIOpenRowset);
// Open a rowset corresponding to the e-mail file. First initialize a property set
// "TablePropSet" for the properties that specify the e-mail file, then open the
// rowset.
pIOpenRowset->OpenRowset(NULL,&TableID,NULL, IID_IRowset, 1, &TablePropSet,
(IUnknown**) &pIRowset );
// Get information about column types.
pIRowset->QueryInterface(IID_IColumnsInfo, (void**) &pIColsInfo);
pIColsInfo->GetColumnInfo(&cCol, &rgColInfo, &szColNames);
pIColsInfo->Release();
// Establish bindings using a convenience function.
CreateBindingsFromInfo(rgColInfo,&cBindings, &rgBindings, &rgData);
// Request the IAccessor interface from rowset.
pIRowset->QueryInterface(IID_IAccessor, (void**) &pIAccessor);
// Create an accessor, return an accessor handle.
pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, cBindings, rgBindings, 0, &hAccessor,
rgStatus);
// Read the rows; 100 rows at a time into the rowset.
while(SUCCEEDED(hr = pIRowset->GetNextRows(NULL, 0, 100, &cRowsObtained, &rghRows))
&& cRowsObtained) {
for(irow = 0; irow < cRowsObtained; irow++) {
// GetData copies the rows into the local buffers, performing the type
// conversions specified in the binding structures associated with the
// accessor.
pIRowset->GetData(rghRows[irow], hAccessor, (void*) rgData);
// Convenience function to print the data.
PrintData(rgData);
}
// Release the rows just printed from the rowset.
pIRowset->ReleaseRows(cRowsObtained, rghRows, NULL, NULL, NULL);
}
// Release the accessor handle and the rowset.
pIAccessor->ReleaseAccessor(hAccessor);
pIAccessor->Release();
pIRowset->Release();
// Release the session and data source object.
pIOpenRowset->Release();
pIDBCS->Release();
pIDBInit->Release();
// Uninitialize OLE.
CoUninitialize();
return;
}