MFC使用ADO讀寫Access數(shù)據(jù)庫(kù)實(shí)例-免費(fèi)_第1頁
MFC使用ADO讀寫Access數(shù)據(jù)庫(kù)實(shí)例-免費(fèi)_第2頁
MFC使用ADO讀寫Access數(shù)據(jù)庫(kù)實(shí)例-免費(fèi)_第3頁
MFC使用ADO讀寫Access數(shù)據(jù)庫(kù)實(shí)例-免費(fèi)_第4頁
MFC使用ADO讀寫Access數(shù)據(jù)庫(kù)實(shí)例-免費(fèi)_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、MFC使用ADO讀寫Access數(shù)據(jù)庫(kù)實(shí)例 ADO(ActiveX Data Object)是Microsoft數(shù)據(jù)庫(kù)應(yīng)用程序開發(fā)的新接口,是建立在OLE DB之上的高層數(shù)據(jù)庫(kù)訪問技術(shù),即使你對(duì)OLE DB,COM不了解也能輕松對(duì)付ADO,因?yàn)樗浅:?jiǎn)單易用,甚至比你以往所接觸的ODBC API、DAO、RDO都要容易使用,并不失靈活性。本文詳細(xì)地介紹在Visual C+開發(fā)環(huán)境下如何使用ADO來進(jìn)行數(shù)據(jù)庫(kù)應(yīng)用程序開發(fā),并給出示例代碼。為了使讀者朋友都能測(cè)試本例提供的代碼,我們采用Access數(shù)據(jù)庫(kù),您可以直接在我們提供的示例代碼中找到這個(gè)test.mdb一、實(shí)現(xiàn)方法萬事開頭難,任何一種新技術(shù)

2、對(duì)于初學(xué)者來說最重要的還是"入門",掌握其要點(diǎn)。讓我們來看看ADO數(shù)據(jù)庫(kù)開發(fā)的基本流程吧!它的基本步驟如下:(1)初始化COM庫(kù),引入ADO庫(kù)定義文件(2)用Connection對(duì)象連接數(shù)據(jù)庫(kù)(3)利用建立好的連接,通過Connection、Command對(duì)象執(zhí)行SQL命令,或利用Recordset對(duì)象取得結(jié)果記錄集進(jìn)行查詢、處理。(4)使用完畢后關(guān)閉連接釋放對(duì)象。下面我們將詳細(xì)介紹上述步驟并給出相關(guān)代碼。1、COM庫(kù)的初始化我們可以使用AfxOleInit()來初始化COM庫(kù),這項(xiàng)工作通常在CWinApp:InitInstance()的重載函數(shù)中完成,請(qǐng)看如下代碼:BOO

3、L CADOTest1App:InitInstance()AfxOleInit();. 2、用#import指令引入ADO類型庫(kù)為了引入ADO類型庫(kù),需要在項(xiàng)目的stdafx.h文件中加入如下語句:#import "c:program filescommon filessystemadomsado15.dll" no_namespace rename("EOF","adoEOF")這一語句有何作用呢?其最終作用同我們已經(jīng)十分熟悉的#include類似,編譯的時(shí)候系統(tǒng)會(huì)為我們生成msado15.tlh,ado15.tli兩個(gè)C+頭文件來

4、定義ADO庫(kù)。需要讀者朋友注意的是:您的開發(fā)環(huán)境中msado15.dll不一定在這個(gè)目錄下,請(qǐng)按實(shí)際情況修改;在編譯的時(shí)候可能會(huì)出現(xiàn)如下警告,對(duì)此微軟在MSDN中作了說明,并建議我們不要理會(huì)這個(gè)警告:msado15.tlh(405) : warning C4146: unary minus operator applied to unsigned type, result still unsigned。3、創(chuàng)建Connection對(duì)象并連接數(shù)據(jù)庫(kù)為了首先我們需要添加一個(gè)指向Connection對(duì)象的指針_ConnectionPtr m_pConnection,下面的代碼演示了如何創(chuàng)建Conne

