添加二进制文件到数据库示例

时间:2022-03-25 20:58:08

添加二进制文件到数据库示例

第一步:读取文件到类成员变量m_pFileBuffer

BOOL CDLGAddModel::Load3DDataFile(CString fileName)

{

    CFile file;

    if( !file.Open( fileName, CFile::modeRead) )

        return FALSE;

    m_fileLen = file.GetLength();

    HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, m_fileLen );

    LPVOID lpBuf = ::GlobalLock( hMem );

    if( file.Read( lpBuf, m_fileLen ) != m_fileLen )

        return FALSE;

    file.Close();

    m_pFileBuffer = (char *)lpBuf; 

    ::GlobalUnlock( hMem );

    return TRUE;

}

 

第二步:向数据库写入数据

……

_RecordsetPtr   pRecordset;

pRecordset.CreateInstance(__uuidof(Recordset));

pRecordset->Open("SELECT * FROM model",_variant_t((IDispatch*)DBM.p_Con),adOpenStatic,adLockOptimistic,adCmdText);

pRecordset->AddNew();

Load3DDataFile(fileName);

VARIANT pvList;

SetDataToVariant(pvList,(unsigned char *)m_pFileBuffer);

pRecordset->Fields->Item["data"]->AppendChunk(pvList);

pRecordset->Update();

pRecordset->Close();

……