HRESULT CreateAccessor
(
 IRowset*		pIRowset,
 DBBINDING*		rgBind,
 DBCOUNTITEM	cBind,
 HACCESSOR*		phAccessor_out 
 )
{
	IAccessor*	pIAccessor = NULL;
	HACCESSOR   hAccessor;
	HRESULT 	hr;


	assert(pIRowset != NULL);
	assert(rgBind != NULL);
	assert(phAccessor_out != NULL);

	// Get an accessor for our bindings from the rowset, via IAccessor 
	hr = pIRowset->QueryInterface( IID_IAccessor, (void**)&pIAccessor );
	if (FAILED(hr))   	
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "pIRowset->QI for IID_IAccessor" );	
		goto error;
	}
	hr = pIAccessor->CreateAccessor( DBACCESSOR_ROWDATA, cBind, rgBind, 0, 
		&hAccessor, NULL );
	if (FAILED(hr))   	
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "pIAccessor->CreateAccessor" );	
		goto error;
	}
	pIAccessor->Release();
	pIAccessor = NULL;

	*phAccessor_out = hAccessor;

	return ResultFromScode( S_OK );	

error:
	if (pIAccessor)
		pIAccessor->Release();
	*phAccessor_out = NULL;

	return ResultFromScode( hr );
}
HRESULT CleanupRowset
(
 IRowset*	pIRowset,
 HACCESSOR 	hAccessor
 )
{
	IAccessor*	pIAccessor = NULL;
	HRESULT		hr;

	assert(pIRowset != NULL);

	// tell the rowset object it can release the accessor, via IAccessor
	hr = pIRowset->QueryInterface( IID_IAccessor, (void**)&pIAccessor );
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "pIRowset->QI for IID_IAccessor" );
		goto error;
	} 
	hr = pIAccessor->ReleaseAccessor( hAccessor, NULL );
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "pIAccessor->ReleaseAccessor" );
		goto error;
	}    
	pIAccessor->Release();
	pIAccessor = NULL;

	return ResultFromScode( S_OK );

error:    
	if (pIAccessor)
		pIAccessor->Release();

	return ResultFromScode( hr );    
}
void CClientDB::GetInventoryData( const char* pszName, std::list<S_InventoryData>& Inventory)
{
	ICommandText*       pICommandText           = NULL;
	IRowset*            pIRowset                = NULL;
	HRESULT             hr;

	ULONG               cbColOffset = 0;
	LONG                cNumRows = 0;
	LONG				cRowsAffected = 0;
	int					iCount = 0;

	unsigned long		cRowsObtained;
	unsigned long		cCount;
	HACCESSOR			hAccessor;
	IAccessor *			pIAccessor = NULL;

	WCHAR wCmdString[1024];
	wsprintfW(wCmdString, L"SELECT realindex,virtualindex FROM INVENTORY WHERE name = '%S'", pszName );

	pICommandText = CreateCommand( m_pIDBInitialize ,wCmdString );
	if( pICommandText == NULL )
	{
		assert( 0 && "Fail GetInventoryData");
		return;
	}

   
    if(FAILED(hr = pICommandText->Execute(
                                    NULL, 
                                    IID_IRowset, 
                                    NULL, 
                                    &cNumRows, 
                                    (IUnknown **) &pIRowset)))
    {
		assert( 0 && "Fail GetInventoryData : ICommandText->Execute");
	    pICommandText->Release();
		return;
    }

	if( FAILED( pIRowset->QueryInterface( IID_IAccessor, (void **) &pIAccessor)))
    {
		assert( 0 && "Fail IRowset->QueryInterface"	);
		pIRowset->Release();
		pICommandText->Release();
		return;
    }
	
	const int       nParams = 2;
	S_InventoryData Data;
	memset( &Data, 0, sizeof( S_InventoryData ));
	
	static DBBINDING ExactBindings [nParams] = {
      {
         1,                          // iOrdinal
         offsetof (S_InventoryData, lRealIndex), // 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 (long),
         0,
         DBTYPE_I4,
         0,                          // No Precision
         0                           // No Scale
      },
      {
         2,                          // iOrdinal
         offsetof (S_InventoryData, lVirtualIndex ),   // 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(long),
         0,
         DBTYPE_I4,
         0,                          // No Precision
         0                           // No Scale
      },
	};

    if( FAILED( hr = pIAccessor->CreateAccessor( 
				DBACCESSOR_ROWDATA, 
                                nParams, 
                                ExactBindings, 
                                0, 
                                &hAccessor, 
                                NULL)))
    {
		pIAccessor->Release();
		pIRowset->Release();
		pICommandText->Release();
		assert( 0 && "Fail CreateAccessor");
		return;
        // Handle errors here.
    }

    HROW*	pRows = new HROW[256];
    while( TRUE )
    {
        if( FAILED( hr = pIRowset->GetNextRows( NULL, 0, 256, &cRowsObtained, &pRows ) ) )
        {
			CheckErrorGetNextRows( hr );
            break;
			// Handle errors here.
        }

        // Make sure some rows were obtained.
        if( cRowsObtained == 0 )
        {
			LogString(LOG_NORMAL,"ROW IS 0");
            break;
        }

        // Get the data for the each of the rows.
        for( cCount = 0; cCount < cRowsObtained; cCount++ )
        {
            // Get the row data needed.
			ZeroMemory( &Data, sizeof( Data ));
            if( FAILED( hr = pIRowset->GetData( pRows[cCount], hAccessor,  (void*)&Data )))
                CheckErrorGetData( hr );
			else
				Inventory.push_back( Data );

	    }
        // Release the rows.
        if( FAILED( hr = pIRowset->ReleaseRows( 
                                    cRowsObtained, 
                                    pRows, 
                                    NULL, 
                                    NULL, 
                                    NULL )))
        {
            // Handle errors here.
        }
		memset( &Data, 0 , sizeof( S_InventoryData ));
    }

    if( FAILED( hr = pIAccessor->ReleaseAccessor( hAccessor, NULL )))
    {
        // Handle errors here.
    }

    // Release the IAccessor object.
	if( pRows )
		delete []pRows;

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