首页 > 编程语言 >用C++封装的ADO类

用C++封装的ADO类

时间:2023-06-11 13:31:59浏览次数:37  
标签:pConnection return pCommand 封装 vNull C++ ADO CString CADO




用C++封装的ADO类



作者:刘振海


.H文件

// ADO.h: interface for the CADO class.
 //
 //#if !defined(AFX_ADO_H__5A466E67_5E04_445D_9CB0_C64650B9AC68__INCLUDED_)
 #define AFX_ADO_H__5A466E67_5E04_445D_9CB0_C64650B9AC68__INCLUDED_#if _MSC_VER > 1000
 #pragma once
 #endif // _MSC_VER > 1000#include <comdef.h>
 #include "msado15.tlh"class CADO  
 {
 public:
  _RecordsetPtr  m_pRecordset;
 public:
  CADO();
  virtual ~CADO();
  BOOL openDB(CString m_strPathName, CString m_strName, CString m_strPassword, CString m_strProvider);
  void close();
  _RecordsetPtr search(const CString m_strSql);
  BOOL dbDelete(const CString m_strSql);
  BOOL update(const CString m_strSql);
  BOOL insert(const CString m_strSql);
 private:
  void initConnection();
  void initRecordSet();
  void initCommand();
  BOOL CommandIsValid();
  BOOL RecordSetIsValid();
  BOOL ConnectionIsValid();
 private:
  _CommandPtr m_pCommand;
  _ConnectionPtr  m_pConnection;};
#endif // !defined(AFX_ADO_H__5A466E67_5E04_445D_9CB0_C64650B9AC68__INCLUDED_)
 