5、ction對(duì)象實(shí)例及如何連接數(shù)據(jù)庫(kù)并進(jìn)行異常捕捉:BOOL CADOTest1Dlg:OnInitDialog()CDialog:OnInitDialog();HRESULT hr;tryhr = m_pConnection.CreateInstance("ADODB.Connection");/創(chuàng)建Connection對(duì)象if(SUCCEEDED(hr)hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","",&quo

6、t;",adModeUnknown);/連接數(shù)據(jù)庫(kù)/上面一句中連接字串中的Provider是針對(duì)ACCESS2000環(huán)境的,對(duì)于ACCESS97,/需要改為:Provider=Microsoft.Jet.OLEDB.3.51;catch(_com_error e)/捕捉異常CString errormessage;errormessage.Format("連接數(shù)據(jù)庫(kù)失敗!rn錯(cuò)誤信息:%s",e.ErrorMessage();AfxMessageBox(errormessage);/顯示錯(cuò)誤信息在這段代碼中我們是通過Connection對(duì)象的Open方法來進(jìn)行連接

7、數(shù)據(jù)庫(kù)的,下面是該方法的原型:HRESULT Connection15:Open ( _bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options );上述函數(shù)中參數(shù)ConnectionString為連接字串;參數(shù)UserID是用戶名;參數(shù)Password是登陸密碼;參數(shù)Options是連接選項(xiàng),用于指定Connection對(duì)象對(duì)數(shù)據(jù)的更新許可權(quán),一般情況下Options可以是如下幾個(gè)常量:adModeUnknown:缺省。當(dāng)前的許可權(quán)未設(shè)置adModeRead:只讀adModeWrite:只寫adModeRe

8、adWrite:可以讀寫adModeShareDenyRead:阻止其它Connection對(duì)象以讀權(quán)限打開連接adModeShareDenyWrite:阻止其它Connection對(duì)象以寫權(quán)限打開連接adModeShareExclusive:阻止其它Connection對(duì)象以讀寫權(quán)限打開連接adModeShareDenyNone:阻止其它Connection對(duì)象以任何權(quán)限打開連接我們給出一些常用的連接方式供大家參考:(1)通過JET數(shù)據(jù)庫(kù)引擎對(duì)ACCESS2000數(shù)據(jù)庫(kù)的連接:m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.

9、4.0;Data Source=C:test.mdb","","",adModeUnknown);(2)通過DSN數(shù)據(jù)源對(duì)任何支持ODBC的數(shù)據(jù)庫(kù)進(jìn)行連接:m_pConnection->Open("Data Source=adotest;UID=sa;PWD=;","","",adModeUnknown);(3)不通過DSN對(duì)SQL SERVER數(shù)據(jù)庫(kù)進(jìn)行連接: m_pConnection->Open("driver=SQL Server;Server=127.0

10、.0.1;DATABASE=vckbase;UID=sa;PWD=139","","",adModeUnknown);其中Server是SQL服務(wù)器的名稱,DATABASE是庫(kù)的名稱。Connection對(duì)象除Open()方法外還有許多方法,我們先介紹Connection對(duì)象中兩個(gè)有用的屬性ConnectionTimeOut與State。ConnectionTimeOut用來設(shè)置連接的超時(shí)時(shí)間,需要在Open之前調(diào)用,例如:m_pConnection->ConnectionTimeout = 5;/設(shè)置超時(shí)時(shí)間為5秒m_pConnecti

11、on->Open("Data Source=adotest;","","",adModeUnknown);State屬性指明當(dāng)前Connection對(duì)象的狀態(tài),0表示關(guān)閉,1表示已經(jīng)打開,我們可以通過讀取這個(gè)屬性來作相應(yīng)的處理,例如:if(m_pConnection->State)m_pConnection->Close(); /如果已經(jīng)打開了連接則關(guān)閉它4、執(zhí)行SQL命令并取得結(jié)果記錄集為了取得結(jié)果記錄集,我們定義一個(gè)指向Recordset對(duì)象的指針:_RecordsetPtr m_pRecordset;并為其創(chuàng)建

12、Recordset對(duì)象的實(shí)例: m_pRecordset.CreateInstance("ADODB.Recordset"),SQL命令的執(zhí)行可以采用多種形式,下面我們一一進(jìn)行闡述。(1)利用Connection對(duì)象的Execute方法執(zhí)行SQL命令Execute()方法的原型如下所示:_RecordsetPtr Connection15:Execute ( _bstr_t CommandText, VARIANT * RecordsAffected, long Options )其中CommandText是命令字串,通常是SQL命令。參數(shù)RecordsAffected是操

13、作完成后所影響的行數(shù), 參數(shù)Options表示CommandText中內(nèi)容的類型,Options可以取如下值之一:adCmdText表明CommandText是文本命令;adCmdTable表明CommandText是一個(gè)表名;adCmdProc表明CommandText是一個(gè)存儲(chǔ)過程;adCmdUnknown表明CommandText內(nèi)容未知。Execute()函數(shù)執(zhí)行完后返回一個(gè)指向記錄集的指針,下面我們給出具體代碼并作說明:_variant_t RecordsAffected;/執(zhí)行SQL命令:CREATE TABLE創(chuàng)建表格users,users包含四個(gè)字段:整形ID,字符串usern

14、ame,整形old,日期型birthdaym_pConnection->Execute("CREATE TABLE users(ID INTEGER,username TEXT,old INTEGER,birthday DATETIME)",&RecordsAffected,adCmdText);/往表格里面添加記錄m_pConnection->Execute("INSERT INTO users(ID,username,old,birthday) VALUES (1, 'Washington',25,'1970/1/1

15、')",&RecordsAffected,adCmdText);/將所有記錄old字段的值加一m_pConnection->Execute("UPDATE users SET old = old+1",&RecordsAffected,adCmdText);/執(zhí)行SQL統(tǒng)計(jì)命令得到包含記錄條數(shù)的記錄集m_pRecordset = m_pConnection->Execute("SELECT COUNT(*) FROMusers",&RecordsAffected,adCmdText);_variant

16、_t vIndex = (long)0;_variant_t vCount = m_pRecordset->GetCollect(vIndex);/取得第一個(gè)字段的值放入vCount變量m_pRecordset->Close();/關(guān)閉記錄集CString message;message.Format("共有%d條記錄",vCount.lVal);AfxMessageBox(message);/顯示當(dāng)前記錄條數(shù)(2)利用Command對(duì)象來執(zhí)行SQL命令_CommandPtr m_pCommand;m_pCommand.CreateInstance("

17、ADODB.Command");_variant_t vNULL;vNULL.vt = VT_ERROR;vNULL.scode = DISP_E_PARAMNOTFOUND;/定義為無參數(shù)m_pCommand->ActiveConnection = m_pConnection;/非常關(guān)鍵的一句,將建立的連接賦值給它m_pCommand->CommandText = "SELECT * FROM users"/命令字串m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdTex

18、t);/執(zhí)行命令取得記錄集在這段代碼中我們只是用Command對(duì)象來執(zhí)行了SELECT查詢語句,Command對(duì)象在進(jìn)行存儲(chǔ)過程的調(diào)用中能真正體現(xiàn)它的作用。下次我們將詳細(xì)介紹。(3)直接用Recordset對(duì)象進(jìn)行查詢?nèi)〉糜涗浖?,例如:m_pRecordset->Open("SELECT * FROM users",_variant_t(IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);Open()方法的原型如下:HRESULT Recordset15:Open ( con

19、st _variant_t & Source, const _variant_t & ActiveConnection, enum CursorTypeEnum CursorType, enum LockTypeEnum LockType, long Options )上述函數(shù)中參數(shù)Source是數(shù)據(jù)查詢字符串;參數(shù)ActiveConnection是已經(jīng)建立好的連接(我們需要用Connection對(duì)象指針來構(gòu)造一個(gè)_variant_t對(duì)象);參數(shù)CursorType光標(biāo)類型,它可以是以下值之一;請(qǐng)看這個(gè)枚舉結(jié)構(gòu):enum CursorTypeEnumadOpenUnspecif

20、ied = -1,/不作特別指定adOpenForwardOnly = 0,/前滾靜態(tài)光標(biāo)。這種光標(biāo)只能向前瀏覽記錄集,比如用MoveNext向前滾動(dòng),這種方式可以提高瀏覽速度。但諸如BookMark,RecordCount,AbsolutePosition,AbsolutePage都不能使用adOpenKeyset = 1,/采用這種光標(biāo)的記錄集看不到其它用戶的新增、刪除操作,但對(duì)于更新原有記錄的操作對(duì)你是可見的。adOpenDynamic = 2,/動(dòng)態(tài)光標(biāo)。所有數(shù)據(jù)庫(kù)的操作都會(huì)立即在各用戶記錄集上反應(yīng)出來。adOpenStatic = 3/靜態(tài)光標(biāo)。它為你的記錄集產(chǎn)生一個(gè)靜態(tài)備份,但其它

21、用戶的新增、刪除、更新操作對(duì)你的記錄集來說是不可見的。;參數(shù)LockType表示數(shù)據(jù)庫(kù)的鎖定類型,它可以是以下值之一,請(qǐng)看如下枚舉結(jié)構(gòu):enum LockTypeEnumadLockUnspecified = -1,/未指定adLockReadOnly = 1,/只讀記錄集adLockPessimistic = 2,悲觀鎖定方式。數(shù)據(jù)在更新時(shí)鎖定其它所有動(dòng)作,這是最安全的鎖定機(jī)制adLockOptimistic = 3,樂觀鎖定方式。只有在你調(diào)用Update方法時(shí)才鎖定記錄。在此之前仍然可以做數(shù)據(jù)的更新、插入、刪除等動(dòng)作adLockBatchOptimistic = 4,樂觀分批更新。編輯時(shí)記

22、錄不會(huì)鎖定,更改、插入及刪除是在批處理模式下完成。;參數(shù)Options的含義請(qǐng)參考本文中對(duì)Connection對(duì)象的Execute()方法的介紹。5、記錄集的遍歷、更新根據(jù)我們剛才通過執(zhí)行SQL命令建立好的users表,它包含四個(gè)字段:ID,username,old,birthday以下的代碼實(shí)現(xiàn):打開記錄集,遍歷所有記錄,刪除第一條記錄,添加三條記錄,移動(dòng)光標(biāo)到第二條記錄,更改其年齡數(shù)據(jù),保存到數(shù)據(jù)庫(kù)。_variant_t vUsername,vBirthday,vID,vOld;_RecordsetPtr m_pRecordset;m_pRecordset.CreateInstance(&

23、quot;ADODB.Recordset");m_pRecordset->Open("SELECT * FROM users",_variant_t(IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);while(!m_pRecordset->adoEOF)/這里為什么是adoEOF而不是EOF呢?還記得rename("EOF","adoEOF")這一句嗎?vID = m_pRecordset->GetCollect

24、(_variant_t(long)0);/取得第1列的值,從0開始計(jì)數(shù),你也可以直接給出列的名稱;vUsername = m_pRecordset->GetCollect("username");/取得username字段的值vOld = m_pRecordset->GetCollect("old");vBirthday = m_pRecordset->GetCollect("birthday");/在DEBUG方式下的OUTPUT窗口輸出記錄集中的記錄if(vID.vt != VT_NULL &&

25、vUsername.vt != VT_NULL && vOld.vt != VT_NULL && vBirthday.vt != VT_NULL)TRACE("id:%d,姓名:%s,年齡:%d,生日:%srn",vID.lVal,(LPCTSTR)(_bstr_t)vUsername,vOld.lVal,(LPCTSTR)(_bstr_t)vBirthday);m_pRecordset->MoveNext();/移到下一條記錄m_pRecordset->MoveFirst();/移到首條記錄m_pRecordset->De

26、lete(adAffectCurrent);/刪除當(dāng)前記錄/添加三條新記錄并賦值for(int i=0;i<3;i+)m_pRecordset->AddNew();/添加新記錄m_pRecordset->PutCollect("ID",_variant_t(long)(i+10);m_pRecordset->PutCollect("username",_variant_t("葉利欽");m_pRecordset->PutCollect("old",_variant_t(long)71)

27、;m_pRecordset->PutCollect("birthday",_variant_t("1930-3-15");m_pRecordset->Move(1,_variant_t(long)adBookmarkFirst);/從第一條記錄往下移動(dòng)一條記錄,即移動(dòng)到第二條記錄處m_pRecordset->PutCollect(_variant_t("old"),_variant_t(long)45);/修改其年齡 m_pRecordset->Update();/保存到庫(kù)中6、關(guān)閉記錄集與連接 記錄集或連接都

28、可以用Close()方法來關(guān)閉: m_pRecordset->Close();/關(guān)閉記錄集m_pConnection->Close();/關(guān)閉連接至此,我想讀者朋友已經(jīng)熟悉了ADO操作數(shù)據(jù)庫(kù)的大致流程,也許您已經(jīng)胸有成竹,也許您還有點(diǎn)胡涂,不要緊!建議你嘗試寫幾個(gè)例子,這樣會(huì)更好地熟悉ADO,最后我給大家寫了一個(gè)小例子,例子實(shí)現(xiàn)的功能是讀出所有記錄并放到列表控件中,同時(shí)可以添加、刪除、修改記錄。二、編程步驟1、 啟動(dòng)Visual C+6.0,生成一個(gè)基于對(duì)話框的應(yīng)用程序,將該程序命名為ADOTest1;2、 在對(duì)話框界面上放置顯示記錄列表控件和添加、刪除記錄用的的編輯、按鈕控件,具

29、體設(shè)置參加代碼中對(duì)話框資源部分;3、 使用Class Wizard為添加、修改數(shù)據(jù)庫(kù)記錄的按鈕添加消息響應(yīng)函數(shù);4、 添加成程序代碼,編譯運(yùn)行程序。三、程序代碼/ ADOTest1Dlg.h : header file#if !defined(AFX_ADOTEST1DLG_H_29B385C0_02C0_4588_A8B4_D0EFBB4F578D_INCLUDED_)#define AFX_ADOTEST1DLG_H_29B385C0_02C0_4588_A8B4_D0EFBB4F578D_INCLUDED_#if _MSC_VER > 1000#pragma once#endif

30、/ _MSC_VER > 1000class CADOTest1Dlg : public CDialog/ Constructionpublic:BOOL m_bAutoSave;void SaveData();void LoadData();_variant_t vUserID,vUsername,vOld,vBirthday;BOOL m_bSuccess;int m_nCurrentSel;_RecordsetPtr m_pRecordset;CADOTest1Dlg(CWnd* pParent = NULL); / standard constructor/ Dialog Dat

31、a/AFX_DATA(CADOTest1Dlg)enum IDD = IDD_ADOTEST1_DIALOG ;CButton m_cDelItem;CButton m_cAddItem;CListCtrl m_userlist;UINT m_nUserID;UINT m_nOld;CString m_sUsername;COleDateTime m_tBirthday;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CADOTest1Dlg)protected:virtual void DoDat

32、aExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CADOTest1Dlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();virtual v

33、oid OnOK();afx_msg void OnAdditem();afx_msg void OnDelitem();afx_msg void OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult);/AFX_MSGDECLARE_MESSAGE_MAP();#endif / ADOTest1Dlg.cpp : implementation file#include "stdafx.h"#include "ADOTest1.h"#include "ADOTest1Dlg.h"#

34、ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endifextern CADOTest1App theApp;class CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAbout

35、Dlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP(); CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pD

36、X)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()CADOTest1Dlg:CADOTest1Dlg(CWnd* pParent /*=NULL*/): CDialog(CADOTest1Dlg:IDD, pParent)/AFX_DATA_INIT(CADOTest1Dlg)m_nUserID

37、 = 0;m_nOld = 0;m_sUsername = _T("");m_tBirthday = COleDateTime:GetCurrentTime();/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);m_nCurrentSel = -1;m_bSuccess = FALSE;m_bAutoSave = TRUE;void CADOTest1Dlg

38、:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CADOTest1Dlg)DDX_Control(pDX, IDC_DELITEM, m_cDelItem);DDX_Control(pDX, IDC_ADDITEM, m_cAddItem);DDX_Control(pDX, IDC_USERLIST, m_userlist);DDX_Text(pDX, IDC_USERID, m_nUserID);DDX_Text(pDX, IDC_OLD, m_nOld);DDX_Text(pDX, I

39、DC_USERNAME, m_sUsername);DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_tBirthday);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CADOTest1Dlg, CDialog)/AFX_MSG_MAP(CADOTest1Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_ADDITEM, OnAdditem)ON_BN_CLICKED(IDC_DELITEM, OnDelitem)ON_NOTIFY(LVN

40、_ITEMCHANGED, IDC_USERLIST, OnItemchangedUserlist)/AFX_MSG_MAPEND_MESSAGE_MAP()BOOL CADOTest1Dlg:OnInitDialog()CDialog:OnInitDialog();m_cDelItem.EnableWindow(FALSE);:SendMessage(m_userlist.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);/為列表控件添加列/m_userlist.InsertCol

