我寫了一個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) 時沒有錯誤,再次進入就有問題了。
你可以試一下在 void CTeacherDlg::AddToList(CString bstrSQL)裡邊執行完操作後
把m_pRecordset釋放。