資源描述:
《使用ado及l(fā)istctrl控件操作access數(shù)據(jù)庫(kù)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、使用ADO及l(fā)istCtrl控件操作ACCESS數(shù)據(jù)庫(kù)第1步:顯示數(shù)據(jù)記錄●創(chuàng)建基于對(duì)話框的應(yīng)用程序AdoList?!窦尤隠istControl控件,設(shè)置屬性如下:ID值為IDC_LIST1View列表選ReportSingleselection選中(限制列表項(xiàng)單選)●在StdAfx.h文件加入下面命令:#import"C:ProgramFilesCommonFilesSystemadomsado15.dll"no_namespacerename("EOF","adoEOF")●使用類向?qū)镮DC_LIST1列表視控件綁定控件型變量m_
2、list●在CadoListDlg類添加下面三個(gè)成員變量_ConnectionPtrm_pConnection;_RecordsetPtrm_pRecordset;●在BOOLCAdoListDlg::OnInitDialog()窗口初始化函數(shù)中加入下面命令://TODO:AddextrainitializationhereCoInitialize(NULL);try{HRESULThr;m_pConnection.CreateInstance("ADODB.Connection");hr=m_pConnection->Open("Provide
3、r=Microsoft.Jet.OLEDB.4.0;DataSource=stu1.mdb","","",adModeUnknown);}catch(_com_errore){CStringerrormessage;errormessage.Format("連接數(shù)據(jù)庫(kù)失敗!錯(cuò)誤信息:%s",e.ErrorMessage());AfxMessageBox(errormessage);}try{m_pRecordset.CreateInstance(__uuidof(Recordset));m_pRecordset->Open("SELECT*F
4、ROMstudent",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);}catch(_com_errore){CStringerrormessage;errormessage.Format("連接數(shù)據(jù)庫(kù)表失敗!錯(cuò)誤信息:%s",e.ErrorMessage());AfxMessageBox(errormessage);}m_list.SetExtendedStyle(LVS_EX_FLATSB
5、LVS_EX_FULLROWSELECT//點(diǎn)擊
6、一項(xiàng)時(shí),整行都為選中狀態(tài)
7、LVS_EX_GRIDLINES);//設(shè)置網(wǎng)格線m_list.InsertColumn(0,"學(xué)號(hào)",LVCFMT_LEFT,80);m_list.InsertColumn(1,"姓名",LVCFMT_LEFT,50);m_list.InsertColumn(2,"年齡",LVCFMT_RIGHT,36);disp();//顯示數(shù)據(jù)庫(kù)數(shù)據(jù)記錄returnTRUE;//returnTRUEunlessyousetthefocustoacontrol●在CAdoListDlg類添加下面成員函數(shù)disp()(并在類定義中聲明
8、)顯示數(shù)據(jù)庫(kù)記錄:voidCAdoListDlg::disp(){m_list.DeleteAllItems();inti=0;m_pRecordset->MoveFirst();while(!m_pRecordset->adoEOF)//adoEOF是末尾標(biāo)識(shí){_variant_ttheValue;CStringid,name,age;theValue=m_pRecordset->GetCollect("StuID");//獲取學(xué)號(hào)if(theValue.vt!=VT_NULL)id=(char*)_bstr_t(theValue);theVa
9、lue=m_pRecordset->GetCollect("StuName");//獲取姓名if(theValue.vt!=VT_NULL)name=(char*)_bstr_t(theValue);theValue=m_pRecordset->GetCollect("age");//獲取年齡if(theValue.vt!=VT_NULL)age.Format("%d",theValue.iVal);m_list.InsertItem(i,"");m_list.SetItemText(i,0,id);m_list.SetItemText(i,1,
10、name);m_list.SetItemText(i,2,age);m_pRecordset->MoveNext();//移動(dòng)到下一行(