VC下用ADO连接数据库通用类,来自网上,稍微整理了一下。用的时候直接添加进去并在项目里引用头文件就可以了。
AdoConn.h
- // AdoConn.h: interface for the AdoConn class.
- //
- //
- //几点说明:
- //(1) 您的环境中msado15.dll不一定在这个目录下,请按实际情况修改
- //(2) 在编译的时候肯能会出现如下警告,对此微软在MSDN中作了说明,并建议我们不要理会这个警告。
- //msado15.tlh(405) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
- #import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
- #if !defined(AFX_AdoConn_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
- #define AFX_AdoConn_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_
- #if _MSC_VER >1000
- #pragma once
- #endif //_MSC_VER>1000
- class AdoConn
- {
- //定义变量
- public:
- //添加一个指向Connection对象的指针:
- _ConnectionPtr m_pConnection;
- //添加一个指向Recordset对象的指针:
- _RecordsetPtr m_pRecordset;
- //定义方法
- public:
- AdoConn();
- virtual ~AdoConn();
- //初始化—连接数据库
- void OnInitAdoConn();
- //连接数据库,供登录时测试用
- void TestAdoConn(CString servername,CString username,CString password);
- //执行查询
- _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
- //执行SQL语句,Insert Update _variant_t
- BOOL ExecuteSQL(_bstr_t bstrSQL);
- void ExitConnect();
- };
- #endif //!defined(AFX_AdoConn_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
AdoConn.cpp
- ///
- // AdoConn.cpp: implementation of the AdoConn class. //
- ///
- #include "stdafx.h"
- #include "AdoConn.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //
- // Construction/Destruction
- //
- AdoConn::AdoConn()
- {
- }
- AdoConn::~AdoConn()
- {
- }
- //初始化—连接数据库
- void AdoConn::OnInitAdoConn()
- {
- //初始化OLE/COM库环境
- ::CoInitialize(NULL);
- try
- {
- //创建Connection对象
- m_pConnection.CreateInstance("ADODB.Connection");
- //设置连接字符串,必须是BSTR型或者_bstr_t类型
- char path[ MAX_PATH ] = { '/0' };
- CString DataSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
- GetCurrentDirectory( MAX_PATH, path );
- CString pathstr;
- pathstr.Format("%s",path);
- DataSource +=pathstr;
- DataSource += "//info.mdb";
- DataSource +=";Persist Security Info=False";
- _bstr_t strConnect = (_bstr_t)DataSource;
- m_pConnection->Open(strConnect,"","",adModeUnknown);
- }
- //捕捉异常
- catch(_com_error e)
- {
- //显示错误信息
- AfxMessageBox(e.Description());
- }
- }
- //以下用于测试数据库联接
- void AdoConn::TestAdoConn(CString servername,CString username,CString password)
- {
- //初始化OLE/COM库环境
- ::CoInitialize(NULL);
- try
- {
- //创建Connection对象
- m_pConnection.CreateInstance("ADODB.Connection");
- //设置连接字符串,必须是BSTR型或者_bstr_t类型
- char path[ MAX_PATH ] = { '/0' };
- CString DataSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
- GetCurrentDirectory( MAX_PATH, path );
- CString pathstr;
- pathstr.Format("%s",path);
- DataSource +=pathstr;
- DataSource += "//info.mdb";
- DataSource +=";Persist Security Info=False";
- _bstr_t strConnect = (_bstr_t)DataSource;
- m_pConnection->Open(strConnect,"","",adModeUnknown);
- }
- //捕捉异常
- catch(_com_error e)
- {
- //显示错误信息
- AfxMessageBox(e.Description());
- }
- }
- //执行查询
- _RecordsetPtr& AdoConn::GetRecordSet(_bstr_t bstrSQL)
- {
- try
- {
- //连接数据库,如果Connection对象为空,则重新连接数据库
- if(m_pConnection==NULL)
- OnInitAdoConn();
- //创建记录集对象
- m_pRecordset.CreateInstance(__uuidof(Recordset));
- //取得表中的记录
- m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
- }
- //捕捉异常
- catch(_com_error e)
- {
- //显示错误信息
- AfxMessageBox(e.Description());
- }
- //返回记录集
- return m_pRecordset;
- }
- //执行SQL语句,Insert Update _variant_t
- BOOL AdoConn::ExecuteSQL(_bstr_t bstrSQL)
- {
- //_variant_t RecordsAffected;
- try
- {
- //是否已经连接数据库
- if(m_pConnection ==NULL)
- OnInitAdoConn();
- //Connection对象的Execute方法:(_bstr_t CommandText,
- //VARIANT * RecordsAffected,long Options )
- //其中CommandText是命令字串,通常是SQL命令。
- //参数RecordsAffected是操作完成后所影响的行数,
- //参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
- //adCmdProc-存储过程;adCmdUnknown-未知
- m_pConnection->Execute(bstrSQL,NULL,adCmdText);
- return true;
- }
- catch(_com_error e)
- {
- AfxMessageBox(e.Description());
- return false;
- }
- }
- void AdoConn::ExitConnect()
- {
- //关闭记录集和连接
- if (m_pRecordset !=NULL)
- m_pRecordset->Close();
- m_pConnection->Close();
- //释放环境
- ::CoUninitialize();
- }
用法: AdoConn ado;ado.GetRecordSet((_bstr_t)"select * from table1");