IRowPosition Code Example

The following code fragment shows how a consumer might use IRowPosition when traversing a rowset.

// Code to generate a pRowset
…

IRowPosition * pRowPosition = NULL;
HRESULT hr;

hr = CoCreateInstance(CLSID_OLEDB_ROWPOSITIONOBJECT, NULL, CLSCTX_INPROC_SERVER,  IID_IRowPosition,
(LPVOID *)&pRowPosition)

if (SUCCEEDED(hr))
   {
   hr = pRowPosition->Initialize(pRowset);   // init with pRowset

   if (FAILED(hr))
       pRowPosition->Release();
   }

return hr;

// Read the rows; 100 rows at a time into the rowset.
While(SUCCEEDED(hr = pRowset->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.
  pRowset->GetData(rghRows[irow], hAccessor, (void*) rgData);

   // Set currently read row as “current row”
   // Note: other clients will choose different notions of current row.
   If (pIRowPosition->ClearRowPosition () == S_OK)
         pIRowPosition->SetRowPosition (NULL, rghRows[irow], DBPOSITION_OK);

   // Convenience function to print the data.
   PrintData(rgData);
 }

 // Release the rows just printed from the rowset.
 PRowset->ReleaseRows(cRowsObtained, rghRows, NULL, NULL, NULL);

}