急求ADO Field AppendChunk插入大字节数据的例子。 简单管用就行,谢啦! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的OLE数据是一个文件。我在file.read就出问题了。 CFile file; file.Open(pathname,CFile::modeRead); ULONG filelenth=file.GetLength();//读取文件长度 SAFEARRAYBOUND szbuffer[1]; SAFEARRAY FAR *psa; _variant_t varChunk; szbuffer->lLbound =0; szbuffer->cElements =filelenth; file.Read(szbuffer,filelenth); file.Close(); BOOL xxxxxx::SaveData(){ try { //读取文件 char *lpBuffer; char *temp; DWORD m_nFileLen; CFile f; if(!f.Open("你的文件名",CFile::modeRead)) { AfxMessageBox("打开文件失败."); return FALSE; } m_nFileLen = f.GetLength(); lpBuffer = new char[m_nFileLen]; if(lpBuffer == NULL) { AfxMessageBox("分配文件缓冲区失败."); return FALSE; } if (f.ReadHuge(lpBuffer,m_nFileLen)!= m_nFileLen) { AfxMessageBox("读文件到内存出错"); return FALSE; } f.Close(); //打开记录集 _RecordsetPtr m_recordsetSave; m_recordsetSave.CreateInstance(__uuidof(Recordset)); _bstr_t SQLWord_t=(_bstr_t)csSelTable; m_recordsetSave->Open(SQLWord_t, _variant_t((IDispatch *)PLMConnection,TRUE), adOpenDynamic,adLockOptimistic, adCmdText); _variant_t var; SAFEARRAY *psa; SAFEARRAYBOUND rgsabound[1]; temp=lpBuffer; if(lpBuffer) { rgsabound[0].lLbound = 0; rgsabound[0].cElements = m_nFileLen; psa = SafeArrayCreate(VT_UI1, 1, rgsabound); //创建SAFEARRAY对象 for (long i = 0; i < (long)m_nFileLen; i++) { SafeArrayPutElement (psa, &i,temp++); //将lpBuffer指向的二进制数据保存到SAFEARRAY对象psa中 } var.vt = VT_ARRAY | VT_UI1;//将var的类型设置为BYTE类型的数组 var.parray = psa; //为var变量赋值 m_recordsetSave->GetFields()->GetItem((_bstr_t)csFieldName)->AppendChunk(var); //加入BLOB类型的数据 } m_recordsetSave->Update(); //保存我们的数据到库中 delete []lpBuffer; } catch (_com_error &e) { CString thismsg; thismsg="保存出错:"; thismsg+=e.ErrorMessage(); AfxMessageBox(thismsg); return FALSE; } return TRUE;} long AddFileToDatabase(CQSTreeItem *pitem, CString filename){ CQsofficeApp *papp=(CQsofficeApp*)AfxGetApp(); CString tablename=pitem->tablename; try { m_commandptr.CreateInstance (__uuidof(Command)); m_pUserSet.CreateInstance (__uuidof(Recordset)); //然后添加到tid????中去 CFile file; if (!file.Open (filename ,CFile::modeRead)) return 2; DWORD filesize=file.GetLength (); char *m_pBuffer; m_pBuffer=new char[filesize+1]; if (file.ReadHuge (m_pBuffer,filesize)!=filesize) return 1; char* pwritebase=m_pBuffer; VARIANT varBlob; SAFEARRAY* psa; SAFEARRAYBOUND safebound[1]; if(pwritebase) { safebound[0].lLbound =0; safebound[0].cElements =filesize; psa=SafeArrayCreate(VT_UI1,1,safebound); for (long i=0;i<(long)filesize;i++) SafeArrayPutElement(psa,&i,pwritebase++); varBlob.vt =VT_ARRAY | VT_UI1; varBlob.parray =psa; } sqltext="insert into "; sqltext+=(_bstr_t)tablename; sqltext+=" (id,parentid,bedelete,data) values (?,?,0,?)"; m_commandptr.CreateInstance (__uuidof(Command)); m_commandptr->ActiveConnection =papp->m_pConnection ; m_commandptr->CommandText =sqltext; m_commandptr->CommandType =adCmdText; m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("tableitemid",adInteger,adParamInput,4,COleVariant((short)tableitemid))); m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("parentid",adInteger,adParamInput,4,COleVariant((short)pitem->parentid ))); m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("data",/*adLongVarBinary*/adVarBinary,adParamInput,filesize+1,varBlob)); m_commandptr->Execute (NULL,NULL,adCmdUnknown); delete []m_pBuffer; } catch(...) { // delete []m_pBuffer; DisplayAdoError(); } return 0;} vc++ 关于文件拷贝的问题 急!! 对话框中如何建立滚动条控制? 定义IHTMLElement使用以后用不用release? 如何向vc6.0中添加不在system32之中的.dll文件? Win2000下插拔USB弹出警告 多文档中,如何用程序创建新文挡? 那个网站有小波变换的书,或者教程? ----------C++如何取得类实例的集合。-------- Win32做一个MFC多文档的界面 能帮我解决一个简单的问题吗?(只需指点一下) 把一个按钮SubClassWindow有什么讲究吗? 我的程序出现了“Debug Assertion Failed”,这是怎么回事?请进来看看!
CFile file;
file.Open(pathname,CFile::modeRead);
ULONG filelenth=file.GetLength();//读取文件长度 SAFEARRAYBOUND szbuffer[1];
SAFEARRAY FAR *psa;
_variant_t varChunk;
szbuffer->lLbound =0;
szbuffer->cElements =filelenth;
file.Read(szbuffer,filelenth);
file.Close();
{
try
{
//读取文件
char *lpBuffer;
char *temp;
DWORD m_nFileLen;
CFile f;
if(!f.Open("你的文件名",CFile::modeRead))
{
AfxMessageBox("打开文件失败.");
return FALSE;
}
m_nFileLen = f.GetLength();
lpBuffer = new char[m_nFileLen];
if(lpBuffer == NULL)
{
AfxMessageBox("分配文件缓冲区失败.");
return FALSE;
}
if (f.ReadHuge(lpBuffer,m_nFileLen)!= m_nFileLen)
{
AfxMessageBox("读文件到内存出错");
return FALSE;
}
f.Close();
//打开记录集
_RecordsetPtr m_recordsetSave;
m_recordsetSave.CreateInstance(__uuidof(Recordset));
_bstr_t SQLWord_t=(_bstr_t)csSelTable;
m_recordsetSave->Open(SQLWord_t,
_variant_t((IDispatch *)PLMConnection,TRUE),
adOpenDynamic,adLockOptimistic,
adCmdText);
_variant_t var;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
temp=lpBuffer;
if(lpBuffer)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = m_nFileLen;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); //创建SAFEARRAY对象
for (long i = 0; i < (long)m_nFileLen; i++)
{
SafeArrayPutElement (psa, &i,temp++);
//将lpBuffer指向的二进制数据保存到SAFEARRAY对象psa中
}
var.vt = VT_ARRAY | VT_UI1;//将var的类型设置为BYTE类型的数组
var.parray = psa; //为var变量赋值
m_recordsetSave->GetFields()->GetItem((_bstr_t)csFieldName)->AppendChunk(var);
//加入BLOB类型的数据
}
m_recordsetSave->Update(); //保存我们的数据到库中
delete []lpBuffer; }
catch (_com_error &e)
{
CString thismsg;
thismsg="保存出错:";
thismsg+=e.ErrorMessage();
AfxMessageBox(thismsg);
return FALSE;
}
return TRUE;
}
long AddFileToDatabase(CQSTreeItem *pitem, CString filename)
{
CQsofficeApp *papp=(CQsofficeApp*)AfxGetApp();
CString tablename=pitem->tablename;
try
{
m_commandptr.CreateInstance (__uuidof(Command));
m_pUserSet.CreateInstance (__uuidof(Recordset));
//然后添加到tid????中去
CFile file;
if (!file.Open (filename ,CFile::modeRead))
return 2;
DWORD filesize=file.GetLength ();
char *m_pBuffer;
m_pBuffer=new char[filesize+1];
if (file.ReadHuge (m_pBuffer,filesize)!=filesize)
return 1;
char* pwritebase=m_pBuffer;
VARIANT varBlob;
SAFEARRAY* psa;
SAFEARRAYBOUND safebound[1];
if(pwritebase)
{
safebound[0].lLbound =0;
safebound[0].cElements =filesize;
psa=SafeArrayCreate(VT_UI1,1,safebound);
for (long i=0;i<(long)filesize;i++)
SafeArrayPutElement(psa,&i,pwritebase++);
varBlob.vt =VT_ARRAY | VT_UI1;
varBlob.parray =psa;
}
sqltext="insert into ";
sqltext+=(_bstr_t)tablename;
sqltext+=" (id,parentid,bedelete,data) values (?,?,0,?)";
m_commandptr.CreateInstance (__uuidof(Command));
m_commandptr->ActiveConnection =papp->m_pConnection ;
m_commandptr->CommandText =sqltext;
m_commandptr->CommandType =adCmdText;
m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("tableitemid",adInteger,adParamInput,4,COleVariant((short)tableitemid)));
m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("parentid",adInteger,adParamInput,4,COleVariant((short)pitem->parentid )));
m_commandptr->Parameters ->Append (m_commandptr->CreateParameter ("data",/*adLongVarBinary*/adVarBinary,adParamInput,filesize+1,varBlob));
m_commandptr->Execute (NULL,NULL,adCmdUnknown); delete []m_pBuffer;
}
catch(...)
{
// delete []m_pBuffer;
DisplayAdoError();
} return 0;
}