[MFC学习]1.ADO编程——读取数据

1.新建一个对话框MFC项目,命名为MineGeology。

2.导入msado15.dll类库,在stdafx.h文件中输入:

#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace\

    rename("EOF","adoEOF") rename("BOF","adoBOF")

用.udl文件设置连接字符串

3.封装操作ADO的类CADOHelper。

 1 #pragma once

 2 class CADOHelper

 3 {

 4 

 5 public:    

 6     _ConnectionPtr m_pConnection;//_com_ptr_t智能指针

 7     _RecordsetPtr m_pRecordset; 

 8     CString connStr;

 9 public:

10 

11     CADOHelper(void);

12     ~CADOHelper(void);

13     CString GetConnString();

14     void SetConnString(CString _conn);

15     void InitADOConn();

16     _RecordsetPtr& OpenRecordset(CString sql);

17     void CloseRecordset();

18     void CloseConn();

19     UINT GetRecordCount(_RecordsetPtr pRecordset);

20 };

实现文件ADOHelper.cpp

ADOHelper.CPP
 1 #include "StdAfx.h"

 2 #include "ADOHelper.h"

 3 

 4 

 5 CADOHelper::CADOHelper(void):connStr()

 6 {

 7     

 8 }

 9 CString CADOHelper::GetConnString()

10 {

11     return connStr;

12 }

13 void CADOHelper::SetConnString(CString _conn)

14 {

15     connStr=_conn;

16 }

17 CADOHelper::~CADOHelper(void)

18 {

19 }

20 void CADOHelper::InitADOConn()

21 {

22     ::CoInitialize(NULL);

23     try

24     {

25         m_pConnection.CreateInstance("ADODB.Connection");

26         _bstr_t strConn=connStr;

27         m_pConnection->Open(strConn,"","",adModeUnknown);

28     }

29     catch    (_com_error ex)

30     {

31         AfxMessageBox(ex.Description());

32     }

33 }

34 _RecordsetPtr& CADOHelper::OpenRecordset(CString sql)

35 {

36     ASSERT(!sql.IsEmpty());

37     try

38     {

39         m_pRecordset.CreateInstance(_uuidof(Recordset));

40         m_pRecordset->Open(_bstr_t(sql),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);

41     }

42     catch (_com_error e)

43     {

44         AfxMessageBox(e.Description());

45     }

46     return m_pRecordset;

47 }

48 void CADOHelper::CloseRecordset()

49 {

50     if (m_pRecordset->GetState()==adStateOpen)

51     {

52         m_pRecordset->Close();

53     }

54 }

55 void CADOHelper::CloseConn()

56 {

57     m_pConnection->Close();

58     ::CoUninitialize();

59 }

60 UINT CADOHelper::GetRecordCount(_RecordsetPtr pRecordset)

61 {

62     int nCount=0;

63     try

64     {

65         pRecordset->MoveFirst();

66 

67     }

68     catch (...)

69     {

70         return 0;

71     }

72     if (pRecordset->adoBOF)

73     {

74         return 0;

75     }

76     while(!pRecordset->adoEOF)

77     {

78         pRecordset->MoveNext();

79         nCount++;

80 

81     }

82     pRecordset->MoveFirst();

83     return nCount;

84 }

 

4.在OnInitDialog方法中添加如下代码,查询并显示用户表的Users数据

 1 m_listctrl.InsertColumn(0,L"ID",LVCFMT_LEFT,130,0);

 2     m_listctrl.InsertColumn(1,L"姓名",LVCFMT_LEFT,130,1);

 3     m_listctrl.InsertColumn(2,L"密码",LVCFMT_LEFT,130,2);

 4     m_listctrl.InsertItem(0,L"");

 5     m_listctrl.SetItemText(0,0,L"guanyu");

 6     m_listctrl.SetItemText(0,1,L"guanyu");

 7     m_listctrl.InsertItem(1,L"");

 8     m_listctrl.SetItemText(1,0,L"guanyu");

 9     m_listctrl.SetItemText(1,1,L"guanyu");

10     

11     /*CADOHelper m_pAdoHelper;

12     m_pAdoHelper.SetConnString(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\我的文档\\Visual Studio 2010\\Projects\\MineGeology\\MineGeology\\AppData\\Geology.mdb;Persist Security Info=False");

13     m_pAdoHelper.InitADOConn();

14     CString sql("select * from Users");

15     m_pAdoHelper.m_pRecordset=m_pAdoHelper.OpenRecordset(sql);

16     int i=1;

17     while(!m_pAdoHelper.m_pRecordset->adoEOF)

18     {

19         i++;

20         m_listctrl.InsertItem(0,L"");

21         m_listctrl.SetItemText(0,0,(_bstr_t)m_pAdoHelper.m_pRecordset->GetCollect("ID"));

22         m_listctrl.SetItemText(0,1,(_bstr_t)m_pAdoHelper.m_pRecordset->GetCollect("User_Name"));

23         m_listctrl.SetItemText(0,2,(_bstr_t)m_pAdoHelper.m_pRecordset->GetCollect("Pwd"));

24         m_pAdoHelper.m_pRecordset->MoveNext();

25     }

26     m_pAdoHelper.CloseRecordset();

27     m_pAdoHelper.CloseConn();*/

28     CADOHelper* m_pAdoHelper=new CADOHelper();

29     m_pAdoHelper->SetConnString(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\我的文档\\Visual Studio 2010\\Projects\\MineGeology\\MineGeology\\AppData\\Geology.mdb;Persist Security Info=False");

30     m_pAdoHelper->InitADOConn();

31     CString sql("select * from Users");

32     m_pAdoHelper->m_pRecordset=m_pAdoHelper->OpenRecordset(sql);

33     int i=1;

34     while(!m_pAdoHelper->m_pRecordset->adoEOF)

35     {

36         i++;

37         m_listctrl.InsertItem(0,L"");

38         m_listctrl.SetItemText(0,0,(_bstr_t)m_pAdoHelper->m_pRecordset->GetCollect("ID"));

39         m_listctrl.SetItemText(0,1,(_bstr_t)m_pAdoHelper->m_pRecordset->GetCollect("User_Name"));

40         m_listctrl.SetItemText(0,2,(_bstr_t)m_pAdoHelper->m_pRecordset->GetCollect("Pwd"));

41         m_pAdoHelper->m_pRecordset->MoveNext();

42     }

43     m_pAdoHelper->CloseRecordset();

44     m_pAdoHelper->CloseConn();

45     delete m_pAdoHelper;

代码中注释部分为类对象的访问方式,使用中的为动态内存访问方式。

[MFC学习]1.ADO编程——读取数据

你可能感兴趣的:(读取数据)