程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ ADO讀取數據庫

C++ ADO讀取數據庫

編輯:C++入門知識

雖然比較簡單,不過,對於初學者還是有點復雜,記錄一下。 將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.GetInterfacePtr(),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
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved