VC下的通用数据库访问类

   VC下用ADO连接数据库通用类,来自网上,稍微整理了一下。用的时候直接添加进去并在项目里引用头文件就可以了。

AdoConn.h

  1. //   AdoConn.h:   interface   for   the   AdoConn   class.   
  2. //   
  3. //   
  4. //几点说明:   
  5. //(1)   您的环境中msado15.dll不一定在这个目录下,请按实际情况修改   
  6. //(2)   在编译的时候肯能会出现如下警告,对此微软在MSDN中作了说明,并建议我们不要理会这个警告。   
  7. //msado15.tlh(405)   :   warning   C4146:   unary   minus   operator   applied   to   unsigned   type,   result   still   unsigned     
  8.    
  9. #import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")   
  10. #if !defined(AFX_AdoConn_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)   
  11. #define AFX_AdoConn_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_   
  12. #if _MSC_VER >1000   
  13. #pragma once   
  14. #endif //_MSC_VER>1000   
  15. class AdoConn       
  16. {
  17.     //定义变量   
  18. public:   
  19.     //添加一个指向Connection对象的指针:   
  20.     _ConnectionPtr m_pConnection;   
  21.     //添加一个指向Recordset对象的指针:   
  22.     _RecordsetPtr m_pRecordset;   
  23.     //定义方法   
  24. public:   
  25.     AdoConn();   
  26.     virtual ~AdoConn();   
  27.     //初始化—连接数据库   
  28.     void OnInitAdoConn();   
  29.     //连接数据库,供登录时测试用   
  30.     void TestAdoConn(CString servername,CString username,CString password);   
  31.     //执行查询   
  32.     _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);   
  33.     //执行SQL语句,Insert Update _variant_t   
  34.     BOOL ExecuteSQL(_bstr_t bstrSQL);   
  35.     void ExitConnect();   
  36. };   
  37.   #endif //!defined(AFX_AdoConn_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)

AdoConn.cpp

  1. ///
  2. //   AdoConn.cpp:   implementation   of   the   AdoConn   class.     //
  3. ///
  4. #include "stdafx.h"
  5. #include "AdoConn.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //   
  12. //   Construction/Destruction   
  13. //   
  14. AdoConn::AdoConn()   
  15. {
  16.     
  17. }
  18. AdoConn::~AdoConn()   
  19. {   
  20.     
  21. }   
  22. //初始化—连接数据库   
  23. void     AdoConn::OnInitAdoConn()   
  24. {   
  25.     //初始化OLE/COM库环境     
  26.     ::CoInitialize(NULL);   
  27.     try   
  28.     {   
  29.         //创建Connection对象   
  30.         m_pConnection.CreateInstance("ADODB.Connection");   
  31.         //设置连接字符串,必须是BSTR型或者_bstr_t类型
  32.         char path[ MAX_PATH ] = { '/0' }; 
  33.         CString DataSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
  34.         GetCurrentDirectory( MAX_PATH, path ); 
  35.         CString pathstr;
  36.         pathstr.Format("%s",path);
  37.         DataSource +=pathstr;
  38.         DataSource += "//info.mdb"
  39.         DataSource +=";Persist Security Info=False";
  40.         _bstr_t   strConnect   =   (_bstr_t)DataSource;   
  41.         m_pConnection->Open(strConnect,"","",adModeUnknown);   
  42.     }   
  43.     //捕捉异常   
  44.     
  45.     catch(_com_error e)   
  46.     {   
  47.         //显示错误信息   
  48.         AfxMessageBox(e.Description());   
  49.     }   
  50. }   
  51. //以下用于测试数据库联接   
  52. void AdoConn::TestAdoConn(CString servername,CString username,CString password)   
  53. {   
  54.     //初始化OLE/COM库环境     
  55.     ::CoInitialize(NULL);   
  56.     try   
  57.     {   
  58.         //创建Connection对象   
  59.         m_pConnection.CreateInstance("ADODB.Connection");   
  60.         
  61.         //设置连接字符串,必须是BSTR型或者_bstr_t类型
  62.         char path[ MAX_PATH ] = { '/0' }; 
  63.         CString DataSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
  64.         GetCurrentDirectory( MAX_PATH, path ); 
  65.         CString pathstr;
  66.         pathstr.Format("%s",path);
  67.         DataSource +=pathstr;
  68.         DataSource += "//info.mdb"
  69.         DataSource +=";Persist Security Info=False";
  70.         _bstr_t   strConnect   =   (_bstr_t)DataSource;     
  71.         m_pConnection->Open(strConnect,"","",adModeUnknown);   
  72.     }   
  73.     //捕捉异常   
  74.     
  75.     catch(_com_error e)   
  76.     {   
  77.         //显示错误信息   
  78.         AfxMessageBox(e.Description());   
  79.     }   
  80. }   
  81. //执行查询   
  82. _RecordsetPtr& AdoConn::GetRecordSet(_bstr_t bstrSQL)   
  83. {   
  84.     try   
  85.     {   
  86.         //连接数据库,如果Connection对象为空,则重新连接数据库   
  87.         if(m_pConnection==NULL)   
  88.             OnInitAdoConn();   
  89.         //创建记录集对象   
  90.         m_pRecordset.CreateInstance(__uuidof(Recordset));   
  91.         //取得表中的记录   
  92.         m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);   
  93.     }   
  94.     //捕捉异常   
  95.     catch(_com_error e)   
  96.     {   
  97.         //显示错误信息   
  98.         AfxMessageBox(e.Description());   
  99.     }   
  100.     //返回记录集   
  101.     return m_pRecordset;   
  102. }   
  103. //执行SQL语句,Insert Update _variant_t   
  104. BOOL AdoConn::ExecuteSQL(_bstr_t bstrSQL)   
  105. {   
  106.     //_variant_t RecordsAffected;   
  107.     try   
  108.     {   
  109.         //是否已经连接数据库   
  110.         if(m_pConnection ==NULL)   
  111.             OnInitAdoConn();   
  112.         //Connection对象的Execute方法:(_bstr_t CommandText,     
  113.         //VARIANT * RecordsAffected,long Options   )     
  114.         //其中CommandText是命令字串,通常是SQL命令。   
  115.         //参数RecordsAffected是操作完成后所影响的行数,     
  116.         //参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名   
  117.         //adCmdProc-存储过程;adCmdUnknown-未知   
  118.         m_pConnection->Execute(bstrSQL,NULL,adCmdText);   
  119.         return true;   
  120.     }   
  121.     catch(_com_error e)   
  122.     {   
  123.         AfxMessageBox(e.Description());   
  124.         return false;   
  125.     }   
  126. }   
  127. void AdoConn::ExitConnect()   
  128. {   
  129.     //关闭记录集和连接   
  130.     if (m_pRecordset !=NULL)   
  131.         m_pRecordset->Close();   
  132.     m_pConnection->Close();   
  133.     //释放环境   
  134.     ::CoUninitialize();   
  135. }

用法: AdoConn ado;ado.GetRecordSet((_bstr_t)"select * from table1");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值