MFC 出租记录系统编译成功了,可以显示数据库内容,但查询总是出现错误
Rent.exe 中的 0x753bd36f 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x001ae840 处的 _com_error。
这是按照VisualC++6.0开发指南的第二十章ADO数据库编程里源代码打进去的,因数据库连接问题,我把
m_pConnection->ConnectionString="File Name=my_data1.udl";
m_pConnection->ConnectionTimeout=20;
改为
hr=m_pConnection->Open("DSN=Rent;Provider=MSDASQL","","",adConnectUnspecified);
这样才能成功显示数据库内容了,但点击查询按钮还是出现错误,
调试了N久,尝试了很多方法,依然没有头绪,求大神指教,下面附上一些函数源代码,请大神帮忙发现错误,thank you
[code=C/C++][/code]
CString CRentDlg::QueryDVDName(CString DVDID)
{
_RecordsetPtr pDVDNameRecordset;
pDVDNameRecordset.CreateInstance(__uuidof(Recordset));
CString strValue;
_variant_t var;
CString vSQL;
vSQL.Format("select DVDName from tbDVDInfo where DVDID='%s'",DVDID);
try
{
HRESULT hr;
hr=pDVDNameRecordset->Open(_variant_t(vSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(!FAILED(hr))
{
var=pDVDNameRecordset->GetCollect("DVDName");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
}
else strValue="";
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
strValue="";
return strValue;//===
}
pDVDNameRecordset->Close();
pDVDNameRecordset=NULL;
return strValue;
}
void CRentDlg::InitComboCtr()
{
_RecordsetPtr pDVDNameRecordset;
pDVDNameRecordset.CreateInstance(__uuidof(Recordset));
try
{
pDVDNameRecordset->Open("SELECT DVDName FROM tbDVDInfo",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
CString strValue;
m_comboDVD.AddString("");
try
{
while(!pDVDNameRecordset->adoEOF)
{
var=pDVDNameRecordset->GetCollect("DVDName");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_comboDVD.AddString(strValue);
pDVDNameRecordset->MoveNext();
}
pDVDNameRecordset->Close();
pDVDNameRecordset=NULL;
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
}
void CRentDlg::OnBnClickedButtonQuery()
{
UpdateData(TRUE);
_RecordsetPtr pQueryRecordset;
pQueryRecordset.CreateInstance(__uuidof(Recordset));
CString strDateFrom,strDateTo,strDVD;
CString strSQL,temp;
m_comboDVD.GetWindowText(strDVD);
if((!m_check_Date)&&(m_name.IsEmpty())&&(strDVD.IsEmpty()))
strSQL="SELECT * FROM tbRentInfo";
else
strSQL="SELECT * FROM tbRentInfo where";
if(m_check_Date)
{
CTime timeFrom,timeTo;
m_DateFrom.GetTime(timeFrom);
m_DateTo.GetTime(timeTo);
m_DateFrom.GetWindowText(strDateFrom);
m_DateTo.GetWindowText(strDateTo);
if(timeFrom.GetMonth()>timeTo.GetMonth())
{
MessageBox("Date set is wrong!");
return;
}
else if(timeFrom.GetMonth()==timeTo.GetMonth())
{
if(timeFrom.GetDay()>timeTo.GetDay())
{
MessageBox("Date set is wrong!");
return;
}
}
temp.Format("Date>='%s' and Date<='%s'",strDateFrom,strDateTo);
strSQL+=temp;
}
if(!m_name.IsEmpty())
{
if(m_check_Date)
temp.Format("and Name='%s'",m_name);
else
temp.Format("Name='%s'",m_name);
strSQL+=temp;
}
if(!strDVD.IsEmpty())
{
if((!m_check_Date)&&(m_name.IsEmpty()))
temp.Format("DVDID=%d",QueryDVDID(strDVD));//====!!!!!!!!!!!!!!!!!
else
temp.Format("and DVDID=%d",QueryDVDID(strDVD));
strSQL+=temp;
}
try
{
pQueryRecordset->Open(_variant_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
if((pQueryRecordset->BOF)&&(pQueryRecordset->adoEOF))
MessageBox("there is no record");
else
{
_variant_t var;
CString strValue;
int curItem=0;
m_rentInfoList.DeleteAllItems();
try
{
while(!pQueryRecordset->adoEOF)
{
var=pQueryRecordset->GetCollect("ID");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_rentInfoList.InsertItem(curItem,strValue);
var=pQueryRecordset->GetCollect("Name");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_rentInfoList.SetItemText(curItem,1,strValue);
var=pQueryRecordset->GetCollect("DVDID");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_rentInfoList.SetItemText(curItem,2,QueryDVDName(strValue));
var=pQueryRecordset->GetCollect("Date");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_rentInfoList.SetItemText(curItem,3,strValue);
pQueryRecordset->MoveNext();
curItem++;
}
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
}
pQueryRecordset->Close();
pQueryRecordset=NULL;
// TODO: 在此添加控件通知处理程序代码
}
int CRentDlg::QueryDVDID(CString dvdName)
{
_RecordsetPtr pDVDIDRecordset;
pDVDIDRecordset.CreateInstance(__uuidof(Recordset));
int value;
CString strValue,strSQL;
strSQL.Format("select DVDID from tbDVDInfo where DVDName='%s'",dvdName);
try
{
pDVDIDRecordset->Open(_variant_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
value=-1;
return value;
}
_variant_t var;
var=pDVDIDRecordset->GetCollect("DVDID");
if(var.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(var);
value=atoi(strValue);//-===
}
else value=-1;
pDVDIDRecordset->Close();
pDVDIDRecordset=NULL;
return value;
}
[解决办法]
加异常捕获看是什么错误
try{//你的ADO代码}catch (_com_error& e){CString strMsg;strMsg.Format(_T("错误描述:%s\n错误消息%s"), (LPCTSTR)e.Description(), (LPCTSTR)e.ErrorMessage());AfxMessageBox(strMsg);}