41、umn(0,"用戶ID",LVCFMT_LEFT,60);m_userlist.InsertColumn(1,"用戶名",LVCFMT_LEFT,100);m_userlist.InsertColumn(2,"年齡",LVCFMT_LEFT,60);m_userlist.InsertColumn(3,"生日",LVCFMT_LEFT,100);/讀取數(shù)據(jù)庫(kù)中的信息添加到列表控件/int nItem;_variant_t vUsername,vBirthday,vID,vOld;trym_pRecordset.Cre

42、ateInstance("ADODB.Recordset");m_pRecordset->Open("SELECT*FROM users",_variant_t(IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);m_bSuccess = TRUE;while(!m_pRecordset->adoEOF)vID = m_pRecordset->GetCollect("ID");vUsername = m_pRec

43、ordset->GetCollect("username");vOld = m_pRecordset->GetCollect("old");vBirthday = m_pRecordset->GetCollect("birthday");nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vID);m_userlist.SetItem(nItem,1,1,(_bstr_t)vUsername,NULL,0,0,0);m_userlist.SetItem(nItem,2,1,(_bs

44、tr_t)vOld,NULL,0,0,0);m_userlist.SetItem(nItem,3,1,(_bstr_t)vBirthday,NULL,0,0,0);m_pRecordset->MoveNext();catch(_com_error e)/捕捉異常AfxMessageBox("讀取數(shù)據(jù)庫(kù)失敗!");/顯示錯(cuò)誤信息ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);i

