如何将一个CBitmap对象存到数据库的Blob字段中?

huangdong 2000-10-20 09:06:00
...全文
174 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
masterz 2000-12-24
  • 打赏
  • 举报
回复
write a file into access database.
This database only have a table: table1;
two field: recid---autonumber; imagefd---blob field


CFile imagefile;
if(0==imagefile.Open(m_sSourceFile,CFile::modeRead))
return;//read file error
_RecordsetPtr pRs = NULL;
_ConnectionPtr pConnection = NULL;
_variant_t varChunk;
HRESULT hr;
BYTE* pbuf;
int nLength=imagefile.GetLength();
pbuf=new BYTE[nLength+2];
if(pbuf==NULL)
return;//allocate memory error;
imagefile.Read(pbuf,nLength);//read the file into memory

BYTE *pBufEx;
pBufEx= pbuf;
//build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);

for (long i = 0; i < nLength; i++)
SafeArrayPutElement (psa, &i, pBufEx++);
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;

VARIANT var;
VariantInit(&var);
var.vt=VT_ARRAY|VT_UI1;
_bstr_t strCnn("driver={Microsoft Access Driver (*.mdb)};dbq=g:\\tmp\\db1.mdb;");
try
{
//Open a connection
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
hr = pConnection->Open(strCnn,"","",NULL);

TESTHR(pRs.CreateInstance(__uuidof(Recordset)));

pRs->Open("Table1",
_variant_t((IDispatch *) pConnection,true),
adOpenKeyset,adLockOptimistic,adCmdTable);
pRs->AddNew();
pRs->Fields->GetItem("imagefd")->AppendChunk(varBLOB);
pRs->Update();
pRs->Close();
pConnection->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format("Source : %s \n Description : %s\n",
(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);
}
huangdong 2000-11-17
  • 打赏
  • 举报
回复
实际上根本解决不了问题.
Edward 2000-10-21
  • 打赏
  • 举报
回复
呵呵,纸上谈兵:

BITMAP bm;
DWORD dw;

myBitmap.GetObject(&bm);
dw = bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes
// bm.bmBits 是指向位图数据的指针,上面得到的 dw 是数据大小。
// 现在你可以用 ADO 的 AppendChunk 把这块数据存入 BLOB 字段了。

至于如何用 ADO 操作数据库,详情请看 MSDN ADO 参考;例子较多,不占用 CSDN 可怜的速度了。

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