关于ADO中纪录集对象循环关闭的问题 数据库连接关闭了记录集关闭了再操作时是否有打开?还是不怎么明白代码发给我看看[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CString dbname = "g.mdb"; _ConnectionPtr m_pConnect2;//声明连接指针对象 _RecordsetPtr m_pSet3;//声明数据集对象 CString strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbname;//连接字符串 HRESULT hr; hr = m_pConnect2.CreateInstance(__uuidof(Connection)); if(!SUCCEEDED(hr)) { AfxMessageBox("database error"); return ; } try{ m_pConnect2->Open(_bstr_t(strConnection),"","",adModeUnknown); } catch(_com_error &e) { AfxMessageBox(e.ErrorMessage()); } m_pSet3.CreateInstance(__uuidof(Recordset));//创建对象 CString sqltext = ""; CString buffer = ""; CStringArray dest; CJSApp *app = (CJSApp*)AfxGetApp(); CStdioFile *file = new CStdioFile; CFileDialog dlg(TRUE); if(dlg.DoModal() != IDOK) return; if(!file->Open(dlg.GetPathName(),CFile::modeRead)) { AfxMessageBox("文件不能打开!"); return; } file->SeekToBegin(); file->ReadString(buffer); while(file->ReadString(buffer)) { decodeCString(buffer,dest,','); sqltext = "SELECT * FROM DANWEI WHERE CODE = '";sqltext += dest[0];sqltext += "'"; try{ HRESULT hr = m_pSet3->Open(_variant_t(sqltext),m_pConnect2.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText); } catch(_com_error &e) { AfxMessageBox(e.ErrorMessage()); } if(m_pSet3->adoBOF) { if(m_pSet3) { m_pSet3->Close(); } continue; } m_pSet3->MoveFirst(); for(int i =0; i < app->s_power_data.GetSize(); i++) { if(dest[i+1].GetLength() == 0) dest[i+1] = " "; m_pSet3->PutCollect((const _variant_t)app->s_power_data.GetAt(i),_variant_t(dest[i+1])); } m_pSet3->Update(); m_pSet3->Close(); } if(m_pConnect2) { m_pConnect2->Close();m_pConnect2 = NULL; } m_pSet3 = NULL; AfxMessageBox("数据导入成功!");这就是函数A的代码再操作时肯定有打开了B函数单独执行是没有问题的,但先A后B就出错(不仅仅是B函数,其他数据库操作也一样出错) 可否让connection设置为全局只打开一次 那应该没什么关系吧。比如函数B:{StartDB() ..... EndDB() }函数C:{StartDB() ..... EndDB() }无论先执行B再执行C还是先执行C再执行B都没问题,但只要执行了A再执行B或C就不行。执行了B或C后再执行A也不行感觉就是A里面set打开关闭多次后就不行了 说不定connection关闭并非马上关闭 事实上也确实没用,我测试过了,设置为类的成员变量,结果还是一样。(ABC均为类的成员函数) 你把catch(_com_error &e) { AfxMessageBox(e.ErrorMessage()); } 改成catch(_com_error &e) { AfxMessageBox(e.Description()); }试试先.. 智能卡 SELECT 命令看不懂 如何修改调用MFC的版本 如何让CDialog中的Flash背景透明? CAsyncSocket类中Connect的疑问 对话框获取View指针时内存出错 ActiveX在Maxthon显示正常,在IE下不能显示 急!急!急!哪位好心人帮帮小弟?小弟万分感谢!!!!!!!!!! 刚学VC大家帮忙--在线等 ========500分寻ListCtrl增强类,在线等候,谢谢!!! ======== 如何让机器发“当”声? dll下如何实现串口中断通讯。。。急急急 VC 用shfileoperaion 复制多个指定的文件,如果动态分配源路径的字符数,急!
_ConnectionPtr m_pConnect2;//声明连接指针对象
_RecordsetPtr m_pSet3;//声明数据集对象
CString strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbname;//连接字符串
HRESULT hr;
hr = m_pConnect2.CreateInstance(__uuidof(Connection));
if(!SUCCEEDED(hr))
{
AfxMessageBox("database error");
return ;
}
try{
m_pConnect2->Open(_bstr_t(strConnection),"","",adModeUnknown);
}
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
}
m_pSet3.CreateInstance(__uuidof(Recordset));//创建对象
CString sqltext = ""; CString buffer = ""; CStringArray dest;
CJSApp *app = (CJSApp*)AfxGetApp();
CStdioFile *file = new CStdioFile;
CFileDialog dlg(TRUE);
if(dlg.DoModal() != IDOK)
return;
if(!file->Open(dlg.GetPathName(),CFile::modeRead))
{
AfxMessageBox("文件不能打开!");
return;
}
file->SeekToBegin();
file->ReadString(buffer);
while(file->ReadString(buffer))
{
decodeCString(buffer,dest,',');
sqltext = "SELECT * FROM DANWEI WHERE CODE = '";sqltext += dest[0];sqltext += "'";
try{
HRESULT hr = m_pSet3->Open(_variant_t(sqltext),m_pConnect2.GetInterfacePtr(),
adOpenStatic,
adLockOptimistic,
adCmdText);
}
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
}
if(m_pSet3->adoBOF)
{
if(m_pSet3)
{
m_pSet3->Close();
}
continue;
}
m_pSet3->MoveFirst();
for(int i =0; i < app->s_power_data.GetSize(); i++)
{
if(dest[i+1].GetLength() == 0)
dest[i+1] = " ";
m_pSet3->PutCollect((const _variant_t)app->s_power_data.GetAt(i),_variant_t(dest[i+1]));
}
m_pSet3->Update();
m_pSet3->Close();
}
if(m_pConnect2)
{
m_pConnect2->Close();m_pConnect2 = NULL;
} m_pSet3 = NULL;
AfxMessageBox("数据导入成功!");这就是函数A的代码
再操作时肯定有打开了
B函数单独执行是没有问题的,但先A后B就出错(不仅仅是B函数,其他数据库操作也一样出错)
只打开一次
比如函数B:
{
StartDB()
.....
EndDB()
}
函数C:
{
StartDB()
.....
EndDB()
}
无论先执行B再执行C还是先执行C再执行B都没问题,
但只要执行了A再执行B或C就不行。执行了B或C后再执行A也不行
感觉就是A里面set打开关闭多次后就不行了
connection关闭并非马上关闭
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
}
改成
catch(_com_error &e)
{
AfxMessageBox(e.Description());
}
试试先..