共用方式為


使用 IRow::GetColumns 和 ISequentialStream 提取 BLOB 資料

下列函數會使用 IRow::GetColumnsISequentialStream 來提取大型資料:

void InitializeAndExecuteCommand()
{
    ULONG iidx;
    WCHAR* wCmdString=OLESTR("SELECT * FROM MyTable");
    // Do the initialization, create the session, and set command text.
    hr = pICommandText->Execute(NULL, IID_IRow, NULL, 
                         &cNumRows,(IUnknown **)&pIRow)))
    //Get 1 column at a time.
    for(ULONG i=0; i < NoOfColumns; i++)
      GetSequentialColumn(pIRow, iidx);
    // Do the clean up.
}
HRESULT GetSequentialColumn(IRow* pUnkRow, ULONG iCol)
{
    HRESULT hr = NOERROR;
    ULONG cbRead = 0;
    ULONG cbTotal = 0;
    ULONG cColumns = 0;
    ULONG cReads = 0;
    ISequentialStream* pIStream = NULL;
    WCHAR* pBuffer[kMaxBuff]; //50 chars read by ISequentialStream::Read()
    DBCOLUMNINFO* prgInfo;
    OLECHAR* pColNames;
    IColumnsInfo* pIColumnsInfo;
    DBID columnid;
    DBCOLUMNACCESS column;
    hr = pUnkRow->QueryInterface(IID_IColumnsInfo, 
                            (void**) &pIColumnsInfo);
    if(FAILED(hr))
        goto CLEANUP;
    hr = pIColumnsInfo->GetColumnInfo(&cColumns, &prgInfo, &pColNames);
    // Get Column ID.
    columnid = (prgInfo + (iCol))->columnid;
    IUnknown* pUnkStream = NULL;
    ZeroMemory(&column, sizeof(column));
    column.columnid = prgInfo[iCol].columnid;
    // Ask for Iunknown interface pointer.
    column.wType = DBTYPE_IUNKNOWN;
    column.pData = (LPVOID*) &pUnkStream;

    hr = pUnkRow->GetColumns(1, &column);
    // Get ISequentialStream from Iunknown pointer retrieved from
    // GetColumns().
    hr = pUnkStream->QueryInterface(IID_ISequentialStream, 
                                   (LPVOID*) &pIStream);
    ZeroMemory(pBuffer, kMaxBuff * sizeof(WCHAR));
    // Read 50 chars at a time until no more data.
    do
    {
        hr = pIStream->Read(pBuffer, kMaxBuff, &cbRead);
        cbTotal = cbTotal + cbRead;
        // Process the data.
    } while(cbRead > 0);
  // Do the cleanup.
    return hr;
}