ADOParameter.cpp 6.2 KB

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