共用方式為


ILockBytes::ReadAt

從位元組陣列的開頭,ReadAt 在指定的位移開始讀取指定數量的位元組。

語法

HRESULT ReadAt(   ULARGE_INTEGER ulOffset,   void *pv,   ULONG cb,   ULONG *pcbRead    );

參數

參數 描述

ulOffset [in]

指定位元組陣列開頭處的起點,來讀取資料。

pv [in]

讀入位元組陣列的緩衝區指標。

cb [in]

指定嘗試從位元組陣列讀取的資料位元組數。

pcbRead [out]

此方法將寫入從位元組陣列讀取之實際位元組數的位置指標。您可以將這個指標設為 NULL,表示您對這個值沒有興趣。在這種情況下,這個方法就不會提供讀取的實際位元組數。

傳回值

辭彙 定義

NOERROR

方法已成功。

E_UNEXPECTED

資料列集處於「廢止」狀態。

STG_E_INVALIDPOINTER

傳送的指標無效。

備註

以下範例顯示如何使用 ILockBytes::ReadAt 方法來擷取二進位大型物件 (BLOB) 資料。此範例會從 Employees 資料表的 Photo 資料行讀取 image 資料。

範例

// Declarations
HRESULT            hr;
DBBINDING          rgBindings[1];
DBPROP             dbprop[1];
DBPROPSET          dbpropset[1];
DBOBJECT           dbObject;
BYTE               pData[100];    //buffer for holding the ILockBytes pointer
HROW               rghRows[1]; // Array of row handles obtained from the rowset object
HROW               *prghRows           = rghRows;
BYTE               pBuffer[1000];  //Buffer for holding a chunck of BLOB data
ULARGE_INTEGER     ulOffset;
ULONG              cRows               = 0;
IDBInitialize      *pIDBInitialize     = NULL;        
IDBProperties      *pIDBProperties     = NULL;        
IDBCreateSession   *pIDBCreateSession  = NULL;
IDBCreateCommand   *pIDBCreateCommand  = NULL;
ICommandText       *pICommandText      = NULL;
IAccessor          *pIAccessor         = NULL;
IRowset            *pIRowset           = NULL;
ILockBytes         *pILockBytes        = NULL;
HACCESSOR          hAccessor           = DB_NULL_HACCESSOR;

if (FAILED(hr = CoInitialize(NULL)))
{
    return;
}

VariantInit(&dbprop[0].vValue);        

// Create an instance of the OLE DB Provider
hr = CoCreateInstance(CLSID_SQLSERVERCE_3_0, 0, CLSCTX_INPROC_SERVER,
                IID_IDBInitialize, (void**)&pIDBInitialize);
if(FAILED(hr))
{
    //Send error-specific message and do error handling
    goto Exit;
}

// Initialize a property with name of the database
// Open an exsiting database myDatabase
dbprop[0].dwPropertyID     = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions         = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt         = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(L"\\windows\\MyDB.sdf");
if(NULL == dbprop[0].vValue.bstrVal)
{
    hr = E_OUTOFMEMORY;
    goto Exit;
}

// Initialize the property set
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties     = dbprop;
dbpropset[0].cProperties     = sizeof(dbprop)/sizeof(dbprop[0]);

//Set initialization properties.
hr = pIDBInitialize->QueryInterface(IID_IDBProperties, 
                (void **)&pIDBProperties);
if(FAILED(hr))
{
    goto Exit;
}

// Sets properties in the data source and initialization property groups
hr = pIDBProperties->SetProperties(1, dbpropset); 
if(FAILED(hr))
{
    goto Exit;
}

// Initializes a data source object 
hr = pIDBInitialize->Initialize();
if(FAILED(hr))
{
    goto Exit;
}

// Get the IDBCreateSession interface
hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession,
    (void**)&pIDBCreateSession);
if (FAILED(hr))
{
    //Send error-specific message and do error handling
    goto Exit;
}
// Get the interface to create a command
hr = pIDBCreateSession->CreateSession(NULL, IID_IDBCreateCommand,
    (IUnknown**) &pIDBCreateCommand);
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}


// Create the new command that uses parameters.
hr = pIDBCreateCommand->CreateCommand(NULL, IID_ICommandText, (IUnknown**) &pICommandText);
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

// Specify the command text using parameter markers in the query syntax.
// We know that this command will return 1 row and one image column (Photo).
hr = pICommandText->SetCommandText(DBGUID_DBSQL, L"SELECT Photo FROM Employees WHERE \"Employee ID\" = 2");
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

hr = pICommandText->Execute(NULL, IID_IRowset, NULL, NULL, (IUnknown**) &pIRowset);
if(FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

// Create the acessor object and column specific bindings.

hr = pIRowset->QueryInterface(IID_IAccessor, (void**) &pIAccessor);
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

// Create the bindings for the photo (BLOB) column
dbObject.dwFlags = STGM_READ;
dbObject.iid     = IID_ILockBytes;
rgBindings[0].iOrdinal = 1;
rgBindings[0].obStatus = 0;
rgBindings[0].obLength = rgBindings[0].obStatus + sizeof(DBSTATUS);
rgBindings[0].obValue = rgBindings[0].obLength + sizeof(ULONG);
rgBindings[0].pTypeInfo = NULL;
rgBindings[0].pObject = &dbObject;
rgBindings[0].pBindExt = NULL;
rgBindings[0].dwPart = DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS;
rgBindings[0].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
rgBindings[0].eParamIO = DBPARAMIO_NOTPARAM;
rgBindings[0].cbMaxLen = sizeof(IUnknown*);
rgBindings[0].dwFlags = 0;
rgBindings[0].wType = DBTYPE_IUNKNOWN;
rgBindings[0].bPrecision = 0;
rgBindings[0].bScale = 0;

// Create accessor.
hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, 1, rgBindings,
    0, &hAccessor, NULL);
if(FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

//Get the first row
hr = pIRowset->GetNextRows(NULL, 0, 1, &cRows, &prghRows);
if(FAILED(hr) || (0 == cRows))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

//Get the ILockBytes pointer
hr = pIRowset->GetData(rghRows[0], hAccessor, pData);
if(FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

if(DBSTATUS_S_OK != *(DBSTATUS*)(pData + rgBindings[0].obStatus))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

// Get the ILockBytes pointer from the buffer.
pILockBytes = *(ILockBytes**)(pData + rgBindings[0].obValue);

// Initialize Offset. 
ulOffset.QuadPart = 0;

// Retrieve data from the interface in chunks. 
// The infinite loop, for(;;), will terminate when the ReadAt call fails 
// or if less bytes are returned than were requested. 
for(;;)

{

    ULONG cbRead;

    // Read data at the specified offset
    hr = pILockBytes->ReadAt(ulOffset, pBuffer, sizeof(pBuffer)-sizeof(WCHAR), 
    &cbRead);

/////////////////////////////////////////////////////////////////////////
// Do some useful things here with the chunks of data
/////////////////////////////////////////////////////////////////////////

    if(cbRead < sizeof(pBuffer)-sizeof(WCHAR) || FAILED(hr)) break;

    ulOffset.QuadPart += cbRead;
}

//If more rows need to be read, then release the ILockBytes pointer and 
// row handle here

Exit: 

// When finished, clear the properties arrays and release interfaces.
// Uninitialize the environment.

return;

請參閱

參考

ILockBytes::SetSize
ILockBytes::Stat
ILockBytes::WriteAt

其他資源

ILockBytes (SQL Server Compact Edition)

說明及資訊

取得 SQL Server Compact Edition 協助