資源描述:
《MFC ADO數(shù)據(jù)庫》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、任務(wù)5數(shù)據(jù)庫編程一、綜合課程實(shí)習(xí)目的1.掌握模式VisualC++對于數(shù)據(jù)庫的操作。2.掌握常用控件的使用方法。3.熟悉數(shù)據(jù)庫操作語句(insert/delete/select)。4.熟悉操作數(shù)據(jù)庫的對象。二、綜合課程實(shí)習(xí)內(nèi)容創(chuàng)建一個(gè)MFC應(yīng)用程序TeacherMIS,進(jìn)行教師數(shù)據(jù)的顯示。三、綜合課程實(shí)習(xí)指導(dǎo)1.新建對話框工程,命名為TeacherMIS,刪除TeacherMIS對話框中除了“確定”按鈕的所有控件,對話框的字體為:宋體9號2.導(dǎo)入ADO:在stdafx.h文件中添加:#import"c:programfilescommo
2、nfilessystemadomsado15.dll"no_namespacerename("EOF","adoEOF")3.在工程項(xiàng)目類CTeacherMISApp中添加連接和記錄集變量:_RecordsetPtrm_pRec;_ConnectionPtrm_pConn;4.在CTeacherMISApp類的InitInstance函數(shù)中添加如下代碼:::CoInitialize(NULL);try{m_pConn.CreateInstance(__uuidof(Connection));//創(chuàng)建連接對象m_pConn->Open(
3、"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db1.mdb","","",adModeUnknown);///連接數(shù)據(jù)庫}catch(_com_errore){AfxMessageBox("數(shù)據(jù)庫連接失敗!");returnfalse;}m_pRec.CreateInstance(__uuidof(Recordset));1.在CTeacherMISApp類中添加虛函數(shù)ExitInstance()然后編輯代碼intCTeacherMISApp::ExitInstance(){//TODO:Add
4、yourspecializedcodehereand/orcallthebaseclassif(m_pConn->State)m_pConn->Close();m_pConnection=NULL;returnCWinApp::ExitInstance();}2.在CTeacherMISApp類的頭文件中添加一個(gè)工程類對象:externCTeacherMISApptheApp;3.在CTeacherMISDlg::OnOK()函數(shù)中要釋放連接對象和記錄集對象voidCTeacherMISDlg::OnOK(){//TODO:Addextra
5、validationhereif(theApp.m_pConn->State)theApp.m_pRec->Close();theApp.m_pRec.Release();if(theApp.m_pConn->State)theApp.m_pConn->Close();theApp.m_pConn.Release();::CoUninitialize();CDialog::OnOK();}4.添加一個(gè)“登陸”對話框:宋體9號對該對話框建立類向?qū)?,并在AppWizzard中添加兩個(gè)編輯框和“登陸”“取消”按鈕消息以及OnInitDialog(
6、),OnPain()消息:在OnPaint()函數(shù)中添加如下代碼:voidCLoginDlg::OnPaint(){CFontNewFont;NewFont.CreateFont(30,0,0,0,700,TRUE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH
7、FF_SWISS,"楷體");dc.SetBkMode(TRANSPARENT);CFont*pOldFont=dc.SelectObject(&Ne
8、wFont);dc.SetTextColor(RGB(0,0,255));dc.TextOut(60,20,"教職工信息管理系統(tǒng)");dc.SelectObject(pOldFont);}在“登陸”按鈕消息中添加如下代碼:voidCLoginDlg::OnOK(){//TODO:AddextravalidationhereUpdateData(true);_variant_tv_strQuery,v_strCount;v_strQuery="selectcount(*)fromLoginUserwhereUser_Name='"+m_str
9、UserName+"'andUser_Psw='"+m_strUserPsw+"'";//"selectcount(*)asCountUserfromLoginUserwhere