stdafx.h 中加入
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
中才能引用这三个指针
void CSjtestDlg::OninitADOConn(void){ try{ //创建连接对象实例 m_pConnection.CreateInstance("ADODB.Connection"); //设置连接字符串 CString strConnect=_T("DRIVER={Microsoft Access Driver (*.mdb)};uid=;pwd=;DBQ=shujuku.mdb;"); //使用 Open 方法连接数据库 m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown); } catch(_com_error e) { AfxMessageBox(_T("连接数据失败,请检查数据库路径是否正确!")); }} void CSjtestDlg::ExitConnect(void){ if(m_pRecordset!=NULL) m_pRecordset->Close(); m_pConnection->Close();}
m_Grid.InsertColumn(0,_T("员工编号"),LVCFMT_LEFT,80,0); m_Grid.InsertColumn(1,_T("员工姓名"),LVCFMT_LEFT,80,1); m_Grid.InsertColumn(2,_T("所属部门"),LVCFMT_LEFT,80,2); m_Grid.InsertColumn(3,_T("基本工资"),LVCFMT_LEFT,80,3); //连接数据库 OninitADOConn() ; //设置查询字符串 _bstr_t bstrSQL = "select * from employees order by 员工编号 desc"; //创建记录集指针对象实例 m_pRecordset.CreateInstance(__uuidof(Recordset));//该句也可以使用 m_pRecordset.CreateInstance(“ADODB.Recordset”);代替打开记录集 m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic, adLockOptimistic,adCmdText); while(!m_pRecordset->adoEOF) { m_Grid.InsertItem(0,_T("")); m_Grid.SetItemText(0,0,(_bstr_t)m_pRecordset->GetCollect(_T("员工编号"))); m_Grid.SetItemText(0,1,(_bstr_t)m_pRecordset->GetCollect(_T("员工姓名"))); m_Grid.SetItemText(0,2,(_bstr_t)m_pRecordset->GetCollect(_T("所属部门"))); m_Grid.SetItemText(0,3,(_bstr_t)m_pRecordset->GetCollect(_T("基本工资"))); //将记录集指针移动到下一条记录 m_pRecordset->MoveNext(); } //断开数据库连接 ExitConnect();
ADOConn.h
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF") #pragma onceclass ADOConn{public: _ConnectionPtr m_pConnection; //连接对象指针 _RecordsetPtr m_pRecordset; //记录集对象指针public : ADOConn(void); ~ADOConn(void); void OnInitADOConn() ; //连接数据库 BOOL ExecuteSQL(_bstr_t bstrSQL) ; //执行SQL语句 _RecordsetPtr & GetRecordSet(_bstr_t bstrSQL) ; //执行SQL查询并返回记录集指针 void ExitConnect() ; //关闭记录集便断开数据连接};
ADOConn.cpp
#include "StdAfx.h"#include "ADOConn.h"ADOConn::ADOConn(void){}ADOConn::~ADOConn(void){}void ADOConn::OnInitADOConn(){ try{ //创建连接对象实例 m_pConnection.CreateInstance("ADODB.Connection"); //设置连接字符串 CString strConnect=_T("DRIVER={Microsoft Access Driver (*.mdb)};uid=;pwd=;DBQ=shujuku.mdb;"); //使用 Open 方法连接数据库 m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown); } catch(_com_error e) { AfxMessageBox(e.Description()); } }BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL){ try{ if(m_pConnection == NULL) OnInitADOConn() ; m_pConnection->Execute(bstrSQL , NULL , adCmdText) ; return true ; } catch(_com_error e){ e.Description() ; return false ; } }_RecordsetPtr & ADOConn::GetRecordSet(_bstr_t bstrSQL){ try{ if(m_pConnection == NULL) OnInitADOConn() ; m_pRecordset.CreateInstance(__uuidof(Recordset)); //打开记录集 m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic, adLockOptimistic,adCmdText); } catch(_com_error e){ e.Description() ; } return m_pRecordset ; }void ADOConn::ExitConnect(){ if(m_pRecordset != NULL) m_pRecordset->Close(); m_pConnection->Close();}