ADORecordset.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. #include "StdAfx.h"
  2. #include "ADORecordset.h"
  3. #define ChunkSize 100
  4. CADORecordset::CADORecordset()
  5. {
  6. m_pRecordset = NULL;
  7. m_pCmd = NULL;
  8. m_strQuery = _T("");
  9. m_strLastError = _T("");
  10. m_dwLastError = 0;
  11. m_pRecBinding = NULL;
  12. m_pRecordset.CreateInstance(__uuidof(Recordset));
  13. m_pCmd.CreateInstance(__uuidof(Command));
  14. m_nEditStatus = CADORecordset::dbEditNone;
  15. m_nSearchDirection = CADORecordset::searchForward;
  16. }
  17. CADORecordset::CADORecordset(CADODatabase* pAdoDatabase)
  18. {
  19. m_pRecordset = NULL;
  20. m_pCmd = NULL;
  21. m_strQuery = _T("");
  22. m_strLastError = _T("");
  23. m_dwLastError = 0;
  24. m_pRecBinding = NULL;
  25. m_pRecordset.CreateInstance(__uuidof(Recordset));
  26. m_pCmd.CreateInstance(__uuidof(Command));
  27. m_nEditStatus = CADORecordset::dbEditNone;
  28. m_nSearchDirection = CADORecordset::searchForward;
  29. m_pConnection = pAdoDatabase->GetActiveConnection();
  30. }
  31. BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, const int& nOption, const int& nCursorLocation)
  32. {
  33. Close();
  34. if(_tcscmp(lpstrExec, _T("")) != 0)
  35. m_strQuery = lpstrExec;
  36. ASSERT(!m_strQuery.IsEmpty());
  37. if(m_pConnection == NULL)
  38. m_pConnection = mpdb;
  39. m_strQuery.TrimLeft();
  40. BOOL bIsSelect = m_strQuery.Mid(0, (int)_tcslen(_T("Select "))).CompareNoCase(_T("select ")) == 0 && (nOption == openUnknown);
  41. //adUseClient是需要把数据传输到本地,应用举例是断开数据库进行Recordset操作
  42. //adUseServer是直接在数据库中进行操作,下面这个例子在数据量大的时候,进行Find操作,比adUseClient省了很多时间,
  43. // Jeff.经验证,以上说法不完全对。当使用adUseServer时,只有程序与数据库都在同一电脑上时实施大数据量时才比adUseClient快。
  44. // Jeff.但当程序与数据库分居两地时,情况完全相反。
  45. try
  46. {
  47. m_pRecordset->CursorType = adOpenStatic;
  48. //m_pRecordset->CursorLocation = adUseClient;// //adUseServer使用adUseClient无法使用update更新AppendChunk的内容;
  49. m_pRecordset->CursorLocation = (CursorLocationEnum)nCursorLocation;
  50. if(bIsSelect || nOption == openQuery || nOption == openUnknown)
  51. m_pRecordset->Open((LPCTSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
  52. adOpenStatic, adLockOptimistic, adCmdUnknown);
  53. else if(nOption == openTable)
  54. m_pRecordset->Open((LPCTSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
  55. adOpenKeyset, adLockOptimistic, adCmdTable);
  56. else if(nOption == openStoredProc)
  57. {
  58. m_pCmd->ActiveConnection = mpdb;
  59. m_pCmd->CommandText = _bstr_t(m_strQuery);
  60. m_pCmd->CommandType = adCmdStoredProc;
  61. m_pConnection->CursorLocation = adUseClient;
  62. m_pRecordset = m_pCmd->Execute(NULL, NULL, adCmdText);
  63. }
  64. else if( nOption == openCmdText)
  65. {
  66. m_pRecordset->CursorType = adOpenKeyset;
  67. m_pRecordset->CursorLocation = adUseClient;
  68. m_pRecordset->Open(_variant_t(m_strQuery),_variant_t((IDispatch*)mpdb, TRUE)
  69. ,adOpenKeyset
  70. ,adLockOptimistic
  71. ,adCmdText);
  72. }
  73. else
  74. {
  75. TRACE( "Unknown parameter. %s %d", lpstrExec,nOption);
  76. return FALSE;
  77. }
  78. }
  79. catch(_com_error &e)
  80. {
  81. dump_com_error(e);
  82. return FALSE;
  83. }
  84. return m_pRecordset != NULL && m_pRecordset->GetState()!= adStateClosed;
  85. }
  86. BOOL CADORecordset::Open(LPCTSTR lpstrExec, const int& nOption, const int& nCursorLocation)
  87. {
  88. ASSERT(m_pConnection != NULL);
  89. ASSERT(m_pConnection->GetState() != adStateClosed);
  90. return Open(m_pConnection, lpstrExec, nOption, nCursorLocation);
  91. }
  92. BOOL CADORecordset::OpenSchema(int nSchema, LPCTSTR SchemaID /*= _T("")*/)
  93. {
  94. try
  95. {
  96. _variant_t vtSchemaID = vtMissing;
  97. if(_tcslen(SchemaID) != 0)
  98. {
  99. vtSchemaID = SchemaID;
  100. nSchema = adSchemaProviderSpecific;
  101. }
  102. m_pRecordset = m_pConnection->OpenSchema((enum SchemaEnum)nSchema, vtMissing, vtSchemaID);
  103. return TRUE;
  104. }
  105. catch(_com_error &e)
  106. {
  107. dump_com_error(e);
  108. return FALSE;
  109. }
  110. }
  111. BOOL CADORecordset::Requery()
  112. {
  113. if(IsOpen())
  114. {
  115. try
  116. {
  117. m_pRecordset->Requery(adExecuteRecord);
  118. }
  119. catch(_com_error &e)
  120. {
  121. dump_com_error(e);
  122. return FALSE;
  123. }
  124. }
  125. return TRUE;
  126. }
  127. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, double& dbValue)
  128. {
  129. double val = (double)NULL;
  130. _variant_t vtFld;
  131. try
  132. {
  133. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  134. switch(vtFld.vt)
  135. {
  136. case VT_R4:
  137. val = vtFld.fltVal;
  138. break;
  139. case VT_R8:
  140. val = vtFld.dblVal;
  141. break;
  142. case VT_DECIMAL:
  143. val = vtFld.decVal.Lo32;
  144. val *= (vtFld.decVal.sign == 128) ? -1 : 1;
  145. val /= pow(10.0, vtFld.decVal.scale);
  146. break;
  147. case VT_UI1:
  148. val = vtFld.iVal;
  149. break;
  150. case VT_I2:
  151. case VT_I4:
  152. val = vtFld.lVal;
  153. break;
  154. case VT_INT:
  155. val = vtFld.intVal;
  156. break;
  157. case VT_CY: //Added by John Andy Johnson!!!!
  158. vtFld.ChangeType(VT_R8);
  159. val = vtFld.dblVal;
  160. break;
  161. case VT_NULL:
  162. case VT_EMPTY:
  163. val = 0;
  164. break;
  165. default:
  166. val = vtFld.dblVal;
  167. }
  168. dbValue = val;
  169. return TRUE;
  170. }
  171. catch(_com_error &e)
  172. {
  173. dump_com_error(e);
  174. return FALSE;
  175. }
  176. }
  177. BOOL CADORecordset::GetFieldValue(int nIndex, double& dbValue)
  178. {
  179. double val = (double)NULL;
  180. _variant_t vtFld;
  181. _variant_t vtIndex;
  182. vtIndex.vt = VT_I2;
  183. vtIndex.iVal = nIndex;
  184. try
  185. {
  186. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  187. switch(vtFld.vt)
  188. {
  189. case VT_R4:
  190. val = vtFld.fltVal;
  191. break;
  192. case VT_R8:
  193. val = vtFld.dblVal;
  194. break;
  195. case VT_DECIMAL:
  196. val = vtFld.decVal.Lo32;
  197. val *= (vtFld.decVal.sign == 128)? -1 : 1;
  198. val /= pow(10.0, vtFld.decVal.scale);
  199. break;
  200. case VT_UI1:
  201. val = vtFld.iVal;
  202. break;
  203. case VT_I2:
  204. case VT_I4:
  205. val = vtFld.lVal;
  206. break;
  207. case VT_INT:
  208. val = vtFld.intVal;
  209. break;
  210. case VT_CY: //Added by John Andy Johnson!!!!
  211. vtFld.ChangeType(VT_R8);
  212. val = vtFld.dblVal;
  213. break;
  214. case VT_NULL:
  215. case VT_EMPTY:
  216. val = 0;
  217. break;
  218. default:
  219. val = 0;
  220. }
  221. dbValue = val;
  222. return TRUE;
  223. }
  224. catch(_com_error &e)
  225. {
  226. dump_com_error(e);
  227. return FALSE;
  228. }
  229. }
  230. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, long& lValue)
  231. {
  232. long val = (long)NULL;
  233. _variant_t vtFld;
  234. try
  235. {
  236. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  237. if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
  238. val = vtFld.lVal;
  239. lValue = val;
  240. return TRUE;
  241. }
  242. catch(_com_error &e)
  243. {
  244. dump_com_error(e);
  245. return FALSE;
  246. }
  247. }
  248. BOOL CADORecordset::GetFieldValue(int nIndex, long& lValue)
  249. {
  250. long val = (long)NULL;
  251. _variant_t vtFld;
  252. _variant_t vtIndex;
  253. vtIndex.vt = VT_I2;
  254. vtIndex.iVal = nIndex;
  255. try
  256. {
  257. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  258. if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
  259. val = vtFld.lVal;
  260. lValue = val;
  261. return TRUE;
  262. }
  263. catch(_com_error &e)
  264. {
  265. dump_com_error(e);
  266. return FALSE;
  267. }
  268. }
  269. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, unsigned long& ulValue)
  270. {
  271. long val = (long)NULL;
  272. _variant_t vtFld;
  273. try
  274. {
  275. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  276. if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
  277. val = vtFld.ulVal;
  278. ulValue = val;
  279. return TRUE;
  280. }
  281. catch(_com_error &e)
  282. {
  283. dump_com_error(e);
  284. return FALSE;
  285. }
  286. }
  287. BOOL CADORecordset::GetFieldValue(int nIndex, unsigned long& ulValue)
  288. {
  289. long val = (long)NULL;
  290. _variant_t vtFld;
  291. _variant_t vtIndex;
  292. vtIndex.vt = VT_I2;
  293. vtIndex.iVal = nIndex;
  294. try
  295. {
  296. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  297. if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
  298. val = vtFld.ulVal;
  299. ulValue = val;
  300. return TRUE;
  301. }
  302. catch(_com_error &e)
  303. {
  304. dump_com_error(e);
  305. return FALSE;
  306. }
  307. }
  308. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, int& nValue)
  309. {
  310. int val = NULL;
  311. _variant_t vtFld;
  312. try
  313. {
  314. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  315. switch(vtFld.vt)
  316. {
  317. case VT_BOOL:
  318. val = vtFld.boolVal;
  319. break;
  320. case VT_I2:
  321. case VT_UI1:
  322. val = vtFld.iVal;
  323. break;
  324. case VT_INT:
  325. val = vtFld.intVal;
  326. break;
  327. case VT_NULL:
  328. case VT_EMPTY:
  329. val = 0;
  330. break;
  331. default:
  332. val = vtFld.iVal;
  333. }
  334. nValue = val;
  335. return TRUE;
  336. }
  337. catch(_com_error &e)
  338. {
  339. dump_com_error(e);
  340. return FALSE;
  341. }
  342. }
  343. BOOL CADORecordset::GetFieldValue(int nIndex, int& nValue)
  344. {
  345. int val = (int)NULL;
  346. _variant_t vtFld;
  347. _variant_t vtIndex;
  348. vtIndex.vt = VT_I2;
  349. vtIndex.iVal = nIndex;
  350. try
  351. {
  352. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  353. switch(vtFld.vt)
  354. {
  355. case VT_BOOL:
  356. val = vtFld.boolVal;
  357. break;
  358. case VT_I2:
  359. case VT_UI1:
  360. val = vtFld.iVal;
  361. break;
  362. case VT_INT:
  363. val = vtFld.intVal;
  364. break;
  365. case VT_NULL:
  366. case VT_EMPTY:
  367. val = 0;
  368. break;
  369. default:
  370. val = vtFld.iVal;
  371. }
  372. nValue = val;
  373. return TRUE;
  374. }
  375. catch(_com_error &e)
  376. {
  377. dump_com_error(e);
  378. return FALSE;
  379. }
  380. }
  381. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, CString& strValue, CString strDateFormat)
  382. {
  383. CString str = _T("");
  384. _variant_t vtFld;
  385. try
  386. {
  387. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  388. switch(vtFld.vt)
  389. {
  390. case VT_R4:
  391. str = DblToStr(vtFld.fltVal);
  392. break;
  393. case VT_R8:
  394. str = DblToStr(vtFld.dblVal);
  395. break;
  396. case VT_BSTR:
  397. str = vtFld.bstrVal;
  398. break;
  399. case VT_I2:
  400. case VT_UI1:
  401. str = IntToStr(vtFld.iVal);
  402. break;
  403. case VT_INT:
  404. str = IntToStr(vtFld.intVal);
  405. break;
  406. case VT_I4:
  407. str = LongToStr(vtFld.lVal);
  408. break;
  409. case VT_UI4:
  410. str = ULongToStr(vtFld.ulVal);
  411. break;
  412. case VT_DECIMAL:
  413. {
  414. double val = vtFld.decVal.Lo32;
  415. val *= (vtFld.decVal.sign == 128)? -1 : 1;
  416. val /= pow(10.0, vtFld.decVal.scale);
  417. str = DblToStr(val);
  418. }
  419. break;
  420. case VT_DATE:
  421. {
  422. COleDateTime dt(vtFld);
  423. if(strDateFormat.IsEmpty())
  424. strDateFormat = _T("%Y-%m-%d %H:%M:%S");
  425. str = dt.Format(strDateFormat);
  426. }
  427. break;
  428. case VT_CY: //Added by John Andy Johnson!!!
  429. {
  430. vtFld.ChangeType(VT_R8);
  431. CString str;
  432. str.Format(_T("%f"), vtFld.dblVal);
  433. _TCHAR pszFormattedNumber[64];
  434. // LOCALE_USER_DEFAULT
  435. if(GetCurrencyFormat(
  436. LOCALE_USER_DEFAULT, // locale for which string is to be formatted
  437. 0, // bit flag that controls the function's operation
  438. str, // pointer to input number string
  439. NULL, // pointer to a formatting information structure
  440. // NULL = machine default locale settings
  441. pszFormattedNumber, // pointer to output buffer
  442. 63)) // size of output buffer
  443. {
  444. str = pszFormattedNumber;
  445. }
  446. }
  447. break;
  448. case VT_EMPTY:
  449. case VT_NULL:
  450. str.Empty();
  451. break;
  452. case VT_BOOL:
  453. str = vtFld.boolVal == VARIANT_TRUE? 'T':'F';
  454. break;
  455. default:
  456. str.Empty();
  457. return FALSE;
  458. }
  459. strValue = str;
  460. return TRUE;
  461. }
  462. catch(_com_error &e)
  463. {
  464. dump_com_error(e);
  465. return FALSE;
  466. }
  467. }
  468. BOOL CADORecordset::GetFieldValue(int nIndex, CString& strValue, CString strDateFormat)
  469. {
  470. CString str = _T("");
  471. _variant_t vtFld;
  472. _variant_t vtIndex;
  473. vtIndex.vt = VT_I2;
  474. vtIndex.iVal = nIndex;
  475. try
  476. {
  477. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  478. switch(vtFld.vt)
  479. {
  480. case VT_R4:
  481. str = DblToStr(vtFld.fltVal);
  482. break;
  483. case VT_R8:
  484. str = DblToStr(vtFld.dblVal);
  485. break;
  486. case VT_BSTR:
  487. str = vtFld.bstrVal;
  488. break;
  489. case VT_I2:
  490. case VT_UI1:
  491. str = IntToStr(vtFld.iVal);
  492. break;
  493. case VT_INT:
  494. str = IntToStr(vtFld.intVal);
  495. break;
  496. case VT_I4:
  497. str = LongToStr(vtFld.lVal);
  498. break;
  499. case VT_UI4:
  500. str = ULongToStr(vtFld.ulVal);
  501. break;
  502. case VT_DECIMAL:
  503. {
  504. double val = vtFld.decVal.Lo32;
  505. val *= (vtFld.decVal.sign == 128)? -1 : 1;
  506. val /= pow(10.0, vtFld.decVal.scale);
  507. str = DblToStr(val);
  508. }
  509. break;
  510. case VT_DATE:
  511. {
  512. COleDateTime dt(vtFld);
  513. if(strDateFormat.IsEmpty())
  514. strDateFormat = _T("%Y-%m-%d %H:%M:%S");
  515. str = dt.Format(strDateFormat);
  516. }
  517. break;
  518. case VT_CY: //Added by John Andy Johnson!!!
  519. {
  520. vtFld.ChangeType(VT_R8);
  521. CString str;
  522. str.Format(_T("%f"), vtFld.dblVal);
  523. _TCHAR pszFormattedNumber[64];
  524. // LOCALE_USER_DEFAULT
  525. if(GetCurrencyFormat(
  526. LOCALE_USER_DEFAULT, // locale for which string is to be formatted
  527. 0, // bit flag that controls the function's operation
  528. str, // pointer to input number string
  529. NULL, // pointer to a formatting information structure
  530. // NULL = machine default locale settings
  531. pszFormattedNumber, // pointer to output buffer
  532. 63)) // size of output buffer
  533. {
  534. str = pszFormattedNumber;
  535. }
  536. }
  537. break;
  538. case VT_BOOL:
  539. str = vtFld.boolVal == VARIANT_TRUE? 'T':'F';
  540. break;
  541. case VT_EMPTY:
  542. case VT_NULL:
  543. str.Empty();
  544. break;
  545. default:
  546. str.Empty();
  547. return FALSE;
  548. }
  549. strValue = str;
  550. return TRUE;
  551. }
  552. catch(_com_error &e)
  553. {
  554. dump_com_error(e);
  555. return FALSE;
  556. }
  557. }
  558. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleDateTime& time)
  559. {
  560. _variant_t vtFld;
  561. try
  562. {
  563. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  564. switch(vtFld.vt)
  565. {
  566. case VT_DATE:
  567. {
  568. COleDateTime dt(vtFld);
  569. time = dt;
  570. }
  571. break;
  572. case VT_EMPTY:
  573. case VT_NULL:
  574. time.SetStatus(COleDateTime::null);
  575. break;
  576. default:
  577. return FALSE;
  578. }
  579. return TRUE;
  580. }
  581. catch(_com_error &e)
  582. {
  583. dump_com_error(e);
  584. return FALSE;
  585. }
  586. }
  587. BOOL CADORecordset::GetFieldValue(int nIndex, COleDateTime& time)
  588. {
  589. _variant_t vtFld;
  590. _variant_t vtIndex;
  591. vtIndex.vt = VT_I2;
  592. vtIndex.iVal = nIndex;
  593. try
  594. {
  595. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  596. switch(vtFld.vt)
  597. {
  598. case VT_DATE:
  599. {
  600. COleDateTime dt(vtFld);
  601. time = dt;
  602. }
  603. break;
  604. case VT_EMPTY:
  605. case VT_NULL:
  606. time.SetStatus(COleDateTime::null);
  607. break;
  608. default:
  609. return FALSE;
  610. }
  611. return TRUE;
  612. }
  613. catch(_com_error &e)
  614. {
  615. dump_com_error(e);
  616. return FALSE;
  617. }
  618. }
  619. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, bool& bValue)
  620. {
  621. _variant_t vtFld;
  622. try
  623. {
  624. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  625. switch(vtFld.vt)
  626. {
  627. case VT_BOOL:
  628. bValue = vtFld.boolVal == TRUE? true: false;
  629. break;
  630. case VT_EMPTY:
  631. case VT_NULL:
  632. bValue = false;
  633. break;
  634. default:
  635. return FALSE;
  636. }
  637. return TRUE;
  638. }
  639. catch(_com_error &e)
  640. {
  641. dump_com_error(e);
  642. return FALSE;
  643. }
  644. }
  645. BOOL CADORecordset::GetFieldValue(int nIndex, bool& bValue)
  646. {
  647. _variant_t vtFld;
  648. _variant_t vtIndex;
  649. vtIndex.vt = VT_I2;
  650. vtIndex.iVal = nIndex;
  651. try
  652. {
  653. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  654. switch(vtFld.vt)
  655. {
  656. case VT_BOOL:
  657. bValue = vtFld.boolVal == TRUE? true: false;
  658. break;
  659. case VT_EMPTY:
  660. case VT_NULL:
  661. bValue = false;
  662. break;
  663. default:
  664. return FALSE;
  665. }
  666. return TRUE;
  667. }
  668. catch(_com_error &e)
  669. {
  670. dump_com_error(e);
  671. return FALSE;
  672. }
  673. }
  674. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleCurrency& cyValue)
  675. {
  676. _variant_t vtFld;
  677. try
  678. {
  679. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  680. switch(vtFld.vt)
  681. {
  682. case VT_CY:
  683. cyValue = (CURRENCY)vtFld.cyVal;
  684. break;
  685. case VT_EMPTY:
  686. case VT_NULL:
  687. {
  688. cyValue = COleCurrency();
  689. cyValue.m_status = COleCurrency::null;
  690. }
  691. break;
  692. default:
  693. return FALSE;
  694. }
  695. return TRUE;
  696. }
  697. catch(_com_error &e)
  698. {
  699. dump_com_error(e);
  700. return FALSE;
  701. }
  702. }
  703. BOOL CADORecordset::GetFieldValue(int nIndex, COleCurrency& cyValue)
  704. {
  705. _variant_t vtFld;
  706. _variant_t vtIndex;
  707. vtIndex.vt = VT_I2;
  708. vtIndex.iVal = nIndex;
  709. try
  710. {
  711. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  712. switch(vtFld.vt)
  713. {
  714. case VT_CY:
  715. cyValue = (CURRENCY)vtFld.cyVal;
  716. break;
  717. case VT_EMPTY:
  718. case VT_NULL:
  719. {
  720. cyValue = COleCurrency();
  721. cyValue.m_status = COleCurrency::null;
  722. }
  723. break;
  724. default:
  725. return FALSE;
  726. }
  727. return TRUE;
  728. }
  729. catch(_com_error &e)
  730. {
  731. dump_com_error(e);
  732. return FALSE;
  733. }
  734. }
  735. BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, _variant_t& vtValue)
  736. {
  737. try
  738. {
  739. vtValue = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  740. return TRUE;
  741. }
  742. catch(_com_error &e)
  743. {
  744. dump_com_error(e);
  745. return FALSE;
  746. }
  747. }
  748. BOOL CADORecordset::GetFieldValue(int nIndex, _variant_t& vtValue)
  749. {
  750. _variant_t vtIndex;
  751. vtIndex.vt = VT_I2;
  752. vtIndex.iVal = nIndex;
  753. try
  754. {
  755. vtValue = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  756. return TRUE;
  757. }
  758. catch(_com_error &e)
  759. {
  760. dump_com_error(e);
  761. return FALSE;
  762. }
  763. }
  764. BOOL CADORecordset::IsFieldNull(LPCTSTR lpFieldName)
  765. {
  766. _variant_t vtFld;
  767. try
  768. {
  769. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  770. return vtFld.vt == VT_NULL;
  771. }
  772. catch(_com_error &e)
  773. {
  774. dump_com_error(e);
  775. return FALSE;
  776. }
  777. }
  778. BOOL CADORecordset::IsFieldNull(int nIndex)
  779. {
  780. _variant_t vtFld;
  781. _variant_t vtIndex;
  782. vtIndex.vt = VT_I2;
  783. vtIndex.iVal = nIndex;
  784. try
  785. {
  786. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  787. return vtFld.vt == VT_NULL;
  788. }
  789. catch(_com_error &e)
  790. {
  791. dump_com_error(e);
  792. return FALSE;
  793. }
  794. }
  795. BOOL CADORecordset::IsFieldEmpty(LPCTSTR lpFieldName)
  796. {
  797. _variant_t vtFld;
  798. try
  799. {
  800. vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
  801. return vtFld.vt == VT_EMPTY || vtFld.vt == VT_NULL;
  802. }
  803. catch(_com_error &e)
  804. {
  805. dump_com_error(e);
  806. return FALSE;
  807. }
  808. }
  809. BOOL CADORecordset::IsFieldEmpty(int nIndex)
  810. {
  811. _variant_t vtFld;
  812. _variant_t vtIndex;
  813. vtIndex.vt = VT_I2;
  814. vtIndex.iVal = nIndex;
  815. try
  816. {
  817. vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
  818. return vtFld.vt == VT_EMPTY || vtFld.vt == VT_NULL;
  819. }
  820. catch(_com_error &e)
  821. {
  822. dump_com_error(e);
  823. return FALSE;
  824. }
  825. }
  826. BOOL CADORecordset::SetFieldEmpty(LPCTSTR lpFieldName)
  827. {
  828. _variant_t vtFld;
  829. vtFld.vt = VT_EMPTY;
  830. return PutFieldValue(lpFieldName, vtFld);
  831. }
  832. BOOL CADORecordset::SetFieldEmpty(int nIndex)
  833. {
  834. _variant_t vtFld;
  835. vtFld.vt = VT_EMPTY;
  836. _variant_t vtIndex;
  837. vtIndex.vt = VT_I2;
  838. vtIndex.iVal = nIndex;
  839. return PutFieldValue(vtIndex, vtFld);
  840. }
  841. DWORD CADORecordset::GetRecordCount()
  842. {
  843. DWORD nRows = 0;
  844. nRows = m_pRecordset->GetRecordCount();
  845. if(nRows == -1)
  846. {
  847. nRows = 0;
  848. if(m_pRecordset->EndOfFile != VARIANT_TRUE)
  849. m_pRecordset->MoveFirst();
  850. while(m_pRecordset->EndOfFile != VARIANT_TRUE)
  851. {
  852. nRows++;
  853. m_pRecordset->MoveNext();
  854. }
  855. if(nRows > 0)
  856. m_pRecordset->MoveFirst();
  857. }
  858. return nRows;
  859. }
  860. BOOL CADORecordset::IsOpen()
  861. {
  862. if(m_pRecordset != NULL && IsConnectionOpen())
  863. return m_pRecordset->GetState() != adStateClosed;
  864. return FALSE;
  865. }
  866. void CADORecordset::Close()
  867. {
  868. if(IsOpen())
  869. {
  870. if (m_nEditStatus != dbEditNone)
  871. CancelUpdate();
  872. m_pRecordset->PutSort(_T(""));
  873. m_pRecordset->Close();
  874. }
  875. }
  876. BOOL CADORecordset::RecordBinding(CADORecordBinding &pAdoRecordBinding)
  877. {
  878. HRESULT hr;
  879. m_pRecBinding = NULL;
  880. //Open the binding interface.
  881. if(FAILED(hr = m_pRecordset->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*)&m_pRecBinding )))
  882. {
  883. _com_issue_error(hr);
  884. return FALSE;
  885. }
  886. //Bind the recordset to class
  887. if(FAILED(hr = m_pRecBinding->BindToRecordset(&pAdoRecordBinding)))
  888. {
  889. _com_issue_error(hr);
  890. return FALSE;
  891. }
  892. return TRUE;
  893. }
  894. BOOL CADORecordset::GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo* fldInfo)
  895. {
  896. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  897. return GetFieldInfo(pField, fldInfo);
  898. }
  899. BOOL CADORecordset::GetFieldInfo(int nIndex, CADOFieldInfo* fldInfo)
  900. {
  901. _variant_t vtIndex;
  902. vtIndex.vt = VT_I2;
  903. vtIndex.iVal = nIndex;
  904. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  905. return GetFieldInfo(pField, fldInfo);
  906. }
  907. BOOL CADORecordset::GetFieldInfo(FieldPtr pField, CADOFieldInfo* fldInfo)
  908. {
  909. memset(fldInfo, 0, sizeof(CADOFieldInfo));
  910. _tcscpy_s(fldInfo->m_strName, (LPCTSTR)pField->GetName());
  911. fldInfo->m_lDefinedSize = pField->GetDefinedSize();
  912. fldInfo->m_nType = pField->GetType();
  913. fldInfo->m_lAttributes = pField->GetAttributes();
  914. if(!IsEof())
  915. fldInfo->m_lSize = pField->GetActualSize();
  916. return TRUE;
  917. }
  918. BOOL CADORecordset::GetChunk(LPCTSTR lpFieldName, CString& strValue)
  919. {
  920. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  921. return GetChunk(pField, strValue);
  922. }
  923. BOOL CADORecordset::GetChunk(int nIndex, CString& strValue)
  924. {
  925. _variant_t vtIndex;
  926. vtIndex.vt = VT_I2;
  927. vtIndex.iVal = nIndex;
  928. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  929. return GetChunk(pField, strValue);
  930. }
  931. BOOL CADORecordset::GetChunk(FieldPtr pField, CString& strValue)
  932. {
  933. CString str = _T("");
  934. long lngSize, lngOffSet = 0;
  935. _variant_t varChunk;
  936. lngSize = pField->ActualSize;
  937. str.Empty();
  938. while(lngOffSet < lngSize)
  939. {
  940. try
  941. {
  942. varChunk = pField->GetChunk(ChunkSize);
  943. str += varChunk.bstrVal;
  944. lngOffSet += ChunkSize;
  945. }
  946. catch(_com_error &e)
  947. {
  948. dump_com_error(e);
  949. return FALSE;
  950. }
  951. }
  952. lngOffSet = 0;
  953. strValue = str;
  954. return TRUE;
  955. }
  956. BOOL CADORecordset::GetChunk(LPCTSTR lpFieldName, LPVOID lpData)
  957. {
  958. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  959. return GetChunk(pField, lpData);
  960. }
  961. BOOL CADORecordset::GetChunk(int nIndex, LPVOID lpData)
  962. {
  963. _variant_t vtIndex;
  964. vtIndex.vt = VT_I2;
  965. vtIndex.iVal = nIndex;
  966. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  967. return GetChunk(pField, lpData);
  968. }
  969. BOOL CADORecordset::GetChunk(FieldPtr pField, LPVOID lpData)
  970. {
  971. long lngSize;
  972. _variant_t varChunk;
  973. lngSize = pField->ActualSize;
  974. #if 0
  975. HRESULT hr;
  976. UCHAR chData;
  977. long lngOffSet = 0;
  978. long lBytesCopied = 0;
  979. while(lngOffSet < lngSize)
  980. {
  981. try
  982. {
  983. varChunk = pField->GetChunk(ChunkSize);
  984. //Copy the data only upto the Actual Size of Field.
  985. for(long lIndex = 0; lIndex <= (ChunkSize - 1); lIndex++)
  986. {
  987. hr= SafeArrayGetElement(varChunk.parray, &lIndex, &chData);
  988. if(SUCCEEDED(hr))
  989. {
  990. //Take BYTE by BYTE and advance Memory Location
  991. //hr = SafeArrayPutElement((SAFEARRAY FAR*)lpData, &lBytesCopied ,&chData);
  992. ((UCHAR*)lpData)[lBytesCopied] = chData;
  993. lBytesCopied++;
  994. }
  995. else
  996. {
  997. OutputDebugString(_T("获取安全元素值出错\n"));
  998. break;
  999. }
  1000. }
  1001. lngOffSet += ChunkSize;
  1002. }
  1003. catch(_com_error &e)
  1004. {
  1005. dump_com_error(e);
  1006. return FALSE;
  1007. }
  1008. }
  1009. lngOffSet = 0;
  1010. #else
  1011. try
  1012. {
  1013. varChunk = pField->GetChunk(lngSize);
  1014. if ( varChunk.vt == (VT_ARRAY | VT_UI1))
  1015. {
  1016. BYTE *pBuff = NULL;
  1017. SafeArrayAccessData(varChunk.parray, (void**)&pBuff);
  1018. memcpy(lpData, pBuff, lngSize);
  1019. SafeArrayUnaccessData(varChunk.parray);
  1020. }
  1021. }
  1022. catch(_com_error &e)
  1023. {
  1024. dump_com_error(e);
  1025. return FALSE;
  1026. }
  1027. #endif
  1028. return TRUE;
  1029. }
  1030. LONG CADORecordset::GetChunkEx(LPCTSTR lpFieldName, HGLOBAL *hglobal)
  1031. {
  1032. BYTE* lpData = NULL;
  1033. _variant_t varChunk;
  1034. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  1035. long lngSize = pField->ActualSize;
  1036. *hglobal = GlobalAlloc(GMEM_MOVEABLE, lngSize);
  1037. if ( *hglobal == NULL )
  1038. return 0;
  1039. BYTE* pBuf = NULL;
  1040. lpData = (BYTE*)GlobalLock(*hglobal);
  1041. varChunk = pField->GetChunk(lngSize);
  1042. if ( varChunk.vt == (VT_ARRAY | VT_UI1) )
  1043. {
  1044. SafeArrayAccessData(varChunk.parray, (void**)&pBuf);
  1045. memcpy(lpData, pBuf, lngSize);
  1046. SafeArrayUnaccessData(varChunk.parray);
  1047. }
  1048. return lngSize;
  1049. }
  1050. LONG CADORecordset::GetChunkEx(int nIndex, HGLOBAL *hglobal)
  1051. {
  1052. _variant_t vtIndex;
  1053. vtIndex.vt = VT_I2;
  1054. vtIndex.iVal = nIndex;
  1055. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  1056. BYTE* lpData = NULL;
  1057. _variant_t varChunk;
  1058. long lngSize = pField->ActualSize;
  1059. *hglobal = GlobalAlloc(GMEM_MOVEABLE, lngSize);
  1060. if ( *hglobal == NULL )
  1061. return 0;
  1062. BYTE* pBuf = NULL;
  1063. lpData = (BYTE*)GlobalLock(*hglobal);
  1064. varChunk = pField->GetChunk(lngSize);
  1065. if ( varChunk.vt == (VT_ARRAY | VT_UI1) )
  1066. {
  1067. SafeArrayAccessData(varChunk.parray, (void**)&pBuf);
  1068. memcpy(lpData, pBuf, lngSize);
  1069. SafeArrayUnaccessData(varChunk.parray);
  1070. }
  1071. return lngSize;
  1072. }
  1073. LONG CADORecordset::GetChunkToFile(LPCTSTR lpFieldName, TCHAR* strFileName)
  1074. {
  1075. CFile getFile;
  1076. CFileException e;
  1077. BYTE *lpData = NULL;
  1078. LONG lSize = 0;
  1079. INT nRetCode = 0;
  1080. DeleteFile(strFileName);
  1081. if ( getFile.Open(strFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &e) ) // 打开了一个文件;
  1082. {
  1083. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  1084. lSize = pField->ActualSize;
  1085. if (lSize > 0)
  1086. {
  1087. if (lpData = new BYTE[lSize + 1]) // 重新申请必要的存储空间;
  1088. {
  1089. nRetCode = GetChunk(pField, (LPVOID)lpData);
  1090. if (nRetCode > 0)
  1091. {
  1092. getFile.Write(lpData, lSize);
  1093. }
  1094. if (lpData)
  1095. delete []lpData;
  1096. lpData = NULL;
  1097. }
  1098. }
  1099. getFile.Close();
  1100. }
  1101. return nRetCode;
  1102. }
  1103. LONG CADORecordset::GetChunkToFile(int nIndex, TCHAR* strFileName)
  1104. {
  1105. CFile getFile;
  1106. CFileException e;
  1107. BYTE *lpData = NULL;
  1108. LONG lSize = 0;
  1109. INT iRetCode = 0;
  1110. if (getFile.Open(strFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &e))//打开了一个文件;
  1111. {
  1112. _variant_t vtIndex;
  1113. vtIndex.vt = VT_I2;
  1114. vtIndex.iVal = nIndex;
  1115. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  1116. lSize = pField->ActualSize;
  1117. if (lSize > 0)
  1118. {
  1119. if (lpData = new BYTE[lSize + 1]) //重新申请必要的存储空间;
  1120. {
  1121. iRetCode = GetChunk(pField, (LPVOID)lpData);
  1122. if (iRetCode > 0)
  1123. {
  1124. getFile.Write(lpData, iRetCode);
  1125. }
  1126. delete [] lpData;
  1127. lpData = 0;
  1128. }
  1129. }
  1130. getFile.Close();
  1131. }
  1132. return iRetCode;
  1133. }
  1134. BOOL CADORecordset::AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes)
  1135. {
  1136. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  1137. return AppendChunk(pField, lpData, nBytes);
  1138. }
  1139. BOOL CADORecordset::AppendChunk(int nIndex, LPVOID lpData, UINT nBytes)
  1140. {
  1141. _variant_t vtIndex;
  1142. vtIndex.vt = VT_I2;
  1143. vtIndex.iVal = nIndex;
  1144. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  1145. return AppendChunk(pField, lpData, nBytes);
  1146. }
  1147. BOOL CADORecordset::AppendChunkFromFile(LPCTSTR lpFieldName, TCHAR* strFileName)
  1148. {
  1149. CFile appendFile;
  1150. CFileException e;
  1151. LONG lSize = 0;
  1152. BYTE *lpData = NULL;
  1153. BOOL bRetCode = FALSE;
  1154. if (appendFile.Open(strFileName, CFile::modeRead|CFile::typeBinary, &e)) //打开了一个文件;
  1155. {
  1156. lSize = (LONG)appendFile.GetLength(); //先得到文件长度;
  1157. lpData = new BYTE[lSize]; //按文件的大小在堆上申请一块内存;
  1158. if (appendFile.Read(lpData, lSize) > 0) //把文件读到lpData(堆上申请一块内存);
  1159. {
  1160. FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
  1161. bRetCode = AppendChunk(pField, (LPVOID)lpData, lSize);
  1162. }
  1163. delete [] lpData;
  1164. lpData = NULL;
  1165. appendFile.Close();
  1166. }
  1167. return bRetCode;
  1168. }
  1169. BOOL CADORecordset::AppendChunkFromFile(int nIndex, TCHAR* strFileName)
  1170. {
  1171. CFile appendFile;
  1172. CFileException e;
  1173. LONG lSize = 0;
  1174. BYTE *lpData = NULL;
  1175. BOOL bRetCode = FALSE;
  1176. if (appendFile.Open(strFileName, CFile::modeRead|CFile::typeBinary, &e)) //打开了一个文件;
  1177. {
  1178. lSize = (LONG)appendFile.GetLength(); //先得到文件长度;
  1179. lpData = new BYTE[lSize]; //按文件的大小在堆上申请一块内存;
  1180. _variant_t vtIndex;
  1181. vtIndex.vt = VT_I2;
  1182. vtIndex.iVal = nIndex;
  1183. FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
  1184. if (appendFile.Read(lpData, lSize) > 0) //把文件读到lpData(堆上申请一块内存);
  1185. {
  1186. bRetCode = AppendChunk(pField, (LPVOID)lpData, lSize);
  1187. }
  1188. delete [] lpData;
  1189. lpData = NULL;
  1190. appendFile.Close();
  1191. }
  1192. return bRetCode;
  1193. }
  1194. BOOL CADORecordset::AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
  1195. {
  1196. HRESULT hr;
  1197. _variant_t varChunk;
  1198. long lngOffset = 0;
  1199. UCHAR chData;
  1200. SAFEARRAY FAR *psa = NULL;
  1201. SAFEARRAYBOUND rgsabound[1];
  1202. try
  1203. {
  1204. //Create a safe array to store the array of BYTES
  1205. rgsabound[0].lLbound = 0;
  1206. rgsabound[0].cElements = nBytes;
  1207. psa = SafeArrayCreate(VT_UI1,1,rgsabound);
  1208. while(lngOffset < (long)nBytes)
  1209. {
  1210. chData = ((UCHAR*)lpData)[lngOffset];
  1211. hr = SafeArrayPutElement(psa, &lngOffset, &chData);
  1212. if(FAILED(hr))
  1213. return FALSE;
  1214. lngOffset++;
  1215. }
  1216. lngOffset = 0;
  1217. //Assign the Safe array to a variant.
  1218. varChunk.vt = VT_ARRAY|VT_UI1;
  1219. varChunk.parray = psa;
  1220. hr = pField->AppendChunk(varChunk);
  1221. if(SUCCEEDED(hr))
  1222. return TRUE;
  1223. }
  1224. catch(_com_error &e)
  1225. {
  1226. dump_com_error(e);
  1227. return FALSE;
  1228. }
  1229. return FALSE;
  1230. }
  1231. CString CADORecordset::GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull, long numRows)
  1232. {
  1233. _bstr_t varOutput;
  1234. _bstr_t varNull(_T(""));
  1235. _bstr_t varCols(_T("\t"));
  1236. _bstr_t varRows(_T("\r"));
  1237. if(_tcslen(lpCols) != 0)
  1238. varCols = _bstr_t(lpCols);
  1239. if(_tcslen(lpRows) != 0)
  1240. varRows = _bstr_t(lpRows);
  1241. if(numRows == 0)
  1242. numRows =(long)GetRecordCount();
  1243. varOutput = m_pRecordset->GetString(adClipString, numRows, varCols, varRows, varNull);
  1244. return (LPCTSTR)varOutput;
  1245. }
  1246. void CADORecordset::Edit()
  1247. {
  1248. m_nEditStatus = dbEdit;
  1249. }
  1250. BOOL CADORecordset::AddNew()
  1251. {
  1252. m_nEditStatus = dbEditNone;
  1253. if(m_pRecordset->AddNew() != S_OK)
  1254. return FALSE;
  1255. m_nEditStatus = dbEditNew;
  1256. return TRUE;
  1257. }
  1258. BOOL CADORecordset::AddNew(CADORecordBinding &pAdoRecordBinding)
  1259. {
  1260. try
  1261. {
  1262. if(m_pRecBinding->AddNew(&pAdoRecordBinding) != S_OK)
  1263. {
  1264. return FALSE;
  1265. }
  1266. else
  1267. {
  1268. m_pRecBinding->Update(&pAdoRecordBinding);
  1269. return TRUE;
  1270. }
  1271. }
  1272. catch(_com_error &e)
  1273. {
  1274. dump_com_error(e);
  1275. return FALSE;
  1276. }
  1277. }
  1278. BOOL CADORecordset::Update()
  1279. {
  1280. BOOL bret = TRUE;
  1281. if(m_nEditStatus != dbEditNone)
  1282. {
  1283. try
  1284. {
  1285. if(m_pRecordset->Update() != S_OK)
  1286. bret = FALSE;
  1287. }
  1288. catch(_com_error &e)
  1289. {
  1290. dump_com_error(e);
  1291. bret = FALSE;
  1292. }
  1293. if(!bret)
  1294. m_pRecordset->CancelUpdate();
  1295. m_nEditStatus = dbEditNone;
  1296. }
  1297. return bret;
  1298. }
  1299. void CADORecordset::CancelUpdate()
  1300. {
  1301. m_pRecordset->CancelUpdate();
  1302. m_nEditStatus = dbEditNone;
  1303. }
  1304. BOOL CADORecordset::SetFieldValue(int nIndex, CString strValue)
  1305. {
  1306. _variant_t vtFld;
  1307. _variant_t vtIndex;
  1308. vtIndex.vt = VT_I2;
  1309. vtIndex.iVal = nIndex;
  1310. if(!strValue.IsEmpty())
  1311. vtFld.vt = VT_BSTR;
  1312. else
  1313. vtFld.vt = VT_NULL;
  1314. vtFld.bstrVal = strValue.AllocSysString();
  1315. BOOL bret = PutFieldValue(vtIndex, vtFld);
  1316. SysFreeString(vtFld.bstrVal);
  1317. return bret;
  1318. }
  1319. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, CString strValue)
  1320. {
  1321. _variant_t vtFld;
  1322. if(!strValue.IsEmpty())
  1323. vtFld.vt = VT_BSTR;
  1324. else
  1325. vtFld.vt = VT_NULL;
  1326. vtFld.bstrVal = strValue.AllocSysString();
  1327. BOOL bret = PutFieldValue(lpFieldName, vtFld);
  1328. SysFreeString(vtFld.bstrVal);
  1329. return bret;
  1330. }
  1331. BOOL CADORecordset::SetFieldValue(int nIndex, int nValue)
  1332. {
  1333. _variant_t vtFld;
  1334. vtFld.vt = VT_I2;
  1335. vtFld.iVal = nValue;
  1336. _variant_t vtIndex;
  1337. vtIndex.vt = VT_I2;
  1338. vtIndex.iVal = nIndex;
  1339. return PutFieldValue(vtIndex, vtFld);
  1340. }
  1341. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, int nValue)
  1342. {
  1343. _variant_t vtFld;
  1344. vtFld.vt = VT_I2;
  1345. vtFld.iVal = nValue;
  1346. return PutFieldValue(lpFieldName, vtFld);
  1347. }
  1348. BOOL CADORecordset::SetFieldValue(int nIndex, long lValue)
  1349. {
  1350. _variant_t vtFld;
  1351. vtFld.vt = VT_I4;
  1352. vtFld.lVal = lValue;
  1353. _variant_t vtIndex;
  1354. vtIndex.vt = VT_I2;
  1355. vtIndex.iVal = nIndex;
  1356. return PutFieldValue(vtIndex, vtFld);
  1357. }
  1358. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, long lValue)
  1359. {
  1360. _variant_t vtFld;
  1361. vtFld.vt = VT_I4;
  1362. vtFld.lVal = lValue;
  1363. return PutFieldValue(lpFieldName, vtFld);
  1364. }
  1365. BOOL CADORecordset::SetFieldValue(int nIndex, unsigned long ulValue)
  1366. {
  1367. _variant_t vtFld;
  1368. vtFld.vt = VT_UI4;
  1369. vtFld.ulVal = ulValue;
  1370. _variant_t vtIndex;
  1371. vtIndex.vt = VT_I2;
  1372. vtIndex.iVal = nIndex;
  1373. return PutFieldValue(vtIndex, vtFld);
  1374. }
  1375. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, unsigned long ulValue)
  1376. {
  1377. _variant_t vtFld;
  1378. vtFld.vt = VT_UI4;
  1379. vtFld.ulVal = ulValue;
  1380. return PutFieldValue(lpFieldName, vtFld);
  1381. }
  1382. BOOL CADORecordset::SetFieldValue(int nIndex, double dblValue)
  1383. {
  1384. _variant_t vtFld;
  1385. vtFld.vt = VT_R8;
  1386. vtFld.dblVal = dblValue;
  1387. _variant_t vtIndex;
  1388. vtIndex.vt = VT_I2;
  1389. vtIndex.iVal = nIndex;
  1390. return PutFieldValue(vtIndex, vtFld);
  1391. }
  1392. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, double dblValue)
  1393. {
  1394. _variant_t vtFld;
  1395. vtFld.vt = VT_R8;
  1396. vtFld.dblVal = dblValue;
  1397. return PutFieldValue(lpFieldName, vtFld);
  1398. }
  1399. BOOL CADORecordset::SetFieldValue(int nIndex, COleDateTime time)
  1400. {
  1401. _variant_t vtFld;
  1402. vtFld.vt = VT_DATE;
  1403. vtFld.date = time;
  1404. _variant_t vtIndex;
  1405. vtIndex.vt = VT_I2;
  1406. vtIndex.iVal = nIndex;
  1407. return PutFieldValue(vtIndex, vtFld);
  1408. }
  1409. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleDateTime time)
  1410. {
  1411. _variant_t vtFld;
  1412. vtFld.vt = VT_DATE;
  1413. vtFld.date = time;
  1414. return PutFieldValue(lpFieldName, vtFld);
  1415. }
  1416. BOOL CADORecordset::SetFieldValue(int nIndex, bool bValue)
  1417. {
  1418. _variant_t vtFld;
  1419. vtFld.vt = VT_BOOL;
  1420. vtFld.boolVal = bValue;
  1421. _variant_t vtIndex;
  1422. vtIndex.vt = VT_I2;
  1423. vtIndex.iVal = nIndex;
  1424. return PutFieldValue(vtIndex, vtFld);
  1425. }
  1426. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, bool bValue)
  1427. {
  1428. _variant_t vtFld;
  1429. vtFld.vt = VT_BOOL;
  1430. vtFld.boolVal = bValue;
  1431. return PutFieldValue(lpFieldName, vtFld);
  1432. }
  1433. BOOL CADORecordset::SetFieldValue(int nIndex, COleCurrency cyValue)
  1434. {
  1435. if(cyValue.m_status == COleCurrency::invalid)
  1436. return FALSE;
  1437. _variant_t vtFld;
  1438. vtFld.vt = VT_CY;
  1439. vtFld.cyVal = cyValue.m_cur;
  1440. _variant_t vtIndex;
  1441. vtIndex.vt = VT_I2;
  1442. vtIndex.iVal = nIndex;
  1443. return PutFieldValue(vtIndex, vtFld);
  1444. }
  1445. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue)
  1446. {
  1447. if(cyValue.m_status == COleCurrency::invalid)
  1448. return FALSE;
  1449. _variant_t vtFld;
  1450. vtFld.vt = VT_CY;
  1451. vtFld.cyVal = cyValue.m_cur;
  1452. return PutFieldValue(lpFieldName, vtFld);
  1453. }
  1454. BOOL CADORecordset::SetFieldValue(int nIndex, _variant_t vtValue)
  1455. {
  1456. _variant_t vtIndex;
  1457. vtIndex.vt = VT_I2;
  1458. vtIndex.iVal = nIndex;
  1459. return PutFieldValue(vtIndex, vtValue);
  1460. }
  1461. BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue)
  1462. {
  1463. return PutFieldValue(lpFieldName, vtValue);
  1464. }
  1465. BOOL CADORecordset::SetBookmark()
  1466. {
  1467. if(m_varBookmark.vt != VT_EMPTY)
  1468. {
  1469. m_pRecordset->Bookmark = m_varBookmark;
  1470. return TRUE;
  1471. }
  1472. return FALSE;
  1473. }
  1474. BOOL CADORecordset::Delete()
  1475. {
  1476. if(m_pRecordset->Delete(adAffectCurrent) != S_OK)
  1477. return FALSE;
  1478. if(m_pRecordset->Update() != S_OK)
  1479. return FALSE;
  1480. m_nEditStatus = dbEditNone;
  1481. return TRUE;
  1482. }
  1483. BOOL CADORecordset::Find(LPCTSTR lpFind, int nSearchDirection)
  1484. {
  1485. m_strFind = lpFind;
  1486. m_nSearchDirection = nSearchDirection;
  1487. ASSERT(!m_strFind.IsEmpty());
  1488. if(m_nSearchDirection == searchForward)
  1489. {
  1490. m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchForward, "");
  1491. if(!IsEof())
  1492. {
  1493. m_varBookFind = m_pRecordset->Bookmark;
  1494. return TRUE;
  1495. }
  1496. }
  1497. else if(m_nSearchDirection == searchBackward)
  1498. {
  1499. m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchBackward, "");
  1500. if(!IsBof())
  1501. {
  1502. m_varBookFind = m_pRecordset->Bookmark;
  1503. return TRUE;
  1504. }
  1505. }
  1506. else
  1507. {
  1508. TRACE("Unknown parameter. %d", nSearchDirection);
  1509. m_nSearchDirection = searchForward;
  1510. }
  1511. return FALSE;
  1512. }
  1513. BOOL CADORecordset::FindFirst(LPCTSTR lpFind)
  1514. {
  1515. m_pRecordset->MoveFirst();
  1516. return Find(lpFind);
  1517. }
  1518. BOOL CADORecordset::FindNext()
  1519. {
  1520. if(m_nSearchDirection == searchForward)
  1521. {
  1522. m_pRecordset->Find(_bstr_t(m_strFind), 1, adSearchForward, m_varBookFind);
  1523. if(!IsEof())
  1524. {
  1525. m_varBookFind = m_pRecordset->Bookmark;
  1526. return TRUE;
  1527. }
  1528. }
  1529. else
  1530. {
  1531. m_pRecordset->Find(_bstr_t(m_strFind), 1, adSearchBackward, m_varBookFind);
  1532. if(!IsBof())
  1533. {
  1534. m_varBookFind = m_pRecordset->Bookmark;
  1535. return TRUE;
  1536. }
  1537. }
  1538. return FALSE;
  1539. }
  1540. BOOL CADORecordset::PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld)
  1541. {
  1542. if(m_nEditStatus == dbEditNone)
  1543. return FALSE;
  1544. try
  1545. {
  1546. m_pRecordset->Fields->GetItem(lpFieldName)->Value = vtFld;
  1547. return TRUE;
  1548. }
  1549. catch(_com_error &e)
  1550. {
  1551. dump_com_error(e);
  1552. return FALSE;
  1553. }
  1554. }
  1555. BOOL CADORecordset::PutFieldValue(_variant_t vtIndex, _variant_t vtFld)
  1556. {
  1557. if(m_nEditStatus == dbEditNone)
  1558. return FALSE;
  1559. try
  1560. {
  1561. m_pRecordset->Fields->GetItem(vtIndex)->Value = vtFld;
  1562. return TRUE;
  1563. }
  1564. catch(_com_error &e)
  1565. {
  1566. dump_com_error(e);
  1567. return FALSE;
  1568. }
  1569. }
  1570. BOOL CADORecordset::Clone(CADORecordset &pRs)
  1571. {
  1572. try
  1573. {
  1574. pRs.m_pRecordset = m_pRecordset->Clone(adLockUnspecified);
  1575. return TRUE;
  1576. }
  1577. catch(_com_error &e)
  1578. {
  1579. dump_com_error(e);
  1580. return FALSE;
  1581. }
  1582. }
  1583. BOOL CADORecordset::SetFilter(LPCTSTR strFilter)
  1584. {
  1585. ASSERT(IsOpen());
  1586. try
  1587. {
  1588. m_pRecordset->PutFilter(strFilter);
  1589. return TRUE;
  1590. }
  1591. catch(_com_error &e)
  1592. {
  1593. dump_com_error(e);
  1594. return FALSE;
  1595. }
  1596. }
  1597. BOOL CADORecordset::SetSort(LPCTSTR strCriteria)
  1598. {
  1599. ASSERT(IsOpen());
  1600. try
  1601. {
  1602. m_pRecordset->PutSort(strCriteria);
  1603. return TRUE;
  1604. }
  1605. catch(_com_error &e)
  1606. {
  1607. dump_com_error(e);
  1608. return FALSE;
  1609. }
  1610. }
  1611. BOOL CADORecordset::SaveAsXML(LPCTSTR lpstrXMLFile)
  1612. {
  1613. HRESULT hr;
  1614. ASSERT(IsOpen());
  1615. try
  1616. {
  1617. hr = m_pRecordset->Save(lpstrXMLFile, adPersistXML);
  1618. return hr == S_OK;
  1619. }
  1620. catch(_com_error &e)
  1621. {
  1622. dump_com_error(e);
  1623. return FALSE;
  1624. }
  1625. return TRUE;
  1626. }
  1627. BOOL CADORecordset::OpenXML(LPCTSTR lpstrXMLFile)
  1628. {
  1629. HRESULT hr = S_OK;
  1630. if(IsOpen())
  1631. Close();
  1632. try
  1633. {
  1634. hr = m_pRecordset->Open(lpstrXMLFile, "Provider=MSPersist;", adOpenForwardOnly, adLockOptimistic, adCmdFile);
  1635. return hr == S_OK;
  1636. }
  1637. catch(_com_error &e)
  1638. {
  1639. dump_com_error(e);
  1640. return FALSE;
  1641. }
  1642. }
  1643. BOOL CADORecordset::Execute(CADOCommand* pAdoCommand)
  1644. {
  1645. if(IsOpen())
  1646. Close();
  1647. ASSERT(!pAdoCommand->GetText().IsEmpty());
  1648. try
  1649. {
  1650. m_pConnection->CursorLocation = adUseClient;
  1651. m_pRecordset = pAdoCommand->GetCommand()->Execute(NULL, NULL, pAdoCommand->GetType());
  1652. return TRUE;
  1653. }
  1654. catch(_com_error &e)
  1655. {
  1656. dump_com_error(e);
  1657. return FALSE;
  1658. }
  1659. }
  1660. void CADORecordset::dump_com_error(_com_error &e)
  1661. {
  1662. CString ErrorStr;
  1663. _bstr_t bstrSource(e.Source());
  1664. _bstr_t bstrDescription(e.Description());
  1665. ErrorStr.Format(_T("CADORecordset Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"),
  1666. e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription );
  1667. m_strLastError = _T("Query = " + GetQuery() + '\n' + ErrorStr);
  1668. m_dwLastError = e.Error();
  1669. #ifdef _DEBUG
  1670. AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
  1671. #endif
  1672. }