45、f (pSysMenu != NULL)CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE); / Set big iconSetIcon(m_hIcon, FALSE); / Set small iconreturn TRUE; /

46、 return TRUE unless you set the focus to a controlvoid CADOTest1Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);void CADOTest1Dlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for

47、paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;

48、/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();HCURSOR CADOTest1Dlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CADOTest1Dlg:OnOK() if(m_bSuccess)m_pRecordset->Update();m_pRecordset->Close();CDialog:OnOK();void CADOTest1Dlg:OnAdditem() if(UpdateData()if(m_sUsername.GetLengt

49、h()>0)m_pRecordset->AddNew();m_nCurrentSel = m_userlist.InsertItem(0xffff,"");SaveData();/保存數(shù)據(jù) m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);m_userlist.SetHotItem(m_nCurrentSel);m_userlist.SetFocus();elseAfxMessageBox("請(qǐng)輸入用戶名")

50、;void CADOTest1Dlg:OnDelitem() m_bAutoSave = FALSE;if(m_nCurrentSel >= 0)m_userlist.DeleteItem(m_nCurrentSel);int count = m_userlist.GetItemCount();if(count <= m_nCurrentSel)m_nCurrentSel = count-1;m_pRecordset->Delete(adAffectCurrent);m_pRecordset->MoveNext();LoadData();m_userlist.SetIt

