新手面临的问题:首次运行正常,二次出异常.我估计是对象没有回收.具体如何作?
我作了一个函数,由一个按纽来调用:
void CheckDb()
{
//建立连接
_ConnectionPtr m_pConnection;
// 初始化COM,创建ADO连接等操作
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库Demo.mdb是否在当前路径下!");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//打开数据表
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM mobile where isSend=false", // 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox("找不到指定的数据表");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//读取数据表中数据
_variant_t var;
CString mobile,content;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
}
// 读入库中各字段
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("mobile");
if(var.vt != VT_NULL)
mobile = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("content");
if(var.vt != VT_NULL)
content = (LPCSTR)_bstr_t(var);
SendMsg(mobile,content);
///更新当前记录isSend为true
m_pRecordset->PutCollect("isSend", _variant_t(true));
m_pRecordset->Update();
AfxMessageBox( mobile + " --> "+content+"更新成功");
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//关闭DB
if(m_pRecordset->State)
m_pRecordset->Close();
m_pRecordset=NULL;
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
}
调用:void CTestDlg::OnBnClickedButton3()
{
CheckDb();
}
现在的问题是:运行时,我第一次点击这个按纽运行正常,第二次点击时就出现异常:
debug assertion failed!
program:f:\.......\oleinit.cpp
...
for information on how your program can cause an assertion .....
老兄们帮忙看看怎么回事啊.