+ -
当前位置:首页 → 问答吧 → 请教,急!,mysql中为什么用odbc写入blob字段不成功

请教,急!,mysql中为什么用odbc写入blob字段不成功

时间:2010-08-23

来源:互联网

在网上查了好多相关资料,一直写入不成功,请高手指点!!
我是在windows下,mysql odbc 5.1驱动,vc++ 7.0
代码如下:
recordset->open();
recordset->addnew();
//其他字段
recordset->m_abc = "abc";
......

//blob字段
CFile file;
file.open("c:\\aa.doc",CFile::modeRead|CFile::typeBinary);//打开指定文件
recordset->m_blobfile.m_dwDataLength = file.GetLength();//m_blobfile为数据库中blob字段由classwizard映射过来的CLongBinary字段
HGLOBAL hGlobal = GlobalAlloc(GPTR,recordset->m_blobfile.m_dwDataLength);
recordset->m_blobfile.m_hData = GlobalLock(hGlobal);
file.read(recordset->m_blobfile.m_hData,recordset->m_blobfile.m_dwDataLength);
recordset->SetFieldDirty(&recordset->m_blobfile,true);
recordset->SetFieldNull(&recordset->m_blobfile.false);
GlobalUnlock(hGlobal );
recordset->Update();
recordset->Close();
file.Close();
//////////////////////////////////////////////
上面的代码recordset->Update()执行不报错,其他字段均可以添加成功,只有blob字段还为NULL,调试状态下,内存中recordset->m_blobfile.m_hData的内容是正确的。请教了,这到底是什么问题?谢谢!!!!

作者: chengkai911   发布时间: 2010-08-23

mysql需要这样:


//开始事务

sql="select * from 表名 where id=新id"
recordset->Open(CRecordset::snapshot, sql);
//其他字段
recordset->m_abc = "abc";
...
recordset->Upate();
recordset->requery();
recordset->edit();
//写入CLongBinary字段
recordset->Update();

//结束事务

作者: smh_mouse   发布时间: 2011-12-15

我上面这样做可以写入成功,但是读出来后发现数据不对。

作者: smh_mouse   发布时间: 2011-12-15

热门下载

更多