C++ ADO读取数据库
虽然比较简单,不过,对于初学者还是有点复杂,记录一下。 将C++数据库操作,写一个类。
1 ADOConn.h
01 #if !defined(AFX_ADOCONN_H__56A56674_91DC_43BB_BD09_9A0C8995161E__INCLUDED_)
02 #define AFX_ADOCONN_H__56A56674_91DC_43BB_BD09_9A0C8995161E__INCLUDED_
03
04 #import "C:\Program Files\Common Files\System\ado\msado15.dll"no_namespace \
05 rename("EOF","adoEOF")rename("BOF","adoBOF")
06 #if _MSC_VER > 1000
07 #pragma once
08 #endif // _MSC_VER > 1000
09 class ADOConn
10 {
11 public:
12 ADOConn(void);
13 ~ADOConn(void);
14
15
16 //添加一个指向Connection对象的指针
17 _ConnectionPtr m_pConnection;
18 //添加一个指向Recordset对象的指针
19 _RecordsetPtr m_pRecordset;
20 //_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
21 /* //封闭ADO类,方便以后使用 */
22 void OnInitADOConn(void);
23 void ExitConnect(void);
24 // 打开记录集
25 _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
26 };
27 #endif // !defined(AFX_ADOCONN_H__56A56674_91DC_43BB_BD09_9A0C8995161E__INCLUDED_)
ADOConn.cpp
01 #include "StdAfx.h"
02 #include "ADOConn.h"
03
04
05 ADOConn::ADOConn(void)
06 {
07 }
08
09
10 ADOConn::~ADOConn(void)
11 {
12 }
13
14
15 // //封闭ADO类,方便以后使用
16 void ADOConn::OnInitADOConn(void)
17 {
18 ::CoInitialize(NULL);
19 try
20 {
21 //创建connection对象
22 m_pConnection.CreateInstance("ADODB.Connection");
23 //设置连接字符串
24 _bstr_t strConnect="uid=;pwd=;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=Grades.mdb;";
25 //SERVER和UID,PWD的设置根据实际情况来设置"
26 /*m_pConnection->Open(strConnect,_T("admin"),_T("owenyang"),adModeUnknown);*/
27 m_pConnection->Open(strConnect,"admin","owenyang",adModeUnknown);
28 }
29 catch (_com_error e)
30 {
31 //显示错误信息
32 AfxMessageBox(e.Description());
33 }
34 }
35
36
37 void ADOConn::ExitConnect(void)
38 {
39 //关闭记录集和连接
40 if (m_pRecordset!=NULL)
41 {
42 m_pRecordset->Close();
43
44 }
45 m_pConnection->Close();
46 ::CoUninitialize();
47 }
48
49
50 // 打开记录集
51 _RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
52 {
53 //TODO: insert return statement here
54 try
55 {
56 if (m_pConnection==NULL)
57 {
58 OnInitADOConn();
59 }
60 //创建记录对象
61 m_pRecordset.CreateInstance(__uuidof(Recordset));
62 //取得表中记录
63 m_pRecordset->Open(bstrSQL,m_pConnection.GetInte易做图cePtr(),adOpenDynamic,
64 adLockOptimistic,adCmdText);
65 }
66 catch (_com_error e)
67 {
68 e.Description();
69 }
70 return m_pRecordset;
71 }
readAccessToList.cpp
view source
print?
01 void CTypeHistoryDlg::readAccessToList(void)
02 {
03 CRect rect;
04 GetClientRect(&rect);
05 int gridWidth=(rect.Width()-15)/11;
06 /*CString tem=_T("试试");
07 tem.Format(_T("%d"),gridWidth);*/
08 //MessageBox(tem);
09 m_historyList.InsertColumn(0,_T("编号"),LVCFMT_CENTER,gridWidth-10);
10 m_historyList.InsertColumn(1,_T("日期"),LVCFMT_CENTER,gridWidth+25);
11 m_historyList.InsertColumn(2,_T("段数"),LVCFMT_CENTER,gridWidth+10);
12 m_historyList.InsertColumn(3,_T("速度"),LVCFMT_CENTER,gridWidth);
13 m_historyList.InsertColumn(4,_T("回改"),LVCFMT_CENTER,gridWidth);
14 m_historyList.InsertColumn(5,_T("击键"),LVCFMT_CENTER,gridWidth);
15 m_historyList.InsertColumn(6,_T("码长"),LVCFMT_CENTER,gridWidth);
16 m_historyList.InsertColumn(7,_T("错字"),LVCFMT_CENTER,gridWidth);
17 m_historyList.InsertColumn(8,_T("字数"),LVCFMT_CENTER,gridWidth);
18 m_historyList.InsertColumn(9,_T("键数"),LVCFMT_CENTER,gridWidth);
19 m_historyList.InsertColumn(10,_T("用时"),LVCFMT_CENTER,gridWidth);
20 m_historyList.SetExtendedStyle(LVS_EX_FLATSB
21 |LVS_EX_FULLROWSELECT
22 |LVS_SHOWSELALWAYS
23 |LVS_EX_GRIDLINES);
24 ADOConn m_AdoConn;
25 m_AdoConn.OnInitADOConn();
26 CString sql;
27 sql.Format(_T("select* from grade"));
28 _RecordsetPtr m_pRecordset;
29 m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql);
30 while(m_AdoConn.m_pRecordset->adoEOF==0)
31 {
32 m_historyList.InsertItem(0,_T(""));
33 m_historyList.SetItemText(0,1,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("date"));
34 m_historyList.SetItemText(0,2,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("para"));
35 m_historyList.SetItemText(0,3,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("speed"));
36 m_historyList.SetItemText(0,4,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("back"));
37 m_historyList.SetItemText(0,5,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("hitkey"));
38 m_historyList.SetItemText(0,6,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("keylong"));
39 m_historyList.SetItemText(0,7,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("wronwor"));
40 m_historyList.SetItemText(0,8,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("wordscount"));
41 m_historyList.SetItemText(0,9,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("keycount"));
42 m_historyList.SetItemText(0,10,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("time"));
43 m_historyList.SetItemText(0,0,(TCHAR*)(_bstr_t)m_pRecordset->GetCollect("id"));
44 m_pRecordset->MoveNext();
45 }
46 m_historyList.SetItemState(0,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
47 m_AdoConn.ExitConnect();
48 }
作者:owenyang
补充:软件开发 , C++ ,