下面一段代码是在响应点击Button的函数:我点击此控件就把数据库里的数据显示在List Ctrl控件里,
而且可以正常显示。但是,当我再次点击就出现一个对话框,说应用程序错误,"0x00402466"指令引用的"0x00000000"内存。该内存不能为"read",终止点确定,调试点取消。请问是什么原因啊?
void CADOTest4Dlg::OnButtonShow()
{
// TODO: Add your control notification handler code here HRESULT hr;
_variant_t var;
CString strValue;
int index=0;
hr=m_pRec.CreateInstance(__uuidof(Recordset));
try
{
hr=m_pRec->Open(_variant_t("tbRentInfo"),
m_pConn.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdTable);
if(SUCCEEDED(hr))
{
while(!m_pRec->adoEOF)
{
var=m_pRec->GetCollect("ID");
strValue=(LPCSTR)_bstr_t(var);
m_List.InsertItem(index,strValue);
var=m_pRec->GetCollect("DVDID");
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText(index,1,strValue);
var=m_pRec->GetCollect("Name");
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText(index,2,strValue);
var=m_pRec->GetCollect("Date");
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText(index,3,strValue);
m_pRec->MoveNext();
index++;
}
}
else
{
AfxMessageBox("Open recordset fail!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
m_pRec->Close();
m_pConn->Close();
m_pRec.Release();
m_pRec=NULL;
m_pConn.Release();
}
而且可以正常显示。但是,当我再次点击就出现一个对话框,说应用程序错误,"0x00402466"指令引用的"0x00000000"内存。该内存不能为"read",终止点确定,调试点取消。请问是什么原因啊?
void CADOTest4Dlg::OnButtonShow()
{
// TODO: Add your control notification handler code here HRESULT hr;
_variant_t var;
CString strValue;
int index=0;
hr=m_pRec.CreateInstance(__uuidof(Recordset));
try
{
hr=m_pRec->Open(_variant_t("tbRentInfo"),
m_pConn.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdTable);
if(SUCCEEDED(hr))
{
while(!m_pRec->adoEOF)
{
var=m_pRec->GetCollect("ID");
strValue=(LPCSTR)_bstr_t(var);
m_List.InsertItem(index,strValue);
var=m_pRec->GetCollect("DVDID");
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText(index,1,strValue);
var=m_pRec->GetCollect("Name");
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText(index,2,strValue);
var=m_pRec->GetCollect("Date");
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText(index,3,strValue);
m_pRec->MoveNext();
index++;
}
}
else
{
AfxMessageBox("Open recordset fail!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
m_pRec->Close();
m_pConn->Close();
m_pRec.Release();
m_pRec=NULL;
m_pConn.Release();
}
m_pConn.Release();
m_pConn最后都被你关闭了,你重新点击按钮当然会出错啦.你可以在hr=m_pRec.CreateInstance(__uuidof(Recordset)); 后面添加检查m_pConn是不是关闭,如果被关闭,则打开它.然后再进行其他操作.