51、emState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);m_userlist.SetFocus();m_bAutoSave = TRUE;/在選擇列表框的時(shí)候調(diào)用/void CADOTest1Dlg:OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult) NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;if(pNMListView->uNewState&LVIS_SELECTED)U

52、pdateData();SaveData();/保存舊數(shù)據(jù)m_nCurrentSel = pNMListView->iItem;LoadData();/加載新數(shù)據(jù)m_cDelItem.EnableWindow();*pResult = 0;/將記錄集中的數(shù)據(jù)加載到編輯框/void CADOTest1Dlg:LoadData()m_pRecordset->Move(m_nCurrentSel,_variant_t(long)adBookmarkFirst);vUserID = m_pRecordset->GetCollect("ID");vUsername

53、= m_pRecordset->GetCollect("username");vOld = m_pRecordset->GetCollect("old");vBirthday = m_pRecordset->GetCollect("birthday");m_nUserID = vUserID.lVal;m_sUsername = (LPCTSTR)(_bstr_t)vUsername;m_nOld = vOld.lVal;m_tBirthday = vBirthday;UpdateData(FALSE);/將編輯框的數(shù)據(jù)保存到記錄集與列表框void CADOTest1Dlg:SaveData()if(!m_pRecordset->adoEOF &&a

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論