/************************************************************* /* Copyright (C), 2014-2020, lyfz. Co., Ltd. /* 文件名: ADORecordset.h /* 作者: Jeff.w /* 创建日期: 2014-08-16 /* 版本号: V1.0 /* 描述: 核心层底,数据库ADO编程 /* 其它: /* 主要类模块: /* 历史修改记录: /* 作者 时间 版本 描述 /* Jeff.w 14/08/18 1.0 创建这个模块 ***************************************************************/ #ifndef __ADO_RECORDSET_HEADER__ #define __ADO_RECORDSET_HEADER__ #pragma once #include "AdoDef.h" #include "ADODatabase.h" #include "ADOCommand.h" class CADORecordset { public: BOOL Clone(CADORecordset& pRs); enum cadoOpenEnum{ openUnknown = 0, openQuery = 1, openTable = 2, openStoredProc = 3, openCmdText = 4 }; enum cadoEditEnum{ dbEditNone = 0, dbEditNew = 1, dbEdit = 2 }; enum cadoPositionEnum{ positionUnknown = -1, positionBOF = -2, positionEOF = -3 }; enum cadoSearchEnum { searchForward = 1, searchBackward = -1 }; enum cadoDataType { typeEmpty = adEmpty, typeTinyInt = adTinyInt, typeSmallInt = adSmallInt, typeInteger = adInteger, typeBigInt = adBigInt, typeUnsignedTinyInt = adUnsignedTinyInt, typeUnsignedSmallInt = adUnsignedSmallInt, typeUnsignedInt = adUnsignedInt, typeUnsignedBigInt = adUnsignedBigInt, typeSingle = adSingle, typeDouble = adDouble, typeCurrency = adCurrency, typeDecimal = adDecimal, typeNumeric = adNumeric, typeBoolean = adBoolean, typeError = adError, typeUserDefined = adUserDefined, typeVariant = adVariant, typeIDispatch = adIDispatch, typeIUnknown = adIUnknown, typeGUID = adGUID, typeDate = adDate, typeDBDate = adDBDate, typeDBTime = adDBTime, typeDBTimeStamp = adDBTimeStamp, typeBSTR = adBSTR, typeChar = adChar, typeVarChar = adVarChar, typeLongVarChar = adLongVarChar, typeWChar = adWChar, typeVarWChar = adVarWChar, typeLongVarWChar = adLongVarWChar, typeBinary = adBinary, typeVarBinary = adVarBinary, typeLongVarBinary = adLongVarBinary, typeChapter = adChapter, typeFileTime = adFileTime, typePropVariant = adPropVariant, typeVarNumeric = adVarNumeric, typeArray = adVariant }; enum cadoSchemaType { schemaSpecific = adSchemaProviderSpecific, schemaAsserts = adSchemaAsserts, schemaCatalog = adSchemaCatalogs, schemaCharacterSet = adSchemaCharacterSets, schemaCollections = adSchemaCollations, schemaColumns = adSchemaColumns, schemaConstraints = adSchemaCheckConstraints, schemaConstraintColumnUsage = adSchemaConstraintColumnUsage, schemaConstraintTableUsage = adSchemaConstraintTableUsage, shemaKeyColumnUsage = adSchemaKeyColumnUsage, schemaTableConstraints = adSchemaTableConstraints, schemaColumnsDomainUsage = adSchemaColumnsDomainUsage, schemaIndexes = adSchemaIndexes, schemaColumnPrivileges = adSchemaColumnPrivileges, schemaTablePrivileges = adSchemaTablePrivileges, schemaUsagePrivileges = adSchemaUsagePrivileges, schemaProcedures = adSchemaProcedures, schemaTables = adSchemaTables, schemaProviderTypes = adSchemaProviderTypes, schemaViews = adSchemaViews, schemaViewTableUsage = adSchemaViewTableUsage, schemaProcedureParameters = adSchemaProcedureParameters, schemaForeignKeys = adSchemaForeignKeys, schemaPrimaryKeys = adSchemaPrimaryKeys, schemaProcedureColumns = adSchemaProcedureColumns, schemaDBInfoKeywords = adSchemaDBInfoKeywords, schemaDBInfoLiterals = adSchemaDBInfoLiterals, schemaCubes = adSchemaCubes, schemaDimensions = adSchemaDimensions, schemaHierarchies = adSchemaHierarchies, schemaLevels = adSchemaLevels, schemaMeasures = adSchemaMeasures, schemaProperties = adSchemaProperties, schemaMembers = adSchemaMembers, }; BOOL SetFieldValue(int nIndex, int nValue); BOOL SetFieldValue(LPCTSTR lpFieldName, int nValue); BOOL SetFieldValue(int nIndex, long lValue); BOOL SetFieldValue(LPCTSTR lpFieldName, long lValue); BOOL SetFieldValue(int nIndex, unsigned long lValue); BOOL SetFieldValue(LPCTSTR lpFieldName, unsigned long lValue); BOOL SetFieldValue(int nIndex, double dblValue); BOOL SetFieldValue(LPCTSTR lpFieldName, double dblValue); BOOL SetFieldValue(int nIndex, CString strValue); BOOL SetFieldValue(LPCTSTR lpFieldName, CString strValue); BOOL SetFieldValue(int nIndex, COleDateTime time); BOOL SetFieldValue(LPCTSTR lpFieldName, COleDateTime time); BOOL SetFieldValue(int nIndex, bool bValue); BOOL SetFieldValue(LPCTSTR lpFieldName, bool bValue); BOOL SetFieldValue(int nIndex, COleCurrency cyValue); BOOL SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue); BOOL SetFieldValue(int nIndex, _variant_t vtValue); BOOL SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue); BOOL SetFieldEmpty(int nIndex); BOOL SetFieldEmpty(LPCTSTR lpFieldName); void CancelUpdate(); BOOL Update(); void Edit(); BOOL AddNew(); BOOL AddNew(CADORecordBinding &pAdoRecordBinding); BOOL Find(LPCTSTR lpFind, int nSearchDirection = CADORecordset::searchForward); BOOL FindFirst(LPCTSTR lpFind); BOOL FindNext(); CADORecordset(); CADORecordset(CADODatabase* pAdoDatabase); virtual ~CADORecordset() { Close(); if(m_pRecordset) m_pRecordset.Release(); if(m_pCmd) m_pCmd.Release(); m_pRecordset = NULL; m_pCmd = NULL; m_pRecBinding = NULL; m_strQuery = _T(""); m_strLastError = _T(""); m_dwLastError = 0; m_nEditStatus = dbEditNone; } inline CString GetQuery() {return m_strQuery;}; inline void SetQuery(LPCSTR strQuery) {m_strQuery = strQuery;}; BOOL RecordBinding(CADORecordBinding &pAdoRecordBinding); DWORD GetRecordCount(); BOOL IsOpen(); void Close(); BOOL Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec = _T(""), const int& nOption = CADORecordset::openUnknown, const int& nCursorLocation = adUseServer); BOOL Open(LPCTSTR lpstrExec = _T(""), const int& nOption = CADORecordset::openUnknown, const int& nCursorLocation = adUseServer); BOOL OpenSchema(int nSchema, LPCTSTR SchemaID = _T("")); inline long GetFieldCount() {return m_pRecordset->Fields->GetCount();}; BOOL GetFieldValue(LPCTSTR lpFieldName, int& nValue); BOOL GetFieldValue(int nIndex, int& nValue); BOOL GetFieldValue(LPCTSTR lpFieldName, long& lValue); BOOL GetFieldValue(int nIndex, long& lValue); BOOL GetFieldValue(LPCTSTR lpFieldName, unsigned long& ulValue); BOOL GetFieldValue(int nIndex, unsigned long& ulValue); BOOL GetFieldValue(LPCTSTR lpFieldName, double& dbValue); BOOL GetFieldValue(int nIndex, double& dbValue); BOOL GetFieldValue(LPCTSTR lpFieldName, CString& strValue, CString strDateFormat = _T("")); BOOL GetFieldValue(int nIndex, CString& strValue, CString strDateFormat = _T("")); BOOL GetFieldValue(LPCTSTR lpFieldName, COleDateTime& time); BOOL GetFieldValue(int nIndex, COleDateTime& time); BOOL GetFieldValue(int nIndex, bool& bValue); BOOL GetFieldValue(LPCTSTR lpFieldName, bool& bValue); BOOL GetFieldValue(int nIndex, COleCurrency& cyValue); BOOL GetFieldValue(LPCTSTR lpFieldName, COleCurrency& cyValue); BOOL GetFieldValue(int nIndex, _variant_t& vtValue); BOOL GetFieldValue(LPCTSTR lpFieldName, _variant_t& vtValue); BOOL IsFieldNull(LPCTSTR lpFieldName); BOOL IsFieldNull(int nIndex); BOOL IsFieldEmpty(LPCTSTR lpFieldName); BOOL IsFieldEmpty(int nIndex); inline BOOL IsEof(){return m_pRecordset->EndOfFile == VARIANT_TRUE;}; inline BOOL IsEOF(){return m_pRecordset->EndOfFile == VARIANT_TRUE;}; inline BOOL IsBof(){return m_pRecordset->BOF == VARIANT_TRUE;}; inline BOOL IsBOF(){return m_pRecordset->BOF == VARIANT_TRUE;}; inline void MoveFirst() {m_pRecordset->MoveFirst();}; inline void MoveNext() {m_pRecordset->MoveNext();}; inline void MovePrevious() {m_pRecordset->MovePrevious();}; inline void MoveLast() {m_pRecordset->MoveLast();}; inline long GetAbsolutePage(){return m_pRecordset->GetAbsolutePage();}; inline void SetAbsolutePage(int nPage) {m_pRecordset->PutAbsolutePage((enum PositionEnum)nPage);}; inline long GetPageCount() {return m_pRecordset->GetPageCount();}; inline long GetPageSize() {return m_pRecordset->GetPageSize();}; inline void SetPageSize(int nSize) {m_pRecordset->PutPageSize(nSize);}; inline long GetAbsolutePosition() {return m_pRecordset->GetAbsolutePosition();}; inline void SetAbsolutePosition(int nPosition) {m_pRecordset->PutAbsolutePosition((enum PositionEnum)nPosition);}; //inline void Move(ADO_LONGPTR NumRecords){m_pRecordset->Move(NumRecords);} BOOL GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo* fldInfo); BOOL GetFieldInfo(int nIndex, CADOFieldInfo* fldInfo); BOOL AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes); BOOL AppendChunk(int nIndex, LPVOID lpData, UINT nBytes); BOOL AppendChunkFromFile(LPCTSTR lpFieldName, TCHAR* strFileName); BOOL AppendChunkFromFile(int nIndex, TCHAR* strFileName); BOOL GetChunk(LPCTSTR lpFieldName, CString& strValue); BOOL GetChunk(int nIndex, CString& strValue); BOOL GetChunk(LPCTSTR lpFieldName, LPVOID pData); BOOL GetChunk(int nIndex, LPVOID pData); LONG GetChunkEx(LPCTSTR lpFieldName, HGLOBAL *hglobal); LONG GetChunkEx(int nIndex, HGLOBAL *hglobal); LONG GetChunkToFile(LPCTSTR lpFieldName, TCHAR* strFileName); LONG GetChunkToFile(int nIndex, TCHAR* strFileName); CString GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull, long numRows = 0); inline CString GetLastErrorString() {return m_strLastError;}; inline DWORD GetLastError(){return m_dwLastError;}; inline void GetBookmark() {m_varBookmark = m_pRecordset->Bookmark;}; BOOL SetBookmark(); BOOL Delete(); inline BOOL IsConnectionOpen() {return m_pConnection != NULL && m_pConnection->GetState() != adStateClosed;}; inline _RecordsetPtr GetRecordset(){return m_pRecordset;}; inline _ConnectionPtr GetActiveConnection() {return m_pConnection;}; BOOL SetFilter(LPCTSTR strFilter); BOOL SetSort(LPCTSTR lpstrCriteria); BOOL SaveAsXML(LPCTSTR lpstrXMLFile); BOOL OpenXML(LPCTSTR lpstrXMLFile); BOOL Execute(CADOCommand* pCommand); BOOL Requery(); public: _RecordsetPtr m_pRecordset; _CommandPtr m_pCmd; protected: _ConnectionPtr m_pConnection; int m_nSearchDirection; CString m_strFind; _variant_t m_varBookFind; _variant_t m_varBookmark; int m_nEditStatus; CString m_strLastError; DWORD m_dwLastError; void dump_com_error(_com_error &e); IADORecordBinding *m_pRecBinding; CString m_strQuery; protected: BOOL PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld); BOOL PutFieldValue(_variant_t vtIndex, _variant_t vtFld); BOOL GetFieldInfo(FieldPtr pField, CADOFieldInfo* fldInfo); BOOL GetChunk(FieldPtr pField, CString& strValue); BOOL GetChunk(FieldPtr pField, LPVOID lpData); BOOL AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes); }; #endif