Retrieving data

The following code example demonstrates how to retrieve data.

StepsTo retrieve data

  1. Create a Command object:

    ICommandText* pICommandText;
    hr = pIDBCreateCommand->CreateCommand(
         NULL, IID_ICommandText,
         (IUnknown**)&pICommandText);
    
  2. Set the SQL statement:

    hr = pICommandText->SetCommandText(
         DBGUID_DBSQL,
         L"SELECT * FROM testReadStringData");
    
  3. Create and describe the rowset data structure. This structure contains fields for each column you want accessed, as shown:

    IAccessor* pIAccessor;
    hr = pICommandText->QueryInterface(IID_IAccessor, (void**)&pIAccessor);
    
    static DBBINDING ExactBindings [1] = {
    {
         1, // iOrdinal
         offsetof (ExactlyTheSame,s), // obValue
         0, // No length binding
         0, // No Status binding
         NULL, // No TypeInfo
         NULL, // No Object
         NULL, // No Extensions
         DBPART_VALUE,
         DBMEMOWNER_CLIENTOWNED, // Ignored
         DBPARAMIO_NOTPARAM,
         sizeof(mystr), // number of bytes
         0,
         DBTYPE_WSTR | DBTYPE_BYREF,
         0, // No Precision
         0 // No Scale
    }
    };
    
    DBBINDSTATUS status[1];
    HACCESSOR hAccessor;
    HRESULT hr = pIAccessor->CreateAccessor(
         DBACCESSOR_ROWDATA, 1, ExactBindings,
         sizeof(ExactlyTheSame), &hAccessor, status);
    pIAccessor->Release();
    
  4. Execute the rowset:

    DBROWCOUNT cRowsAffected;
    IRowset* pIRowset;
    hr = pICommandText->Execute(
    NULL, IID_IRowset, params,
    &cRowsAffected, (IUnknown**)&pIRowset);
    
  5. Use the following code to get the rows one row at a time:

    DBCOUNTITEM cRowsReturned;
    HROW hRow[1];
    HROW* pRow = hRow;
    hr = pIRowset->GetNextRows(NULL, 0, 1, &cRowsReturned, &pRow);
    
  6. Use IMalloc to free the memory allocated by GetData:

    CComPtr<IMalloc> pIMalloc = NULL;
    hr = CoGetMalloc( MEMCTX_TASK, &pIMalloc );
    
    while (hr == S_OK)
    {
    
  7. Retrieve the data for the specified row, for example:

         ExactlyTheSame pData[1] = { {NULL} };
         hr = pIRowset->GetData(hRow[0], hAccessor, pData);
         wchar_t* value = pData[0].s;
    
  8. Free the allocated memory:

         // client owned memory must be freed by the client
         pIMalloc->Free(pData[0].s);
         pData[0].s = NULL;
    
  9. Release the rows:

         hr = pIRowset->ReleaseRows(1, pRow, NULL, NULL, NULL);
    
  10. Get the next row:

         hr = pIRowset->GetNextRows(NULL, 0, 1,
              &cRowsReturned, &pRow);
    }
    
    pIRowset->Release();
    pICommandText->Release();
    

Get the next row:

     hr = pIRowset->GetNextRows(NULL, 0, 1,
          &cRowsReturned, &pRow);
}

pIRowset->Release();
pICommandText->Release();

To retrieve rows from a database, execute a SELECT statement using ICommandText::Execute. This opens a cursor on the statement.Then, use IRowset::GetNextRows to fetch rows through the cursor. When an application frees the statement by releasing the rowset, it closes the cursor.