ADOParameter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "StdAfx.h"
  2. #include "ADOParameter.h"
  3. CADOParameter::CADOParameter(int nType, long lSize, int nDirection, CString strName)
  4. {
  5. m_pParameter = NULL;
  6. m_pParameter.CreateInstance(__uuidof(Parameter));
  7. m_strName = _T("");
  8. m_pParameter->Direction = (ParameterDirectionEnum)nDirection;
  9. m_strName = strName;
  10. m_pParameter->Name = m_strName.AllocSysString();
  11. m_pParameter->Type = (DataTypeEnum)nType;
  12. m_pParameter->Size = lSize;
  13. }
  14. BOOL CADOParameter::SetValue(int nValue)
  15. {
  16. _variant_t vtVal;
  17. ASSERT(m_pParameter != NULL);
  18. vtVal.vt = VT_I2;
  19. vtVal.iVal = nValue;
  20. try
  21. {
  22. if(m_pParameter->Size == 0)
  23. m_pParameter->Size = sizeof(int);
  24. m_pParameter->Value = vtVal;
  25. return TRUE;
  26. }
  27. catch(_com_error &e)
  28. {
  29. dump_com_error(e);
  30. return FALSE;
  31. }
  32. }
  33. BOOL CADOParameter::SetValue(long lValue)
  34. {
  35. _variant_t vtVal;
  36. ASSERT(m_pParameter != NULL);
  37. vtVal.vt = VT_I4;
  38. vtVal.lVal = lValue;
  39. try
  40. {
  41. if(m_pParameter->Size == 0)
  42. m_pParameter->Size = sizeof(long);
  43. m_pParameter->Value = vtVal;
  44. return TRUE;
  45. }
  46. catch(_com_error &e)
  47. {
  48. dump_com_error(e);
  49. return FALSE;
  50. }
  51. }
  52. BOOL CADOParameter::SetValue(double dblValue)
  53. {
  54. _variant_t vtVal;
  55. ASSERT(m_pParameter != NULL);
  56. vtVal.vt = VT_R8;
  57. vtVal.dblVal = dblValue;
  58. try
  59. {
  60. if(m_pParameter->Size == 0)
  61. m_pParameter->Size = sizeof(double);
  62. m_pParameter->Value = vtVal;
  63. return TRUE;
  64. }
  65. catch(_com_error &e)
  66. {
  67. dump_com_error(e);
  68. return FALSE;
  69. }
  70. }
  71. BOOL CADOParameter::SetValue(CString strValue)
  72. {
  73. _variant_t vtVal;
  74. ASSERT(m_pParameter != NULL);
  75. if(!strValue.IsEmpty())
  76. vtVal.vt = VT_BSTR;
  77. else
  78. vtVal.vt = VT_NULL;
  79. //Corrected by Giles Forster 10/03/2001
  80. vtVal.bstrVal = strValue.AllocSysString();
  81. try
  82. {
  83. if(m_pParameter->Size == 0)
  84. m_pParameter->Size = sizeof(char) * strValue.GetLength();
  85. m_pParameter->Value = vtVal;
  86. return TRUE;
  87. }
  88. catch(_com_error &e)
  89. {
  90. dump_com_error(e);
  91. return FALSE;
  92. }
  93. }
  94. BOOL CADOParameter::SetValue(COleDateTime time)
  95. {
  96. _variant_t vtVal;
  97. ASSERT(m_pParameter != NULL);
  98. vtVal.vt = VT_DATE;
  99. vtVal.date = time;
  100. try
  101. {
  102. if(m_pParameter->Size == 0)
  103. m_pParameter->Size = sizeof(DATE);
  104. m_pParameter->Value = vtVal;
  105. return TRUE;
  106. }
  107. catch(_com_error &e)
  108. {
  109. dump_com_error(e);
  110. return FALSE;
  111. }
  112. }
  113. BOOL CADOParameter::SetValue(_variant_t vtValue)
  114. {
  115. ASSERT(m_pParameter != NULL);
  116. try
  117. {
  118. if(m_pParameter->Size == 0)
  119. m_pParameter->Size = sizeof(VARIANT);
  120. m_pParameter->Value = vtValue;
  121. return TRUE;
  122. }
  123. catch(_com_error &e)
  124. {
  125. dump_com_error(e);
  126. return FALSE;
  127. }
  128. }
  129. BOOL CADOParameter::GetValue(int& nValue)
  130. {
  131. _variant_t vtVal;
  132. int nVal = 0;
  133. try
  134. {
  135. vtVal = m_pParameter->Value;
  136. switch(vtVal.vt)
  137. {
  138. case VT_BOOL:
  139. nVal = vtVal.boolVal;
  140. break;
  141. case VT_I2:
  142. case VT_UI1:
  143. nVal = vtVal.iVal;
  144. break;
  145. case VT_NULL:
  146. case VT_EMPTY:
  147. nVal = 0;
  148. break;
  149. default:
  150. nVal = vtVal.iVal;
  151. }
  152. nValue = nVal;
  153. return TRUE;
  154. }
  155. catch(_com_error& e)
  156. {
  157. dump_com_error(e);
  158. return FALSE;
  159. }
  160. }
  161. BOOL CADOParameter::GetValue(long& lValue)
  162. {
  163. _variant_t vtVal;
  164. long lVal = 0;
  165. try
  166. {
  167. vtVal = m_pParameter->Value;
  168. if(vtVal.vt != VT_NULL && vtVal.vt != VT_EMPTY)
  169. lVal = vtVal.lVal;
  170. lValue = lVal;
  171. return TRUE;
  172. }
  173. catch(_com_error& e)
  174. {
  175. dump_com_error(e);
  176. return FALSE;
  177. }
  178. }
  179. BOOL CADOParameter::GetValue(double& dbValue)
  180. {
  181. _variant_t vtVal;
  182. double dblVal;
  183. try
  184. {
  185. vtVal = m_pParameter->Value;
  186. switch(vtVal.vt)
  187. {
  188. case VT_R4:
  189. dblVal = vtVal.fltVal;
  190. break;
  191. case VT_R8:
  192. dblVal = vtVal.dblVal;
  193. break;
  194. case VT_DECIMAL:
  195. //Corrected by Jos?Carlos Martínez Galán
  196. dblVal = vtVal.decVal.Lo32;
  197. dblVal *= (vtVal.decVal.sign == 128)? -1 : 1;
  198. dblVal /= pow(10.0, vtVal.decVal.scale);
  199. break;
  200. case VT_UI1:
  201. dblVal = vtVal.iVal;
  202. break;
  203. case VT_I2:
  204. case VT_I4:
  205. dblVal = vtVal.lVal;
  206. break;
  207. case VT_NULL:
  208. case VT_EMPTY:
  209. dblVal = 0;
  210. break;
  211. default:
  212. dblVal = 0;
  213. }
  214. dbValue = dblVal;
  215. return TRUE;
  216. }
  217. catch(_com_error& e)
  218. {
  219. dump_com_error(e);
  220. return FALSE;
  221. }
  222. }
  223. BOOL CADOParameter::GetValue(CString& strValue, CString strDateFormat)
  224. {
  225. _variant_t vtVal;
  226. CString strVal = _T("");
  227. try
  228. {
  229. vtVal = m_pParameter->Value;
  230. switch(vtVal.vt)
  231. {
  232. case VT_R4:
  233. strVal = DblToStr(vtVal.fltVal);
  234. break;
  235. case VT_R8:
  236. strVal = DblToStr(vtVal.dblVal);
  237. break;
  238. case VT_BSTR:
  239. strVal = vtVal.bstrVal;
  240. break;
  241. case VT_I2:
  242. case VT_UI1:
  243. strVal = IntToStr(vtVal.iVal);
  244. break;
  245. case VT_I4:
  246. strVal = LongToStr(vtVal.lVal);
  247. break;
  248. case VT_DECIMAL:
  249. {
  250. //Corrected by Jos?Carlos Martínez Galán
  251. double val = vtVal.decVal.Lo32;
  252. val *= (vtVal.decVal.sign == 128)? -1 : 1;
  253. val /= pow(10.0, vtVal.decVal.scale);
  254. strVal = DblToStr(val);
  255. }
  256. break;
  257. case VT_DATE:
  258. {
  259. COleDateTime dt(vtVal);
  260. if(strDateFormat.IsEmpty())
  261. strDateFormat = _T("%Y-%m-%d %H:%M:%S");
  262. strVal = dt.Format(strDateFormat);
  263. }
  264. break;
  265. case VT_EMPTY:
  266. case VT_NULL:
  267. strVal.Empty();
  268. break;
  269. default:
  270. strVal.Empty();
  271. return FALSE;
  272. }
  273. strValue = strVal;
  274. return TRUE;
  275. }
  276. catch(_com_error& e)
  277. {
  278. dump_com_error(e);
  279. return FALSE;
  280. }
  281. }
  282. BOOL CADOParameter::GetValue(COleDateTime& time)
  283. {
  284. _variant_t vtVal;
  285. try
  286. {
  287. vtVal = m_pParameter->Value;
  288. switch(vtVal.vt)
  289. {
  290. case VT_DATE:
  291. {
  292. COleDateTime dt(vtVal);
  293. time = dt;
  294. }
  295. break;
  296. case VT_EMPTY:
  297. case VT_NULL:
  298. time.SetStatus(COleDateTime::null);
  299. break;
  300. default:
  301. return FALSE;
  302. }
  303. return TRUE;
  304. }
  305. catch(_com_error& e)
  306. {
  307. dump_com_error(e);
  308. return FALSE;
  309. }
  310. }
  311. BOOL CADOParameter::GetValue(_variant_t& vtValue)
  312. {
  313. try
  314. {
  315. vtValue = m_pParameter->Value;
  316. return TRUE;
  317. }
  318. catch(_com_error& e)
  319. {
  320. dump_com_error(e);
  321. return FALSE;
  322. }
  323. }
  324. void CADOParameter::dump_com_error(_com_error &e)
  325. {
  326. CString ErrorStr;
  327. _bstr_t bstrSource(e.Source());
  328. _bstr_t bstrDescription(e.Description());
  329. ErrorStr.Format( "CADOParameter Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n",
  330. e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
  331. m_strLastError = ErrorStr;
  332. m_dwLastError = e.Error();
  333. #ifdef _DEBUG
  334. AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
  335. #endif
  336. }
  337. CString CADOParameter::IntToStr(int nVal)
  338. {
  339. CString strRet;
  340. char buff[10];
  341. itoa(nVal, buff, 10);
  342. strRet = buff;
  343. return strRet;
  344. }
  345. CString CADOParameter::LongToStr(long lVal)
  346. {
  347. CString strRet;
  348. char buff[20];
  349. ltoa(lVal, buff, 10);
  350. strRet = buff;
  351. return strRet;
  352. }
  353. CString CADOParameter::ULongToStr(unsigned long ulVal)
  354. {
  355. CString strRet;
  356. char buff[20];
  357. ultoa(ulVal, buff, 10);
  358. strRet = buff;
  359. return strRet;
  360. }
  361. CString CADOParameter::DblToStr(double dblVal, int ndigits)
  362. {
  363. CString strRet;
  364. char buff[50];
  365. _gcvt(dblVal, ndigits, buff);
  366. strRet = buff;
  367. return strRet;
  368. }
  369. CString CADOParameter::DblToStr(float fltVal)
  370. {
  371. CString strRet = _T("");
  372. char buff[50];
  373. _gcvt(fltVal, 10, buff);
  374. strRet = buff;
  375. return strRet;
  376. }