VC+ADO+MS-SQL的数据库操作类

来源:互联网 发布:java面试简历项目经验 编辑:程序博客网 时间:2024/05/09 14:24

以前写的一个数据库操作类.其中有参考网上很多朋友的代码.非常谢谢!
一.在stdafx.h头文件中加入
#import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
注意在下面二句之间加入.否则会出错.
#endif // _AFX_NO_AFXCMN_SUPPORT
//在这下面加入上面这句.

//{{AFX_INSERT_LOCATION}}

二.新建一个ADOConn的类.在ADOConn.cpp中最上面加入#include "stdafx.h".

三.其中ADOConn.h中的代码如下:

 

  1. // ADOConn.h: interface for the ADOConn class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_ADOCONN_H__FF7DB821_D61D_437A_9FA8_193D48F1C1F5__INCLUDED_)
  5. #define AFX_ADOCONN_H__FF7DB821_D61D_437A_9FA8_193D48F1C1F5__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. class ADOConn  
  10. {
  11. //public:
  12. //  ADOConn();
  13. //  virtual ~ADOConn();
  14. // 定义变量
  15. public:
  16.          //添加一个指向Connection对象的指针:
  17.     _ConnectionPtr m_pConnection;
  18.     //添加一个指向Recordset对象的指针:
  19.     _RecordsetPtr m_pRecordset;
  20. // 定义方法
  21. public:
  22.     ADOConn();
  23.     virtual ~ADOConn();
  24.     // 初始化—连接数据库
  25.     void  OnInitADOConn();
  26.     // 执行查询
  27.     _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
  28.     // 执行SQL语句,Insert Update _variant_t
  29.     BOOL ExecuteSQL(_bstr_t bstrSQL);
  30.     void ExitConnect();
  31. };
  32. #endif // !defined(AFX_ADOCONN_H__FF7DB821_D61D_437A_9FA8_193D48F1C1F5__INCLUDED_)

四.其中ADOConn.cpp中的代码如下:

 

 

  1. // ADOConn.cpp: implementation of the ADOConn class.
  2. //
  3. //////////////////////////////////////////////////////////////////////

  4. #include "stdafx.h"
  5. #include "First.h"
  6. #include "ADOConn.h"



  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif

  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////

  15. ADOConn::ADOConn()
  16. {

  17. }

  18. ADOConn::~ADOConn()
  19. {

  20. }
  21. // 初始化—连接数据库
  22. void  ADOConn::OnInitADOConn()
  23. {

  24.     // 初始化OLE/COM库环境 
  25.     ::CoInitialize(NULL);


  26. HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
  27. if(SUCCEEDED(hr))


  28. try
  29. {
  30. }
  31. catch(_com_error e)//捕获错误
  32. {
  33.  e.Description();
  34.  e.ErrorMessage();
  35.  e.Source();
  36. }


  37. //打开数据库:
  38. CString strConn;

  39. //MyConn.udl为数据库连接文件,以下是其位置.
  40. m_pConnection->ConnectionString ="File Name=d://vc//MyConn.udl"
  41. try
  42. {


  43. m_pConnection->Open(L"",L"",L"",-1); 


  44. }
  45. catch(_com_error e)///捕捉异常
  46. {
  47.  CString errormessage;
  48.  errormessage.Format("打开数据库失败!/r/n错误信息:%s", e.ErrorMessage());
  49.  AfxMessageBox(errormessage);///显示错误信息
  50.  //return FALSE;
  51. }

  52. //AfxMessageBox("打开数据库正常");


  53. }

  54. // 执行查询
  55. _RecordsetPtr&  ADOConn::GetRecordSet(_bstr_t bstrSQL)
  56. {
  57.     try
  58.     {
  59.         // 连接数据库,如果Connection对象为空,则重新连接数据库
  60.         if(m_pConnection==NULL)
  61.             OnInitADOConn();
  62.         // 创建记录集对象
  63.         m_pRecordset.CreateInstance(__uuidof(Recordset));
                m_pRecordset->CursorLocation=adUseClient; //游标的位置出现.
                  //不加这句会出现The rowset is not    bookmarkable运行错误
  1.         // 取得表中的记录
  2.         m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
  3.     }
  4.     // 捕捉异常
  5.     catch(_com_error e)
  6.     {
  7.         // 显示错误信息
  8.         AfxMessageBox(e.Description());
  9.     }
  10.     // 返回记录集
  11.     return m_pRecordset;
  12. }
  13.  // 执行SQL语句,Insert Update _variant_t
  14. BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
  15. {
  16. //  _variant_t RecordsAffected;
  17.     try
  18.     {
  19.         // 是否已经连接数据库
  20.         if(m_pConnection == NULL)
  21.             OnInitADOConn();
  22.         // Connection对象的Execute方法:(_bstr_t CommandText, 
  23.         // VARIANT * RecordsAffected, long Options ) 
  24.         // 其中CommandText是命令字串,通常是SQL命令。
  25.         // 参数RecordsAffected是操作完成后所影响的行数, 
  26.         // 参数Options表示CommandText的类型:
  27.                   //adCmdText-文本命令;adCmdTable-表名
  28.         // adCmdProc-存储过程;adCmdUnknown-未知
  29.         m_pConnection->Execute(bstrSQL,NULL,adCmdText);
  30.         return true;
  31.     }
  32.     catch(_com_error e)
  33.     {
  34.         AfxMessageBox(e.Description());
  35.         return false;
  36.     }
  37. }
  38. void ADOConn::ExitConnect()
  39. {
  40.     // 关闭记录集和连接
  41.     if (m_pRecordset != NULL)
  42.         m_pRecordset->Close();
  43.     m_pConnection->Close();
  44.     // 释放环境
  45.     ::CoUninitialize();
  46. }

 

 

 

 

 

 

原创粉丝点击