欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VC(实现自己的ADO类)  

程序员文章站 2022-05-19 20:21:32
...

.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;