我写了一个CADO类,我在构造函数里初始化com组件,创建连接对象并实例化。在析构函数里清除com组件,并关闭连接对象。我还写个一个函数_Recoreset ExcuteSQL(CString str)
里面定义了一个记录集对象并且实例化,然后将参数转化成SQL 语句,打开记录集并且把记录集返回。
CADO::CADO()
{
//初始化COM库
::CoInitialize(NULL);
try
{
//创建连接对象实例
m_pConnection.CreateInstance("ADODB.Connection");
//设置连接字符串
CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\uid=;pwd=;DBQ=xueshengchengji.mdb;";
//使用Open方法连接数据库
m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
}
CADO::~CADO()
{
//关闭记录集和连接
m_pConnection->Close( );
m_pConnection.Release( );
//清除COM库
::CoUninitialize( );
}
_RecordsetPtr CADO::ExecuteSQL(CString bstrSQL)
{
_RecordsetPtr pRecordset (__uuidof(Recordset));
try
{
pRecordset=m_pConnection->Execute(_bstr_t(bstrSQL), NULL, adCmdText);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
return pRecordset;
}
在一个列表框中我想单击列表的某一列,就按此类进行排序(升序或降序)显示。于是我写了如图函数。点击一次实现了排序,但是再次点击时就显示 IDispatch error #3092。SQL语句经测试没有错误。我想是因为在函数里再次调用ExcuteSQL函数和AddToList 函数前没有关闭记录集或者ADO对象(m_ado是CADO类的对象)造成的吧。请问该怎么解决?
void CTeacherDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
try
{
int colum;
colum = pNMListView->iSubItem;
switch(colum)
{
case 0:
if(isAsc)
{
bstrSQL+=" order by ClassArrange.CourseID DESC";
isAsc=FALSE;
}
else
{
bstrSQL+=" order by ClassArrange.CourseID ASC";
isAsc=TRUE;
}
//m_Recordset.Requery();
MessageBox(bstrSQL);
m_List.DeleteAllItems();
AddToList(bstrSQL);
break;
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
}
void CTeacherDlg::AddToList(CString bstrSQL)
{
try
{
//打开记录集
m_pRecordset=m_ado.ExecuteSQL(bstrSQL);
while(!m_pRecordset->adoEOF)
{
m_List.InsertItem(0,"");
m_List.SetItemText(0,0,(char*)(_bstr_t)m_pRecordset->GetCollect("CourseID"));
m_List.SetItemText(0,1,(char*)(_bstr_t)m_pRecordset->GetCollect("CourseName"));
m_List.SetItemText(0,2,(char*)(_bstr_t)m_pRecordset->GetCollect("ClassName"));
m_List.SetItemText(0,3,(char*)(_bstr_t)m_pRecordset->GetCollect("StudentID"));
m_List.SetItemText(0,4,(char*)(_bstr_t)m_pRecordset->GetCollect("StudentName"));
m_List.SetItemText(0,5,(char*)(_bstr_t)m_pRecordset->GetCollect("MajorName"));
m_List.SetItemText(0,6,(char*)(_bstr_t)m_pRecordset->GetCollect("Grade"));
//将记录集指针移动到下一条记录
m_pRecordset->MoveNext();
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
}
//以下是SQL语句
//设置查询字符串
bstrSQL.Format("SELECT Teacher.TeacherID, ClassArrange.CourseID, Course.CourseName, Class.ClassName, \
Grade.StudentID, Student.StudentName, Major.MajorName, Grade.Grade\
FROM Teacher INNER JOIN (((Major INNER JOIN Class ON Major.MajorID = Class.Major)INNER JOIN Student ON \
Class.ClassID = Student.ClassID) INNER JOIN ((Course INNER JOIN ClassArrange ON Course.CourseID = \
ClassArrange.CourseID) INNER JOIN Grade ON Course.CourseID = Grade.CourseID) ON Student.StudentID = Grade.StudentID)\
ON Teacher.TeacherID = ClassArrange.TeacherID WHERE (((Teacher.TeacherID)='%s')) ",m_Name1);
错误信息为:
IDispatch error#3092
Unkoown error 0x800A0E78
当第一次运行函数
void CTeacherDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult) 时没有错误,再次进入就有问题了。