.CPP文件
// ADO.cpp: implementation of the CADO class.
 //
 //#include "stdafx.h"
 #include "YXRM.h"
 #include "ADO.h"#ifdef _DEBUG
 #undef THIS_FILE
 static char THIS_FILE[]=__FILE__;
 #define new DEBUG_NEW
 #endif//
 // Construction/Destruction
 //CADO::CADO()
 {
  
 }CADO::~CADO()
 {
  
 }BOOL CADO::ConnectionIsValid()
 {
  if (m_pConnection == NULL)
   return FALSE;
  else
   return TRUE;
 }BOOL CADO::RecordSetIsValid()
 {
  if (m_pRecordset == NULL)
   return FALSE;
  else
   return TRUE;
 }BOOL CADO::CommandIsValid()
 {
  if (m_pCommand == NULL)
   return FALSE;
  else
   return TRUE;
 }void CADO::initConnection()
 {
  if (!ConnectionIsValid())
   m_pConnection.CreateInstance( __uuidof(Connection));
 }void CADO::initRecordSet()
 {
  if (!RecordSetIsValid())
   m_pRecordset.CreateInstance(__uuidof(Recordset));
 }void CADO::initCommand()
 {
  if (!CommandIsValid())
   m_pCommand.CreateInstance(__uuidof(Command));
 }BOOL CADO::openDB(CString m_strPathName, CString m_strName, CString m_strPassword, CString m_strProvider)
 {
  initConnection();
  initRecordSet();
     initCommand();
  m_pConnection->Provider = _bstr_t(m_strProvider);
  if(S_OK == m_pConnection->Open(_bstr_t(m_strPathName), _bstr_t(m_strName), _bstr_t(m_strPassword), adModeUnknown))
   return TRUE;
  else 
   return FALSE;
 } void CADO::close()
 {
  if (RecordSetIsValid())
  {
   if( m_pRecordset->State == adStateOpen)
    m_pRecordset->Close();
  }
  if (ConnectionIsValid() || m_pConnection->State == adStateOpen)
  {
   m_pConnection->Close();
  }
 }BOOL CADO::insert(const CString m_strSql)
 {
  try
  {
   if (!ConnectionIsValid() || m_pConnection->State == adStateClosed)
   {
    AfxMessageBox("DB closed");
    //RAConnect(m_ConnectionString);
    return FALSE;
   }
   
   if(!CommandIsValid())
   {
    initCommand();
   }
   
   m_pCommand->ActiveConnection = m_pConnection;
   m_pCommand->CommandType = adCmdText;
   m_pCommand->CommandText = _bstr_t(m_strSql);
   
   _variant_t vNull;
   vNull.vt = VT_ERROR;
   vNull.scode = DISP_E_PARAMNOTFOUND;
   
   m_pCommand->Execute(&vNull, &vNull, adCmdText);//m_pConnection->Execute(........)也可以
   
   return TRUE;
  }
  catch(_com_error &e)
  {
   AfxMessageBox(e.Description()); 
   return FALSE;
  }
 }BOOL CADO::update(const CString m_strSql)
 {
  try
  {
   if (!ConnectionIsValid() || m_pConnection->State == adStateClosed)
   {
    AfxMessageBox("DB closed");
    //RAConnect(m_ConnectionString);
    return FALSE;
   }  if(!CommandIsValid())
   {
    initCommand();
   }
   
   m_pCommand->ActiveConnection = m_pConnection;
   m_pCommand->CommandType = adCmdText;
   m_pCommand->CommandText = _bstr_t(m_strSql);
   
   _variant_t vNull;
   vNull.vt = VT_ERROR;
   vNull.scode = DISP_E_PARAMNOTFOUND;
   
   m_pCommand->Execute(&vNull, &vNull, adCmdText);
   
   return TRUE;
  }
  catch(_com_error &e)
  {
   AfxMessageBox(e.Description()); 
   return FALSE;
  }
 }BOOL CADO::dbDelete(const CString m_strSql)
 {
  try
  {
   if (!ConnectionIsValid() || m_pConnection->State == adStateClosed)
   {
    AfxMessageBox("DB closed");
    //RAConnect(m_ConnectionString);
    return FALSE;
   }  if(!CommandIsValid())
   {
    initCommand();
   }
   
   m_pCommand->ActiveConnection = m_pConnection;
   m_pCommand->CommandType = adCmdText;
   m_pCommand->CommandText = _bstr_t(m_strSql);
   
   _variant_t vNull;
   vNull.vt = VT_ERROR;
   vNull.scode = DISP_E_PARAMNOTFOUND;
   
   m_pCommand->Execute(&vNull, &vNull, adCmdText);
    
    return TRUE;
   }
  catch(_com_error &e)
  {
   AfxMessageBox(e.Description()); 
   return FALSE;
  }
 }_RecordsetPtr CADO::search(const CString m_strSql)
 {
  try
  {
   if (!ConnectionIsValid() || m_pConnection->State == adStateClosed)
   {
    AfxMessageBox("DB closed");
    //RAConnect(m_ConnectionString);
    return NULL;
   }
   else
   { 
    if (RecordSetIsValid())
    {
     if (m_pRecordset->State == adStateOpen)
      m_pRecordset->Close();
    }
    else
    {    
     initRecordSet();
    }
    
    if(!CommandIsValid())
    {
     initCommand();
    }   m_pCommand->ActiveConnection = m_pConnection;
    m_pCommand->CommandType = adCmdText;
    m_pCommand->CommandText = _bstr_t(m_strSql);
    
    _variant_t vNull;
    vNull.vt = VT_ERROR;
    vNull.scode = DISP_E_PARAMNOTFOUND;
    
    m_pRecordset = m_pCommand->Execute(&vNull, &vNull, adCmdText);
    return  m_pRecordset.Detach();
   }
  }
  catch(_com_error &e)
  {
   AfxMessageBox(e.Description()); 
   return NULL;
  }
 }
 调用/ if(!(classAdo.openDB("db1.mdb","","","Microsoft.JET.OLEDB.4.0")))
   return FALSE;

标签:pConnection,return,pCommand,封装,vNull,C++,ADO,CString,CADO
From: https://blog.51cto.com/u_130277/6457579

