封装ado

// Ado.h: interface for the CAdo class.
//
//
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace \
 rename("EOF","adoEOF")rename("BOF","adoBOF")

#include
#include "icrsint.h"

#if !defined(AFX_ADO_H__3E6D7357_14E7_48C2_AA53_76E77EDB364D__INCLUDED_)
#define AFX_ADO_H__3E6D7357_14E7_48C2_AA53_76E77EDB364D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CAdo 
{
public:
 _ConnectionPtr m_pConnection;
 _RecordsetPtr m_pRecordset;
 _CommandPtr   m_pCommand;
 CAdo();
 virtual ~CAdo();
public:
 bool FillRecordSet(CString &strSql);
 CString GetFeild(char* strFeild);
 bool bIsEof();
 bool bIsBof();
 bool ExcutCmdSql(CString &strSql);
 void ConnectDb(CString &strDb);
 void ExitConnect();
 HRESULT MoveNextFeild();

};

#endif // !defined(AFX_ADO_H__3E6D7357_14E7_48C2_AA53_76E77EDB364D__INCLUDED_)

 

// Ado.cpp: implementation of the CAdo class.
//
//

#include "stdafx.h"
#include "Ado.h"

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

//
// Construction/Destruction
//

CAdo::CAdo()
{
 AfxOleInit();
 m_pRecordset.CreateInstance(__uuidof(Recordset));
}

CAdo::~CAdo()
{

}

void CAdo::ConnectDb(CString &strDb)
{
 CString strconn ="Provider = Microsoft.Jet.OLEDB.4.0;Data Source="+strDb+";Persist Security Info=False";
 try
 {
  m_pConnection.CreateInstance("ADODB.Connection");
  m_pConnection->Open( (_bstr_t)strconn,"","",adModeUnknown); 
  
 }
 catch(_com_error e)
 {
  AfxMessageBox(e.Description());
 }
}

void CAdo::ExitConnect()
{
 //关闭记录集和连接
    if(m_pRecordset!=NULL)
  m_pRecordset->Close();
 m_pConnection->Close();
}

bool CAdo::FillRecordSet(CString &strSql)
{
 HRESULT hr;
 try
 {
  hr = m_pRecordset->Open((_bstr_t)strSql,m_pConnection.GetInterfacePtr(),adOpenDynamic,
  adLockOptimistic,adCmdText);
  if (FAILED(hr))
  {
   return false;
  }
 }
 catch(_com_error e)
 {
  AfxMessageBox(e.Description());
 }
 return true;
}

CString CAdo::GetFeild(char* strFeild)
{
 return  (char*)(_bstr_t)m_pRecordset->GetCollect(strFeild);
}

bool CAdo::ExcutCmdSql(CString &strSql)
{
 
 if (m_pCommand ==NULL)
 {
  HRESULT hr;
  hr = m_pCommand.CreateInstance(__uuidof(Command));
  if (FAILED(hr))
  {
   return false;
  }
 }

 if (m_pConnection != NULL)
 {
  m_pCommand->ActiveConnection = m_pConnection;
  m_pCommand->CommandText = (_bstr_t)strSql;
 }
 else
 {
  return false;
 }

 

 _variant_t vNULL;
 vNULL.vt = VT_ERROR;
 vNULL.scode = DISP_E_PARAMNOTFOUND;///定义为无参数
 m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdText);
//  _variant_t   vr;
//  m_pCommand->Execute(NULL,&vr,adCmdText);
//   m_pRecordset =m_pConnection->Execute((_bstr_t)strSql,&vr,adCmdText);
 return true;
}
HRESULT CAdo::MoveNextFeild()
{
 return m_pRecordset->MoveNext();
}
bool CAdo::bIsEof()
{
 if (m_pRecordset->adoEOF)
 {
  return true;
 }
 else
 {
  return false;
 }
 
}
bool CAdo::bIsBof()
{
 if (m_pRecordset->adoBOF)
 {
  return true;
 }
 else
 {
  return false;
 }
 
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12560256/viewspace-591209/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12560256/viewspace-591209/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值