打印本文 打印本文 关闭窗口 关闭窗口
一个功能强大的用ADO 访问数据库的类
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1811  更新时间:2009/4/23 10:48:54  文章录入:mintao  责任编辑:mintao
p; catch(_com_error e)
  {
     bRet = FALSE ;
  memset(lpBuff,0x00,500) ;
  sprintf(lpBuff,"更改数据库表时发生异常 \n:%s",e.ErrorMessage());
  AfxMessageBox(lpBuff) ;
  }

   return bRet  ;
}

BOOL CAdoEx::CloseAdo()
{
    if (m_bOpen)
 {
  m_pConnection->Close() ;
  TRACE0("Close ADO Connection !\n") ;
 }

 return TRUE ;
}

/*   说明:  这是一个 Ado 类,可以实现用ADO对数据库操作     */
/*   1. 连接数据库                                        */
/*   2. 查询数据库的一个或关联表                          */
/*   3. 执行SQL 语句插入 修改 删除 操作                   */
/*   4. 关闭数据库                                        */

#include "stdafx.h"
#include "AdoEx.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CAdoEx::CAdoEx()
{
   m_bOpen = FALSE ;
  
   /*初始化连接的实例*/
   m_pConnection.CreateInstance(_uuidof(Connection)) ;
}

CAdoEx::~CAdoEx()
{

  if(retRecordsetPtr !=NULL)
   retRecordsetPtr->Close() ;
  retRecordsetPtr = NULL ;

  CloseAdo() ;

  m_bOpen = FALSE ;
}
/*打开 Ado 数据库*/
BOOL CAdoEx::OpenAdo(LPCTSTR lpDSN ,LPCTSTR lpUID ,LPCTSTR lpPWD )
{
 BOOL bRet = FALSE ;
   
 if(m_bOpen) return TRUE ;

 try
 {
       hResult = m_pConnection->Open(lpDSN,lpUID,lpPWD,0)  ;
      
       if (SUCCEEDED(hResult))
    {
         TRACE0("Open ADO Database  Succeeded !\n") ;
         m_bOpen = TRUE ;
      bRet    = TRUE ;
    }
    else
    {
      m_bOpen = FALSE ;
         bRet    = FALSE ;
    }
 } /*end of try*/
 catch(_com_error e )
 {
  memset(lpBuff,0x00,500) ;
  sprintf(lpBuff,"打开数据库时发生异常 \n:%s",e.ErrorMessage());
  AfxMessageBox(lpBuff) ;
 }

 return bRet ;
}

/*     ADO 通用查询函数                      */ 
/*     调用参数: 正确的SQL 查询语句.         */
/*     可以对一个表查询,也可以对多个表查询   */
/*     返回一个纪录集                        */
/*     如果成功返回一个非空的纪录集          */
/*     如果失败则返回一个NULL的纪录集        */
_RecordsetPtr CAdoEx::Query(LPCTSTR lpQuery)
{
  retRecordsetPtr = NULL ;
  _variant_t vRecsAffected ;

  if(!m_bOpen) return NULL ;

  try
  {
    retRecordsetPtr = m_pConnection->Execute(lpQuery,
                                     &vRecsAffected,
                              adOptionUnspecified) ;
  }
  catch(_com_error e)
  {
  memset(lpBuff,0x00,500) ;
  sprintf(lpBuff,"查询数据库表时发生异常 \n:%s",e.ErrorMessage());
  AfxMessageBox(lpBuff) ;
  }

   return retRecordsetPtr ;
}

/*     ADO 通用SQL语句执行函数               */ 
/*     调用参数: 正确的SQL 查询语句.         */
/*     可以对一个表插入 修改 删除 操作       */
/*     如果成功返回真                        */
/*     如果失败返回假                        */
BOOL CAdoEx::Execute(LPCTSTR lpExcute)
{
  BOOL  bRet  = FALSE ;
  _variant_t vRecsAffected ;

  if(!m_bOpen) return NULL ;

  try
  {
     m_pConnection->Execute(lpExcute,
                      &vRecsAffected,
                   adOptionUnspecified) ;
    bRet = TRUE ;
  }
  catch(_com_error e)
  {
     bRet = FALSE ;
  memset(lpBuff,0x00,500) ;
  sprintf(lpBuff,"更改数据库表时发生异常 \n:%s",e.ErrorMessage());
  AfxMessageBox(lpBuff) ;
  }

   return bRet  ;
}

BOOL CAdoEx::CloseAdo()
{
    if (m_bOpen)
 {
  m_pConnection->Close() ;
  TRACE0("Close ADO Connection !\n") ;
 }

 return TRUE ;
}

 

 

 

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口