1、向数据库中插入一条带有BLOB字段的记录 以下为手动敲入 由于环境问题不能copy 代码 如有笔误或者错误请见谅 _Connection m_pConnection;//假设已得到 CFile m_file; CFileException e; _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance(__uuidof(Recordset)); HRESULT hr = m_pRecordset->Open("select A,B,C from D",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); if(SUCCEEDED(hr)){ m_pRecordset->AddNew(); m_pRecordset->PutCollect("A",_variant_t(//CString 值)); ... if(m_file.Open(filePath/*文件路径*/,CFile::modeRead | CFile::typeBinary,&e)){ int nsize = m_file.GetLength(); char *m_pbuff = NULL; SAFEARRAYBOUND rgs[1]; rgs[0].lLbound = 0; rgs[0].cElements = nsize; SAFEARRAY *pas; pas = SafeArrayCreate(VT_UI1,1,rgs); SafeArrayAccessData(psa,(void **)&m_pbuff); m_file.ReadHuge(m_pbuff,nsize); m_file.Close(); variant_t varBOLB; varBLOB.vt = VT_ARRAY | VT_UI1; varBLOB.parray = psa; m_pRecordset->GetFields()->GetItem("C")->AppendChunk(varBLOB); } m_pRecordset->Update(); SafeArrayUnaccessData(psa); } m_pRecordset->Close(); m_pRecordset.Release() 2. 从数据库中读取BLOB字段生成文件 //_Recordset .CFile 等声明同上 CString strSql; variant_t var; CString fileName; strSql.Format("select fileName,fileContent from XX where uid='%s'",uid); m_pRecordset = m_pConnection->Execute((_bstr_t)strSql,NULL,adCmdText); if(!m_pRecordset->BOF) { m_pRecordset->MoveFirst(); } while(!m_pRecordset->adoEOF) { var = m_pRecordset->GetCollect("fileName"); if(var.vt != NULL) { fileName = (LPCTSTR)(_bstr_t)var; } long lDataSize = m_pRecordset->GetFields()->GetItem("fileContent")->ActualSize; _variant_t varBLOB = m_pRecordset->GetFields()->GetItem("fileContent")->GetChunk(lDataSize); CFile outFile(_T(XXX+"//"+fileName),CFile::modeWrite | CFile::modeCreate);//XXX+"//"+fileName 文件据对路径 if(varBLOB.vt == (VT_ARRAY | VT_UI1)) { char * pbuf = NULL; SafeArrayAccessData(varBLOB.parray,(void **)&pbuf); outFile.WriteHuge(pbuf,lDataSize); SafeArrayUnaccessData(varBLOB.parray); } m_pRecordset->MoveNext(); } m_pRecordset->Close(); m_pRecordset.Release()