ADORecordset.cpp 32 KB

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