ADORecordset.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*************************************************************
  2. /* Copyright (C), 2014-2020, lyfz. Co., Ltd.
  3. /* 文件名: ADORecordset.h
  4. /* 作者: Jeff.w
  5. /* 创建日期: 2014-08-16
  6. /* 版本号: V1.0
  7. /* 描述: 核心层底,数据库ADO编程
  8. /* 其它:
  9. /* 主要类模块:
  10. /* 历史修改记录:
  11. /* 作者 时间 版本 描述
  12. /* Jeff.w 14/08/18 1.0 创建这个模块
  13. ***************************************************************/
  14. #ifndef __ADO_RECORDSET_HEADER__
  15. #define __ADO_RECORDSET_HEADER__
  16. #pragma once
  17. #include "AdoDef.h"
  18. #include "ADODatabase.h"
  19. #include "ADOCommand.h"
  20. class CADORecordset
  21. {
  22. public:
  23. BOOL Clone(CADORecordset& pRs);
  24. enum cadoOpenEnum{
  25. openUnknown = 0,
  26. openQuery = 1,
  27. openTable = 2,
  28. openStoredProc = 3,
  29. openCmdText = 4
  30. };
  31. enum cadoEditEnum{
  32. dbEditNone = 0,
  33. dbEditNew = 1,
  34. dbEdit = 2
  35. };
  36. enum cadoPositionEnum{
  37. positionUnknown = -1,
  38. positionBOF = -2,
  39. positionEOF = -3
  40. };
  41. enum cadoSearchEnum {
  42. searchForward = 1,
  43. searchBackward = -1
  44. };
  45. enum cadoDataType {
  46. typeEmpty = adEmpty,
  47. typeTinyInt = adTinyInt,
  48. typeSmallInt = adSmallInt,
  49. typeInteger = adInteger,
  50. typeBigInt = adBigInt,
  51. typeUnsignedTinyInt = adUnsignedTinyInt,
  52. typeUnsignedSmallInt = adUnsignedSmallInt,
  53. typeUnsignedInt = adUnsignedInt,
  54. typeUnsignedBigInt = adUnsignedBigInt,
  55. typeSingle = adSingle,
  56. typeDouble = adDouble,
  57. typeCurrency = adCurrency,
  58. typeDecimal = adDecimal,
  59. typeNumeric = adNumeric,
  60. typeBoolean = adBoolean,
  61. typeError = adError,
  62. typeUserDefined = adUserDefined,
  63. typeVariant = adVariant,
  64. typeIDispatch = adIDispatch,
  65. typeIUnknown = adIUnknown,
  66. typeGUID = adGUID,
  67. typeDate = adDate,
  68. typeDBDate = adDBDate,
  69. typeDBTime = adDBTime,
  70. typeDBTimeStamp = adDBTimeStamp,
  71. typeBSTR = adBSTR,
  72. typeChar = adChar,
  73. typeVarChar = adVarChar,
  74. typeLongVarChar = adLongVarChar,
  75. typeWChar = adWChar,
  76. typeVarWChar = adVarWChar,
  77. typeLongVarWChar = adLongVarWChar,
  78. typeBinary = adBinary,
  79. typeVarBinary = adVarBinary,
  80. typeLongVarBinary = adLongVarBinary,
  81. typeChapter = adChapter,
  82. typeFileTime = adFileTime,
  83. typePropVariant = adPropVariant,
  84. typeVarNumeric = adVarNumeric,
  85. typeArray = adVariant
  86. };
  87. BOOL SetFieldValue(int nIndex, int nValue);
  88. BOOL SetFieldValue(LPCTSTR lpFieldName, int nValue);
  89. BOOL SetFieldValue(int nIndex, long lValue);
  90. BOOL SetFieldValue(LPCTSTR lpFieldName, long lValue);
  91. BOOL SetFieldValue(int nIndex, unsigned long lValue);
  92. BOOL SetFieldValue(LPCTSTR lpFieldName, unsigned long lValue);
  93. BOOL SetFieldValue(int nIndex, double dblValue);
  94. BOOL SetFieldValue(LPCTSTR lpFieldName, double dblValue);
  95. BOOL SetFieldValue(int nIndex, CString strValue);
  96. BOOL SetFieldValue(LPCTSTR lpFieldName, CString strValue);
  97. BOOL SetFieldValue(int nIndex, COleDateTime time);
  98. BOOL SetFieldValue(LPCTSTR lpFieldName, COleDateTime time);
  99. BOOL SetFieldValue(int nIndex, bool bValue);
  100. BOOL SetFieldValue(LPCTSTR lpFieldName, bool bValue);
  101. BOOL SetFieldValue(int nIndex, COleCurrency cyValue);
  102. BOOL SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue);
  103. BOOL SetFieldValue(int nIndex, _variant_t vtValue);
  104. BOOL SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue);
  105. BOOL SetFieldEmpty(int nIndex);
  106. BOOL SetFieldEmpty(LPCTSTR lpFieldName);
  107. void CancelUpdate();
  108. BOOL Update();
  109. void Edit();
  110. BOOL AddNew();
  111. BOOL AddNew(CADORecordBinding &pAdoRecordBinding);
  112. BOOL Find(LPCTSTR lpFind, int nSearchDirection = CADORecordset::searchForward);
  113. BOOL FindFirst(LPCTSTR lpFind);
  114. BOOL FindNext();
  115. CADORecordset()
  116. {
  117. m_pRecordset = NULL;
  118. m_pCmd = NULL;
  119. m_strQuery = _T("");
  120. m_strLastError = _T("");
  121. m_dwLastError = 0;
  122. m_pRecBinding = NULL;
  123. m_pRecordset.CreateInstance(__uuidof(Recordset));
  124. m_pCmd.CreateInstance(__uuidof(Command));
  125. m_nEditStatus = CADORecordset::dbEditNone;
  126. m_nSearchDirection = CADORecordset::searchForward;
  127. }
  128. CADORecordset(CADODatabase* pAdoDatabase);
  129. ~CADORecordset()
  130. {
  131. Close();
  132. if(m_pRecordset)
  133. m_pRecordset.Release();
  134. if(m_pCmd)
  135. m_pCmd.Release();
  136. m_pRecordset = NULL;
  137. m_pCmd = NULL;
  138. m_pRecBinding = NULL;
  139. m_strQuery = _T("");
  140. m_strLastError = _T("");
  141. m_dwLastError = 0;
  142. m_nEditStatus = dbEditNone;
  143. }
  144. inline CString GetQuery() {return m_strQuery;};
  145. inline void SetQuery(LPCSTR strQuery) {m_strQuery = strQuery;};
  146. BOOL RecordBinding(CADORecordBinding &pAdoRecordBinding);
  147. DWORD GetRecordCount();
  148. BOOL IsOpen();
  149. void Close();
  150. BOOL Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec = _T(""), int nOption = CADORecordset::openUnknown);
  151. BOOL Open(LPCTSTR lpstrExec = _T(""), int nOption = CADORecordset::openUnknown);
  152. inline long GetFieldCount() {return m_pRecordset->Fields->GetCount();};
  153. BOOL GetFieldValue(LPCTSTR lpFieldName, int& nValue);
  154. BOOL GetFieldValue(int nIndex, int& nValue);
  155. BOOL GetFieldValue(LPCTSTR lpFieldName, long& lValue);
  156. BOOL GetFieldValue(int nIndex, long& lValue);
  157. BOOL GetFieldValue(LPCTSTR lpFieldName, unsigned long& ulValue);
  158. BOOL GetFieldValue(int nIndex, unsigned long& ulValue);
  159. BOOL GetFieldValue(LPCTSTR lpFieldName, double& dbValue);
  160. BOOL GetFieldValue(int nIndex, double& dbValue);
  161. BOOL GetFieldValue(LPCTSTR lpFieldName, CString& strValue, CString strDateFormat = _T(""));
  162. BOOL GetFieldValue(int nIndex, CString& strValue, CString strDateFormat = _T(""));
  163. BOOL GetFieldValue(LPCTSTR lpFieldName, COleDateTime& time);
  164. BOOL GetFieldValue(int nIndex, COleDateTime& time);
  165. BOOL GetFieldValue(int nIndex, bool& bValue);
  166. BOOL GetFieldValue(LPCTSTR lpFieldName, bool& bValue);
  167. BOOL GetFieldValue(int nIndex, COleCurrency& cyValue);
  168. BOOL GetFieldValue(LPCTSTR lpFieldName, COleCurrency& cyValue);
  169. BOOL GetFieldValue(int nIndex, _variant_t& vtValue);
  170. BOOL GetFieldValue(LPCTSTR lpFieldName, _variant_t& vtValue);
  171. BOOL IsFieldNull(LPCTSTR lpFieldName);
  172. BOOL IsFieldNull(int nIndex);
  173. BOOL IsFieldEmpty(LPCTSTR lpFieldName);
  174. BOOL IsFieldEmpty(int nIndex);
  175. inline BOOL IsEof(){return m_pRecordset->EndOfFile == VARIANT_TRUE;};
  176. inline BOOL IsEOF(){return m_pRecordset->EndOfFile == VARIANT_TRUE;};
  177. inline BOOL IsBof(){return m_pRecordset->BOF == VARIANT_TRUE;};
  178. inline BOOL IsBOF(){return m_pRecordset->BOF == VARIANT_TRUE;};
  179. inline void MoveFirst() {m_pRecordset->MoveFirst();};
  180. inline void MoveNext() {m_pRecordset->MoveNext();};
  181. inline void MovePrevious() {m_pRecordset->MovePrevious();};
  182. inline void MoveLast() {m_pRecordset->MoveLast();};
  183. inline long GetAbsolutePage(){return m_pRecordset->GetAbsolutePage();};
  184. inline void SetAbsolutePage(int nPage) {m_pRecordset->PutAbsolutePage((enum PositionEnum)nPage);};
  185. inline long GetPageCount() {return m_pRecordset->GetPageCount();};
  186. inline long GetPageSize() {return m_pRecordset->GetPageSize();};
  187. inline void SetPageSize(int nSize) {m_pRecordset->PutPageSize(nSize);};
  188. inline long GetAbsolutePosition() {return m_pRecordset->GetAbsolutePosition();};
  189. inline void SetAbsolutePosition(int nPosition) {m_pRecordset->PutAbsolutePosition((enum PositionEnum)nPosition);};
  190. inline void Move(ADO_LONGPTR NumRecords){m_pRecordset->Move(NumRecords);}
  191. BOOL GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo* fldInfo);
  192. BOOL GetFieldInfo(int nIndex, CADOFieldInfo* fldInfo);
  193. BOOL AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes);
  194. BOOL AppendChunk(int nIndex, LPVOID lpData, UINT nBytes);
  195. BOOL AppendChunkFromFile(LPCTSTR lpFieldName, CHAR* strFileName);
  196. BOOL AppendChunkFromFile(int nIndex, CHAR* strFileName);
  197. LONG GetChunk(LPCTSTR lpFieldName, CString& strValue);
  198. LONG GetChunk(int nIndex, CString& strValue);
  199. LONG GetChunk(LPCTSTR lpFieldName, LPVOID pData);
  200. LONG GetChunk(int nIndex, LPVOID pData);
  201. LONG GetChunkToFile(LPCTSTR lpFieldName, CHAR* strFileName);
  202. LONG GetChunkToFile(int nIndex, CHAR* strFileName);
  203. CString GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull, long numRows = 0);
  204. inline CString GetLastErrorString() {return m_strLastError;};
  205. inline DWORD GetLastError(){return m_dwLastError;};
  206. inline void GetBookmark() {m_varBookmark = m_pRecordset->Bookmark;};
  207. BOOL SetBookmark();
  208. BOOL Delete();
  209. inline BOOL IsConnectionOpen() {return m_pConnection->GetState() != adStateClosed;};
  210. inline _RecordsetPtr GetRecordset(){return m_pRecordset;};
  211. inline _ConnectionPtr GetActiveConnection() {return m_pConnection;};
  212. BOOL SetFilter(LPCTSTR strFilter);
  213. BOOL SetSort(LPCTSTR lpstrCriteria);
  214. BOOL SaveAsXML(LPCTSTR lpstrXMLFile);
  215. BOOL OpenXML(LPCTSTR lpstrXMLFile);
  216. BOOL Execute(CADOCommand* pCommand);
  217. public:
  218. _RecordsetPtr m_pRecordset;
  219. _CommandPtr m_pCmd;
  220. protected:
  221. _ConnectionPtr m_pConnection;
  222. int m_nSearchDirection;
  223. CString m_strFind;
  224. _variant_t m_varBookFind;
  225. _variant_t m_varBookmark;
  226. int m_nEditStatus;
  227. CString m_strLastError;
  228. DWORD m_dwLastError;
  229. void dump_com_error(_com_error &e);
  230. IADORecordBinding *m_pRecBinding;
  231. CString m_strQuery;
  232. protected:
  233. BOOL PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld);
  234. BOOL PutFieldValue(_variant_t vtIndex, _variant_t vtFld);
  235. BOOL GetFieldInfo(FieldPtr pField, CADOFieldInfo* fldInfo);
  236. LONG GetChunk(FieldPtr pField, CString& strValue);
  237. LONG GetChunk(FieldPtr pField, LPVOID lpData);
  238. BOOL AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes);
  239. private:
  240. CString IntToStr(int nVal);
  241. CString LongToStr(long lVal);
  242. CString ULongToStr(unsigned long ulVal);
  243. CString DblToStr(double dblVal, int ndigits = 20);
  244. CString DblToStr(float fltVal);
  245. };
  246. #endif