// 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/