7. DBRecordSet.h
#pragma once
#include <comutil.h> #pragma comment(lib,"comsupp.lib")
class CDBRecordSetImpl ; class CDBConnection ;
class AFX_EXT_CLASS CDBRecordSet { //构造函数和析构函数 public: CDBRecordSet(void); virtual ~CDBRecordSet(void);
//接口函数 public:
/* * 打开记录集 * [in] szSQL -> SQL语句, select ...... * [out] rConnection -> 本次查询使用的数据库连接 */ BOOL Open(LPCTSTR szSQL,CDBConnection& rConnection) ;
//记录集是否处于打开状态 BOOL IsOpened() ;
//关闭记录集 BOOL Close(void) ;
//是否记录当前位置位于最后一条记录后面 BOOL IsEof(void);
//是否当前位置位于第一条记录前面 BOOL IsBof(void);
/* * 获取记录总数 * 返回值: * -1 -> 错误 * >=0 -> 记录数 */ int GetRecordCount(void) ;
//移动到第一个记录 BOOL MoveFirst(void) ;
//移到下一个记录 BOOL MoveNext(void) ;
//移动到最后一个记录 BOOL MoveLast(void) ;
//移动到前一个记录 BOOL MovePrevious(void) ;
/* * 移动到指定记录 * 参数: * [in] nOffset -> 相对当前位置移动nOffset个记录,nOffset < 0 表示向前移动,nOffset > 0表示向后移动 */ BOOL Move(int nOffset) ;
/* * 获取当前记录集中的列总数 * -1 -> 错误 * >=0 -> 列总数 */ int GetFieldCount(void) ;
/* 获取列号,从0开始 参数 [in] szFieldName 列名 返回值 列号,-1表示没有找到 */ int GetFieldOrder(LPCTSTR szFieldName) ;
/* 获取列名 参数 [in] nFieldOrder列号,从0开始 返回值 列名 , 返回空串表示没有该序号对应的字段 */ LPCTSTR GetFieldName(int nFieldOrder) ;
/* 获取列值,返回字符串,若是日期,则按照数据库默认的日期格式返回 参数 [in] nFieldOrder -> 列号,从0开始 [out] strValue -> 返回该序号对应的字段值的字符串 返回值: TRUE 字段值成功获取 FALSE 获取字段值失败 */ BOOL GetFieldValue(int nFieldOrder,CString& strValue);
/* 获取列值,返回字符串,若是日期,则按照数据库默认的日期格式返回 参数: [in] nFieldOrder -> 列号,从0开始 返回值: 列值, 返回空串表示没有该序号对应的字段值或者该字段值为空 */ LPCTSTR GetFieldValue(int nFieldOrder);
/* 获取列值,返回整型,若字段不是整型或者可以转换为整型的字段,则返回失败 参数 [in] nFieldOrder -> 列号,从0开始 [out] nValue -> 返回该序号对应的字段值的整型值 返回值: TRUE 字段值成功获取 FALSE 获取字段值失败 */ BOOL GetFieldValueInt(int nFieldOrder,int& nValue);
/* 获取列值,返回整型,若字段不是整型或者可以转换为整型的字段,则返回失败 参数: [in] nFieldOrder -> 列号,从0开始 返回值: 列值, 返回值不检查是否成功 */ int GetFieldValueInt(int nFieldOrder);
/* 获取列值,返回浮点型,若字段不是浮点型或者可以转换为浮点型的字段,则返回失败 参数 [in] nFieldOrder -> 列号,从0开始 [out] nValue -> 返回该序号对应的字段值的浮点型值 返回值: TRUE 字段值成功获取 FALSE 获取字段值失败 */ BOOL GetFieldValueFloat(int nFieldOrder,float& fValue);
/* 获取列值,返回浮点型,若字段不是浮点型或者可以转换为浮点型的字段,则返回失败 参数: [in] nFieldOrder -> 列号,从0开始 返回值: 列值, 返回值不检查是否成功 */ float GetFieldValueFloat(int nFieldOrder);
/* * 设置错误信息 * 参数: * [in] szErrMsg -> 错误信息 * [in] szSourceFile -> 发生错误的源文件 * [in] nLine -> 发生错误的行号 */ void SetErrorMessage(LPCTSTR szErrMsg,const char* szSourceFile=NULL,int nLine=0);
//获取错误信息 LPCTSTR GetErrorMessage(void) const;
//记录集的复制采用clone的方法,当一个记录集被复制以后,得到两个独立的记录集, //其中一个关闭以后,另外一个仍然可用。 public: //copy 构造函数 CDBRecordSet(const CDBRecordSet& rRecordSet) ;
//赋值运算符 CDBRecordSet& operator= (const CDBRecordSet& rRecordSet);
protected: CDBRecordSetImpl* m_pRecordSetImpl ; //临时字段名值 CString m_strTempFieldName ; //临时字段名 CString m_strTempFieldValue ; }; //////////////////////////////////////////////////////////// 8. DBRecordSet.cpp
#include "StdAfx.h" #include "dbrecordset.h" #include "DBErrorMsgDefs.h" #include "DBRecordSetImpl.h" #include "DBConnection.h"
#define DBRECORDSETPtr m_pRecordSetImpl->GetRecordSet()
CDBRecordSet::CDBRecordSet(void) { m_pRecordSetImpl = new CDBRecordSetImpl() ; }
CDBRecordSet::~CDBRecordSet(void) { if(m_pRecordSetImpl) { delete m_pRecordSetImpl ; m_pRecordSetImpl = NULL ; } }
//copy 构造函数 CDBRecordSet::CDBRecordSet(const CDBRecordSet& rRecordSet) : m_pRecordSetImpl(NULL) { *this = rRecordSet ; }
//赋值运算符 CDBRecordSet& CDBRecordSet::operator= (const CDBRecordSet& rRecordSet) { if(this == &rRecordSet) { return *this ; } if(rRecordSet.m_pRecordSetImpl == NULL) { if(m_pRecordSetImpl != NULL) { delete m_pRecordSetImpl ; m_pRecordSetImpl = NULL ; } } else { if(m_pRecordSetImpl == NULL) { m_pRecordSetImpl = new CDBRecordSetImpl() ; } *m_pRecordSetImpl = *rRecordSet.m_pRecordSetImpl ; //copy it } return *this ; }
void CDBRecordSet::SetErrorMessage(LPCTSTR szErrMsg,const char* szSourceFile,int nLine) { m_pRecordSetImpl->SetErrorMessage(szErrMsg,szSourceFile,nLine) ; }
LPCTSTR CDBRecordSet::GetErrorMessage(void) const { return (LPCTSTR)m_pRecordSetImpl->GetErrorMessage() ; }
BOOL CDBRecordSet::Open(LPCTSTR szSQL,CDBConnection& rConnection) { if(szSQL == NULL) { SetErrorMessage(SQL_EMPTYSQL,__FILE__,__LINE__) ; return FALSE ; } else if(_tcslen(szSQL) == 0) { SetErrorMessage(SQL_EMPTYSQL,__FILE__,__LINE__) ; return FALSE ; }
//check if is connected if(!rConnection.IsOpened()) { SetErrorMessage(CONNCTION_NOTCONNECT,__FILE__,__LINE__) ; return FALSE ; }
try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; }
//check if is already opened if(DBRECORDSETPtr->GetState() != adStateClosed) { //if is opened,close it first DBRECORDSETPtr->Close() ; }
_variant_t vActiveConnection ; if(!rConnection.GetConnectionIDispatch(vActiveConnection)) { SetErrorMessage(CONNCTION_IDISPATCHERROR,__FILE__,__LINE__) ; return FALSE ; }
DBRECORDSETPtr->CursorLocation = adUseClient ; HRESULT hr = DBRECORDSETPtr->Open( _variant_t(szSQL), vActiveConnection, adOpenKeyset, adLockReadOnly, adOptionUnspecified) ; return (SUCCEEDED(hr)) ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
//关闭记录集 BOOL CDBRecordSet::Close(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; }
HRESULT hr = S_OK ; if(DBRECORDSETPtr->GetState() == adStateOpen) { hr = DBRECORDSETPtr->Close() ; } return (SUCCEEDED(hr)) ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
BOOL CDBRecordSet::IsOpened() { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; }
if(DBRECORDSETPtr->GetState() == adStateOpen) { return TRUE ; } } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
BOOL CDBRecordSet::IsEof(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return TRUE ; } BOOL bEnd = TRUE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bEnd = (BOOL)(DBRECORDSETPtr->GetEndOfFile()); } return bEnd ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return TRUE ; }
//是否当前位置位于第一条记录前面 BOOL CDBRecordSet::IsBof(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return TRUE ; } BOOL bEnd = TRUE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bEnd = (BOOL)(DBRECORDSETPtr->GetBOF()); } return bEnd ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return TRUE ; }
//获取记录总数 int CDBRecordSet::GetRecordCount(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return -1 ; }
int nRecordCount = -1 ; if(DBRECORDSETPtr->GetState() == adStateOpen) { nRecordCount = (int)DBRECORDSETPtr->GetRecordCount() ; } return nRecordCount ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return -1 ; }
//移动到第一个记录 BOOL CDBRecordSet::MoveFirst(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; } BOOL bMovedOK = FALSE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bMovedOK = SUCCEEDED(DBRECORDSETPtr->MoveFirst()) ; } return bMovedOK ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
//移到下一个记录 BOOL CDBRecordSet::MoveNext(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; } BOOL bMovedOK = FALSE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bMovedOK = SUCCEEDED(DBRECORDSETPtr->MoveNext()) ; } return bMovedOK ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
//移动到最后一个记录 BOOL CDBRecordSet::MoveLast(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; } BOOL bMovedOK = FALSE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bMovedOK = SUCCEEDED(DBRECORDSETPtr->MoveLast()) ; } return bMovedOK ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
//移动到前一个记录 BOOL CDBRecordSet::MovePrevious(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; } BOOL bMovedOK = FALSE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bMovedOK = SUCCEEDED(DBRECORDSETPtr->MovePrevious()) ; } return bMovedOK ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
BOOL CDBRecordSet::Move(int nOffset) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; } BOOL bMovedOK = FALSE ; if(DBRECORDSETPtr->GetState() == adStateOpen) { bMovedOK = SUCCEEDED(DBRECORDSETPtr->Move((long)nOffset)) ; } return bMovedOK ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
int CDBRecordSet::GetFieldCount(void) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return -1 ; }
int nFieldCount = -1 ; if(DBRECORDSETPtr->GetState() == adStateOpen) { if(DBRECORDSETPtr->GetFields() != NULL) { //记录集中包含的字段数 nFieldCount = (int)DBRECORDSETPtr->GetFields()->GetCount() ; } } return nFieldCount ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return -1 ; }
int CDBRecordSet::GetFieldOrder(LPCTSTR szFieldName) { try { if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return -1 ; }
int nFieldOrder = -1 ; if(DBRECORDSETPtr->GetState() == adStateOpen) { if(DBRECORDSETPtr->GetFields() != NULL) { //记录集中包含的字段数 long nFieldCount = (int)DBRECORDSETPtr->GetFields()->GetCount() ; for(long nIndex = 0L; nIndex < nFieldCount; nIndex++) { if(DBRECORDSETPtr->GetFields()->Item[nIndex]->Name == _bstr_t(szFieldName)) { nFieldOrder = nIndex ; break ; //找到该字段 } } } } return nFieldOrder ; } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return -1 ; }
LPCTSTR CDBRecordSet::GetFieldName(int nFieldOrder) { try { m_strTempFieldName.Empty() ; if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return (LPCTSTR)m_strTempFieldName; }
if(DBRECORDSETPtr->GetState() == adStateOpen) { if(DBRECORDSETPtr->GetFields() != NULL) { //记录集中包含的字段数 long nFieldCount = (int)DBRECORDSETPtr->GetFields()->GetCount() ; if(nFieldCount > 0) { if((nFieldOrder >= 0) && (nFieldOrder < nFieldCount) ) { m_strTempFieldName = (LPCTSTR)DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->Name ; } } } } } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return (LPCTSTR)m_strTempFieldName ; }
BOOL CDBRecordSet::GetFieldValue(int nFieldOrder,CString& strValue) { try { strValue = _T("") ; if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; }
if(DBRECORDSETPtr->GetState() == adStateOpen) { if(DBRECORDSETPtr->GetFields() != NULL) { //记录集中包含的字段数 long nFieldCount = (int)DBRECORDSETPtr->GetFields()->GetCount() ; if(nFieldCount > 0) { if((nFieldOrder >= 0) && (nFieldOrder < nFieldCount) ) { //保证字段值不为空才取值,否则会出现异常 if(DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->ActualSize > 0L) { strValue = (LPCTSTR)(DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->Value).operator _bstr_t() ; return TRUE ; } } } } } } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
LPCTSTR CDBRecordSet::GetFieldValue(int nFieldOrder) { m_strTempFieldValue.Empty() ; if(!GetFieldValue(nFieldOrder,m_strTempFieldValue)) { m_strTempFieldValue.Empty() ; } return (LPCTSTR)m_strTempFieldValue ; }
BOOL CDBRecordSet::GetFieldValueInt(int nFieldOrder,int& nValue) { try { nValue = -1 ; if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; }
if(DBRECORDSETPtr->GetState() == adStateOpen) { if(DBRECORDSETPtr->GetFields() != NULL) { //记录集中包含的字段数 long nFieldCount = (int)DBRECORDSETPtr->GetFields()->GetCount() ; if(nFieldCount > 0) { if((nFieldOrder >= 0) && (nFieldOrder < nFieldCount) ) { //保证字段值不为空才取值,否则会出现异常 if(DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->ActualSize > 0L) { nValue = (int)(DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->Value).operator long() ; return TRUE; } } } } } } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
int CDBRecordSet::GetFieldValueInt(int nFieldOrder) { int nValue = 0; if(!GetFieldValueInt(nFieldOrder,nValue)) { nValue = 0 ; } return nValue ; }
BOOL CDBRecordSet::GetFieldValueFloat(int nFieldOrder,float& fValue) { try { fValue = -1 ; if (!m_pRecordSetImpl->isValid()) { SetErrorMessage(RECORDSET_INVALIDHANDLE,__FILE__,__LINE__) ; return FALSE ; }
if(DBRECORDSETPtr->GetState() == adStateOpen) { if(DBRECORDSETPtr->GetFields() != NULL) { //记录集中包含的字段数 long nFieldCount = (int)DBRECORDSETPtr->GetFields()->GetCount() ; if(nFieldCount > 0) { if((nFieldOrder >= 0) && (nFieldOrder < nFieldCount) ) { //保证字段值不为空才取值,否则会出现异常 if(DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->ActualSize > 0L) { fValue = (float)(DBRECORDSETPtr->GetFields()->Item[(long)nFieldOrder]->Value).operator float() ; return TRUE; } } } } } } catch (_com_error &e) { SetErrorMessage((LPCTSTR)e.Description(),__FILE__,__LINE__) ; } catch (...) { SetErrorMessage(EXCEPTION_UNKNOWN,__FILE__,__LINE__) ; } return FALSE ; }
float CDBRecordSet::GetFieldValueFloat(int nFieldOrder) { float fValue = 0.0f ; if(!GetFieldValueFloat(nFieldOrder,fValue)) { fValue = 0.0f ; } return fValue ; }
#undef DBRECORDSETPtr
//end

|