相关文章

  • 详解VOLATILE在C++中的作用
    VOLATILE的介绍     volatile类似于大家所熟知的const也是一个类型修饰符。volatile是给编译器的指示来说明对它所修饰的对象不应该执行优化。volatile的作用就是用来进行多线程编程。在单线程中那就是只能起到限制编译器优化的作用。所以单线程的童鞋们就不用浪费精力看下......
  • React - 06 初步尝试封装组件
    1.封装dialog组件调用2.函数组件是静态组件/*函数组件是“静态组件”第一次渲染组件,把函数执行+产生一个私有的上下文:EC(V)+把解析出来的props「含children」传递进来「但是被冻结了」+对函数返回的JSX元素「virtualDOM」进行渲染当我们点击按钮的......
  • [c/c++/OC]高质量的面试题及答案及注解
    一、选择题C语言:1.声明语句为inta[3][4];下列表达式中与数组元素a[2][1]等价的是(A)。A、*(a[2]+1)B、a[9]C、*(a[1]+2)D、*(*(a+2))+1a[2]<==>*(a+2)是等价的C两个数反过来了,D、1放进去2.请问经过表达式a=5?0:1的运算,变量a的最终值是(C......
  • 聊聊结合业务需求给出合理的技术解决方案,改进现有模块功能,提高系统的可扩展性,封装性,稳
    针对提高系统的可扩展性、封装性、稳定性,改进现有模块功能,以下是我给出的一些技术解决方案:使用面向对象编程的设计模式:可以采用一些设计模式如单例模式、工厂模式、观察者模式等,来提高系统的可扩展性和封装性。应用微服务架构:可以将系统拆分成多个独立的服务,使得每个服务都可......
  • C++面试八股文:struct、class和union有哪些区别?
    C++面试八股文:struct、class和union有哪些区别?某日小二参加XXX科技公司的C++工程师开发岗位5面:面试官:struct和class有什么区别?小二:在C++中,struct和class的唯一区别是默认的访问控制。struct默认的成员是public的,而class的默认成员是private的。面试官:struct、class和unio......
  • [C/C++] 结构体
    在C语言中,字符串实际上是使用空字符\0结尾的一维字符数组。因此,\0是用于标记字符串的结束。固定缓冲区#defineMAX_LEN100#defineDATA_LEN100typedefstruct{intlength;chardata[MAX_LEN];//固定缓冲区大小,浪费内存}max_buffer,*max_buffer_ptr;......
  • 《C++》--C转C++基础1
    变量类型、关键字变量类型:shortintlongdoublefloatcharbool输出cout<<""<<endl;#include<iostream>#include<string>usingnamespacestd;intmain(){ charstr1[]="Hello"; stringstr2="World";//string创建字符串......
  • C++ 指针
    一、C++指针的算术运算递增一个指针我们喜欢在程序中使用指针代替数组,因为变量指针可以递增,而数组不能递增,因为数组是一个常量指针。下面的程序递增变量指针,以便顺序访问数组中的每一个元素:实例#include<iostream>usingnamespacestd;constintMAX=3;intmain()......
  • 数据库:Hadoop实验
    Hadoop实验先单机下载、安装hadoop,启动:bin/hdfsnamenode-formatsbin/start-all.sh腾讯云新建三个机器,分别在两地(香港二区、香港三区),分别命名为master、slave1、slave2。修改四个配置文件运行hadoop:分别查看三台机器运行状态:查看HDFS运行状态:通过hdfs......
  • 第十四届蓝桥杯大赛软件赛国赛 C/C++ 大学 A 组
    Preface蓝桥杯战俘闪总出列!逆天比赛早上9点要赶到六七公里外的其它学校,因此早上7点就起来了然后坐公交颠着颠着就到了成都工业学院的门口,还刚好看到了lyy佬,就一起溜去考场了到了考场看了一圈好多熟悉的面孔,应该都是集训队的学长啥的,但好多名字还是叫不出来然后好像8点半就能......