MFC 出租记录系统编译成功了,可以显示数据库内容,但查询总是出现异常
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));//====!!!!!!!!!!!!!!!!!
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));//====!!!!!!!!!!!!!!!!!