//声明文件
#pragma once
class CDiyAdo
{
public:
CDiyAdo(void);
~CDiyAdo();
public:
CString strErrMsg;
BOOL bConn;
_ConnectionPtr pConn;
public:
BOOL OpenConn(CString strConn, CString strUser = _T(""), CString strPwd = _T(""), long nOpt = adConnectUnspecified);
BOOL CloseConn();
BOOL BeginTrans();
BOOL CommitTrans();
BOOL RollbackTrans();
public:
BOOL bRs;
_RecordsetPtr pRs;
public:
BOOL OpenRs(CString strSql);
BOOL CloseRs();
BOOL IsBOF();
BOOL IsEOF();
void MoveFirst();
void MoveLast();
void MovePrev();
void MoveNext();
void AddNew();
void Delete(AffectEnum nAffect);
void Update();
CString GetFldValueAsStr(CString strCol);
int GetFldValueAsInt(CString strCol);
double GetFldValueAsFloat(CString strCol);
void PutFldValueAsStr(CString strCol, CString strValue);
void PutFldValueAsInt(CString strCol, int nValue);
void PutFldValueAsFloat(CString strCol, double fValue);
};
//实现文件
#include "StdAfx.h"
#include "DiyAdo.h"
CDiyAdo::CDiyAdo(void)
{
::CoInitialize(NULL);
pConn.CreateInstance(_T("ADODB.Connection"));
pConn->PutCursorLocation(adUseClient);
bConn = FALSE;
/
pRs.CreateInstance(_T("ADODB.Recordset"));
bRs = FALSE;
}
CDiyAdo::~CDiyAdo()
{
if(bRs) CloseRs();
pRs.Release();
/
if(bConn) CloseRs();
pConn.Release();
::CoUninitialize();
}
BOOL CDiyAdo::OpenConn(CString strConn, CString strUser, CString strPwd, long nOpt)
{
try{
if(pConn == NULL) pConn.CreateInstance(_T("ADODB.Connection"));
pConn->Open((_bstr_t)strConn, (_bstr_t)strUser, (_bstr_t)strPwd, nOpt);
}
catch(_com_error e){
strErrMsg.Format(_T("连接数据库失败!"));
AfxMessageBox(e.Description());
bConn = FALSE;
return FALSE;
}
bConn = TRUE;
return TRUE;
}
BOOL CDiyAdo::CloseConn()
{
if(pConn == NULL) return FALSE;
if(pConn->GetState() != adStateOpen) return FALSE;
try{
pConn->Close();
}
catch(_com_error e){
strErrMsg.Format(_T("断开数据库失败!"));
AfxMessageBox(e.Description());
return FALSE;
}
bConn = FALSE;
return TRUE;
}
BOOL CDiyAdo::BeginTrans()
{
if(!bConn) return FALSE;
return pConn->BeginTrans();
}
BOOL CDiyAdo::CommitTrans()
{
if(!bConn) return FALSE;
return pConn->CommitTrans();
}
BOOL CDiyAdo::RollbackTrans()
{
if(!bConn) return FALSE;
return pConn->RollbackTrans();
}
/
BOOL CDiyAdo::OpenRs(CString strSql)
{
try{
if(pRs == NULL) pRs.CreateInstance(_T("ADODB.Recordset"));
pRs->Open((_bstr_t)strSql, pConn.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error e){
strErrMsg.Format(_T("结果集打开失败!"));
AfxMessageBox(e.Description());
bRs = FALSE;
return FALSE;
}
bRs = TRUE;
return TRUE;
}
BOOL CDiyAdo::CloseRs()
{
if(pRs == NULL) return FALSE;
if(pRs->GetState() != adStateOpen) return FALSE;
try{
pRs->Close();
}
catch(_com_error e){
strErrMsg.Format(_T("结果集关闭失败!"));
AfxMessageBox(e.Description());
return FALSE;
}
bRs = FALSE;
return TRUE;
}
BOOL CDiyAdo::IsBOF()
{
if(pRs->adoBOF) return TRUE;
else return FALSE;
}
BOOL CDiyAdo::IsEOF()
{
if(pRs->adoEOF) return TRUE;
else return FALSE;
}
void CDiyAdo::MoveFirst()
{
if(pRs == NULL) return;
pRs->MoveFirst();
}
void CDiyAdo::MoveLast()
{
if(pRs == NULL) return;
pRs->MoveLast();
}
void CDiyAdo::MovePrev()
{
if(pRs->adoBOF) return;
pRs->MovePrevious();
}
void CDiyAdo::MoveNext()
{
if(pRs->adoEOF) return;
pRs->MoveNext();
}
void CDiyAdo::AddNew()
{
pRs->AddNew();
}
void CDiyAdo::Delete(AffectEnum nAffect)
{
pRs->Delete(nAffect);
}
void CDiyAdo::Update()
{
pRs->Update();
}
CString CDiyAdo::GetFldValueAsStr(CString strCol)
{
try{
CString strValue = pRs->GetCollect((_bstr_t)strCol);
return strValue;
}
catch(_com_error e){
AfxMessageBox(e.Description());
}
return _T("");
}
int CDiyAdo::GetFldValueAsInt(CString strCol)
{
try{
CString strValue = pRs->GetCollect((_bstr_t)strCol);
int nValue = _ttoi(strValue);
return nValue;
}
catch(_com_error e){
AfxMessageBox(e.Description());
}
return -1;
}
double CDiyAdo::GetFldValueAsFloat(CString strCol)
{
try{
CString strValue = pRs->GetCollect((_bstr_t)strCol);
double fValue = _ttof(strValue);
return fValue;
}
catch(_com_error e){
AfxMessageBox(e.Description());
}
return -1.0;
}
void CDiyAdo::PutFldValueAsStr(CString strCol, CString strValue)
{
try{
pRs->GetFields()->GetItem((_bstr_t)strCol)->Value = (_bstr_t)strValue;
}
catch(_com_error e){
AfxMessageBox(e.Description());
}
}
void CDiyAdo::PutFldValueAsInt(CString strCol, int nValue)
{
try{
CString strValue; strValue.Format(_T("%d"), nValue);
pRs->GetFields()->GetItem((_bstr_t)strCol)->Value = (_bstr_t)strValue;
}
catch(_com_error e){
AfxMessageBox(e.Description());
}
}
void CDiyAdo::PutFldValueAsFloat(CString strCol, double fValue)
{
try{
CString strValue; strValue.Format(_T("%0.4f"), fValue);
pRs->GetFields()->GetItem((_bstr_t)strCol)->Value = (_bstr_t)strValue;
}
catch(_com_error e){
AfxMessageBox(e.Description());
}
}
//测试客户端
mpRegion.RemoveAll();
CString strSql, strCode, strName; strCode.Format(_T("")); strName.Format(_T(""));
strSql.Format(_T("Select Code, Place From Region Where Code like '%s%%' and Len(Code) = %d"), strRegion, n);
Ado.OpenRs(strSql);
while(!Ado.IsEOF()){
strCode = Ado.GetFldValueAsStr(_T("Code"));
strName = Ado.GetFldValueAsStr(_T("Place"));
mpRegion[strCode] = strName;
Ado.MoveNext();
}
Ado.CloseRs();