IRowPosition Code Example

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

IRowPosition *pRowPosition = NULL;
HRESULT hr;

hr = CoCreateInstance(CLSID_OLEDB_ROWPOSITIONLIBRARY, NULL, 
   CLSCTX_INPROC_SERVER, IID_IRowPosition,
   (LPVOID *)&pRowPosition);
if (FAILED(hr))
   return hr;

hr = pRowPosition->Initialize(pRowset);   // Initialize 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 (pRowPosition->ClearRowPosition () == S_OK)
         pRowPosition->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);
}
Show: