ADO中_variant_t变量的使用与转换

下载 源代码

用ADO做数据库,有个麻烦的事情就是_variant_t变量的使用,使用本例中封装的CConvert类转换就方便了。

 

view plain copy to clipboard print ?
  1. // Convert.h: interface for the CConvert class.   
  2. //   
  3. //////////////////////////////////////////////////////////////////////   
  4. #if !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_)   
  5. #define AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_   
  6. #if _MSC_VER > 1000   
  7. #pragma once   
  8. #endif // _MSC_VER > 1000   
  9. //////////////////////////////////////////////////////////////////////   
  10. // Construction/Destruction   
  11. /*   
  12. * Copyright (c) 2010,[email protected]   
  13. * All rights reserved.   
  14. *    
  15. * 文件名称:Convert.h   
  16. * 文件标识:数据类型转换  
  17. * 摘    要:主要简化ADO中数据类型的转换   
  18. *    
  19. * 当前版本:1.0   
  20. * 作    者:[email protected]  
  21. * 完成日期:2010年5月11日   
  22. * 发布Blog:http://blog.csdn.net/zyq5945/   
  23.  
  24. */    
  25. //////////////////////////////////////////////////////////////////////   
  26. #define TS(value) CConvert::ToString(value)   
  27. class CConvert     
  28. {   
  29.        
  30. public:   
  31.     static CString ToString(BYTE btValue);   
  32.     static CString ToString(int iValue);   
  33.     static CString ToString(unsigned int iValue);   
  34.     static CString ToString(long lValue);   
  35.     static CString ToString(unsigned long lValue);   
  36.     static CString ToString(__int64 i64Value);   
  37.     static CString ToString(unsigned __int64 i64Value);   
  38.     static CString ToString(float fltValue);   
  39.     static CString ToString(double dblValue);   
  40.     static CString ToString(const COleDateTime& time);   
  41.     static CString ToString(const _variant_t& var);   
  42. public:   
  43.     static BYTE ToByte(LPCTSTR lpszValue);   
  44.     static int ToInt(LPCTSTR lpszValue);   
  45.     static unsigned int ToUInt(LPCTSTR lpszValue);   
  46.     static long ToLong(LPCTSTR lpszValue);   
  47.     static unsigned long ToULong(LPCTSTR lpszValue);   
  48.     static __int64 ToInt64(LPCTSTR lpszValue);   
  49.     static unsigned __int64 ToUInt64(LPCTSTR lpszValue);   
  50.     static float ToFloat(LPCTSTR lpszValue);   
  51.     static double ToDouble(LPCTSTR lpszValue);   
  52.     static COleDateTime ToTime(LPCTSTR lpszValue);   
  53.     static _variant_t ToVariant(const COleDateTime& time);   
  54. };   
  55. extern CConvert Convert;   
  56. #endif // !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_)  

// Convert.h: interface for the CConvert class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_) #define AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ////////////////////////////////////////////////////////////////////// // Construction/Destruction /* * Copyright (c) 2010,[email protected] * All rights reserved. * * 文件名称:Convert.h * 文件标识:数据类型转换 * 摘 要:主要简化ADO中数据类型的转换 * * 当前版本:1.0 * 作 者:[email protected] * 完成日期:2010年5月11日 * 发布Blog:http://blog.csdn.net/zyq5945/ * */ ////////////////////////////////////////////////////////////////////// #define TS(value) CConvert::ToString(value) class CConvert { public: static CString ToString(BYTE btValue); static CString ToString(int iValue); static CString ToString(unsigned int iValue); static CString ToString(long lValue); static CString ToString(unsigned long lValue); static CString ToString(__int64 i64Value); static CString ToString(unsigned __int64 i64Value); static CString ToString(float fltValue); static CString ToString(double dblValue); static CString ToString(const COleDateTime& time); static CString ToString(const _variant_t& var); public: static BYTE ToByte(LPCTSTR lpszValue); static int ToInt(LPCTSTR lpszValue); static unsigned int ToUInt(LPCTSTR lpszValue); static long ToLong(LPCTSTR lpszValue); static unsigned long ToULong(LPCTSTR lpszValue); static __int64 ToInt64(LPCTSTR lpszValue); static unsigned __int64 ToUInt64(LPCTSTR lpszValue); static float ToFloat(LPCTSTR lpszValue); static double ToDouble(LPCTSTR lpszValue); static COleDateTime ToTime(LPCTSTR lpszValue); static _variant_t ToVariant(const COleDateTime& time); }; extern CConvert Convert; #endif // !defined(AFX_CONVERT_H__EC38F865_4607_4659_BAC8_AA6096C50EC7__INCLUDED_)

 

 

view plain copy to clipboard print ?
  1. // Convert.cpp: implementation of the CConvert class.   
  2. //   
  3. //////////////////////////////////////////////////////////////////////   
  4. #include "stdafx.h"   
  5. #include "Convert.h"   
  6. #include <comutil.h>   
  7. #include <comdef.h>   
  8. #ifdef _DEBUG   
  9. #undef THIS_FILE   
  10. static char THIS_FILE[]=__FILE__;   
  11. #define new DEBUG_NEW   
  12. #endif   
  13. //////////////////////////////////////////////////////////////////////   
  14. // Construction/Destruction   
  15. /*   
  16. * Copyright (c) 2010,[email protected]   
  17. * All rights reserved.   
  18. *    
  19. * 文件名称:Convert.cpp   
  20. * 文件标识:数据类型转换  
  21. * 摘    要:主要简化ADO中数据类型的转换   
  22. *    
  23. * 当前版本:1.0   
  24. * 作    者:zyq5945  
  25. * 完成日期:2010年5月11日   
  26. * 发布Blog:http://blog.csdn.net/zyq5945/  
  27.  
  28. */    
  29. //////////////////////////////////////////////////////////////////////   
  30. CConvert Convert;   
  31. CString CConvert::ToString(BYTE btValue)   
  32. {   
  33.     CString strValue;   
  34.     strValue.Format(_T("%d"), btValue);   
  35.     return strValue;   
  36. }   
  37. CString CConvert::ToString(int iValue)   
  38. {   
  39.     CString strValue;   
  40.     strValue.Format(_T("%d"), iValue);   
  41.     return strValue;   
  42. }   
  43. CString CConvert::ToString(unsigned int iValue)   
  44. {   
  45.     CString strValue;   
  46.     strValue.Format(_T("%u"), iValue);   
  47.     return strValue;   
  48. }   
  49. CString CConvert::ToString(long lValue)   
  50. {   
  51.     CString strValue;   
  52.     strValue.Format(_T("%d"), lValue);   
  53.     return strValue;   
  54. }   
  55. CString CConvert::ToString(unsigned long lValue)   
  56. {   
  57.     CString strValue;   
  58.     strValue.Format(_T("%u"), lValue);   
  59.     return strValue;   
  60. }   
  61. CString CConvert::ToString(__int64 i64Value)   
  62. {   
  63.     CString strValue;   
  64.     strValue.Format(_T("%I64d"), i64Value);   
  65.     return strValue;   
  66. }   
  67. CString CConvert::ToString(unsigned __int64 i64Value)   
  68. {   
  69.     CString strValue;   
  70.     strValue.Format(_T("%I64u"), i64Value);   
  71.     return strValue;   
  72. }   
  73. CString CConvert::ToString(float fltValue)   
  74. {   
  75.     CString strValue;   
  76.     strValue.Format(_T("%f"), fltValue);   
  77.     return strValue;   
  78. }   
  79. CString CConvert::ToString(double dblValue)   
  80. {   
  81.     CString strValue;   
  82.     strValue.Format(_T("%f"), dblValue);   
  83.     return strValue;   
  84. }   
  85. // 时间输出格式:2010-05-06 22:07:08   
  86. CString CConvert::ToString(const COleDateTime& time)   
  87. {   
  88.     CString strValue;   
  89.     strValue = time.Format(_T("%Y-%m-%d %H:%M:%S"));   
  90.     return strValue;   
  91. }   
  92. CString CConvert::ToString(const _variant_t& var)   
  93. {   
  94.     CString strValue;   
  95.     switch (var.vt)   
  96.     {   
  97.     case VT_BSTR: //字符串   
  98.     case VT_LPSTR:   
  99.     case VT_LPWSTR:   
  100.         strValue = (LPCTSTR)(_bstr_t)var;   
  101.         break;   
  102.     case VT_I1:   //无符号字符   
  103.     case VT_UI1:   
  104.         strValue.Format(_T("%d"), var.bVal);   
  105.         break;   
  106.     case VT_I2:   //短整型   
  107.         strValue.Format(_T("%d"), var.iVal);   
  108.         break;   
  109.     case VT_UI2:   //无符号短整型   
  110.         strValue.Format(_T("%u"), var.uiVal);   
  111.         break;   
  112.     case VT_INT: //整型   
  113.         strValue.Format(_T("%d"), var.intVal);   
  114.         break;   
  115.     case VT_I4:   //整型   
  116.     case VT_I8:   //长整型   
  117.         strValue.Format(_T("%d"), var.lVal);   
  118.         break;   
  119.     case VT_UINT:   //无符号整型   
  120.         strValue.Format(_T("%d"), var.uintVal);   
  121.         break;   
  122.     case VT_UI4:    //无符号整型   
  123.     case VT_UI8:    //无符号长整型   
  124.         strValue.Format(_T("%d"), var.ulVal);   
  125.         break;   
  126.     case VT_VOID:   
  127.         strValue.Format(_T("%08x"), var.byref);   
  128.         break;   
  129.     case VT_R4:   //浮点型   
  130.         strValue.Format(_T("%f"), var.fltVal);   
  131.         break;   
  132.     case VT_R8:   //双精度型   
  133.         strValue.Format(_T("%f"), var.dblVal);   
  134.         break;   
  135.     case VT_DECIMAL: //小数   
  136.         strValue.Format(_T("%f"), (double)var);   
  137.         break;   
  138.     case VT_CY:   
  139.         {   
  140.             COleCurrency cy = var.cyVal;   
  141.             strValue = cy.Format();   
  142.         }   
  143.         break;   
  144.     case VT_BLOB:   
  145.     case VT_BLOB_OBJECT:   
  146.     case 0x2011:   
  147.         strValue = _T("[BLOB]");   
  148.         break;   
  149.     case VT_BOOL:   //布尔型     
  150.         strValue = var.boolVal ? _T("TRUE") : _T("FALSE");   
  151.         break;   
  152.     case VT_DATE: //日期型   
  153.         {   
  154.             DATE dt = var.date;   
  155.             COleDateTime da = COleDateTime(dt);   
  156.             strValue = da.Format(_T("%Y-%m-%d %H:%M:%S"));   
  157.         }   
  158.         break;   
  159.     case VT_NULL://NULL值   
  160.     case VT_EMPTY:   //空   
  161.         strValue = _T("");   
  162.         break;   
  163.     case VT_UNKNOWN:   //未知类型   
  164.     default:   
  165.         strValue = _T("VT_UNKNOW");   
  166.         break;   
  167.     }   
  168.        
  169.     return strValue;   
  170. }   
  171. BYTE CConvert::ToByte(LPCTSTR lpszValue)   
  172. {   
  173.     BYTE btValue;   
  174.     btValue = (BYTE)_ttoi(lpszValue);   
  175.     return btValue;   
  176. }   
  177. int CConvert::ToInt(LPCTSTR lpszValue)   
  178. {   
  179.     int iValue;   
  180.     iValue = _ttoi(lpszValue);   
  181.     return iValue;   
  182. }   
  183. unsigned int CConvert::ToUInt(LPCTSTR lpszValue)   
  184. {   
  185.     unsigned int iValue;   
  186.     iValue = _ttoi(lpszValue);   
  187.     return iValue;   
  188. }   
  189. long CConvert::ToLong(LPCTSTR lpszValue)   
  190. {   
  191.     long lValue;   
  192.     lValue = _ttol(lpszValue);   
  193.     return lValue;   
  194. }   
  195. unsigned long CConvert::ToULong(LPCTSTR lpszValue)   
  196. {   
  197.     unsigned long lValue;   
  198.     lValue = _ttol(lpszValue);   
  199.     return lValue;   
  200. }   
  201. __int64 CConvert::ToInt64(LPCTSTR lpszValue)   
  202. {   
  203.     __int64 i64Value;   
  204.     i64Value = _ttoi64(lpszValue);   
  205.     return i64Value;   
  206. }   
  207. unsigned __int64 CConvert::ToUInt64(LPCTSTR lpszValue)   
  208. {   
  209.     unsigned __int64 i64Value;   
  210.     i64Value = _ttoi64(lpszValue);   
  211.     return i64Value;   
  212. }   
  213. float CConvert::ToFloat(LPCTSTR lpszValue)   
  214. {   
  215.     float fltValue;   
  216. #ifdef _MBCS   
  217.     fltValue = (float)atof(lpszValue);   
  218. #else   
  219.     fltValue = (float)wtof(lpszValue);   
  220. #endif   
  221.     return fltValue;   
  222. }   
  223. double CConvert::ToDouble(LPCTSTR lpszValue)   
  224. {   
  225.     double dblValue;   
  226. #ifdef _MBCS   
  227.     dblValue = atof(lpszValue);   
  228. #else   
  229.     dblValue = wtof(lpszValue);   
  230. #endif   
  231.     return dblValue;   
  232. }   
  233. // 时间格式例子:2010-05-06 22:07:08   
  234. //               2010 05 06 22 07 08   
  235. //               2010:05:06 22:07:08   
  236. //               2010-05-06-22-07-08   
  237. // 只要是“- :”分割的时间格式都符合   
  238. COleDateTime CConvert::ToTime(LPCTSTR lpszValue)   
  239. {   
  240.     unsigned int iArray[6] = {2010, 5, 11, 12, 00, 00};   
  241.     int nIndex = 0;   
  242. #if _MSC_VER >= 1310  //VC6.0不支持CStringAlt::Tokenize   
  243.     CString strTmp;   
  244.     int curPos = 0;   
  245.     CString strValue(lpszValue);   
  246.        
  247.     strTmp = strValue.Tokenize(_T("- :"),curPos);   
  248.     while (strTmp != _T("") && nIndex <6)   
  249.     {   
  250.         iArray[nIndex++] = ToUInt(strTmp);   
  251.         strTmp = strValue.Tokenize(_T("- :"), curPos);   
  252.     };   
  253. #else   
  254.     TCHAR tChar[MAX_PATH] = {0};   
  255.     TCHAR tCharTmp[MAX_PATH] = {0};   
  256.     TCHAR seps[]   = "- :";   
  257.     TCHAR *next_token = NULL;   
  258.     ASSERT(_tcslen(lpszValue) < MAX_PATH);   
  259.     RtlCopyMemory(tChar, lpszValue, _tcslen(lpszValue) * sizeof(TCHAR));   
  260.     next_token = _tcstok(tChar, seps);   
  261.     while ( next_token != NULL && nIndex <6)   
  262.     {   
  263.         iArray[nIndex++] = _ttoi(next_token);   
  264.         next_token = _tcstok( NULL, seps);   
  265.     }   
  266.        
  267. #endif   
  268.     COleDateTime time(iArray[0], iArray[1], iArray[2],   
  269.         iArray[3], iArray[4], iArray[5]);   
  270.     return time;   
  271. }   
  272. _variant_t CConvert::ToVariant(const COleDateTime& time)   
  273. {   
  274.     _variant_t vtVal;   
  275.        
  276.     vtVal.vt = VT_DATE;   
  277.     vtVal.date = time;   
  278.     return vtVal;   
  279. }  

// Convert.cpp: implementation of the CConvert class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Convert.h" #include <comutil.h> #include <comdef.h> #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction /* * Copyright (c) 2010,[email protected] * All rights reserved. * * 文件名称:Convert.cpp * 文件标识:数据类型转换 * 摘 要:主要简化ADO中数据类型的转换 * * 当前版本:1.0 * 作 者:zyq5945 * 完成日期:2010年5月11日 * 发布Blog:http://blog.csdn.net/zyq5945/ * */ ////////////////////////////////////////////////////////////////////// CConvert Convert; CString CConvert::ToString(BYTE btValue) { CString strValue; strValue.Format(_T("%d"), btValue); return strValue; } CString CConvert::ToString(int iValue) { CString strValue; strValue.Format(_T("%d"), iValue); return strValue; } CString CConvert::ToString(unsigned int iValue) { CString strValue; strValue.Format(_T("%u"), iValue); return strValue; } CString CConvert::ToString(long lValue) { CString strValue; strValue.Format(_T("%d"), lValue); return strValue; } CString CConvert::ToString(unsigned long lValue) { CString strValue; strValue.Format(_T("%u"), lValue); return strValue; } CString CConvert::ToString(__int64 i64Value) { CString strValue; strValue.Format(_T("%I64d"), i64Value); return strValue; } CString CConvert::ToString(unsigned __int64 i64Value) { CString strValue; strValue.Format(_T("%I64u"), i64Value); return strValue; } CString CConvert::ToString(float fltValue) { CString strValue; strValue.Format(_T("%f"), fltValue); return strValue; } CString CConvert::ToString(double dblValue) { CString strValue; strValue.Format(_T("%f"), dblValue); return strValue; } // 时间输出格式:2010-05-06 22:07:08 CString CConvert::ToString(const COleDateTime& time) { CString strValue; strValue = time.Format(_T("%Y-%m-%d %H:%M:%S")); return strValue; } CString CConvert::ToString(const _variant_t& var) { CString strValue; switch (var.vt) { case VT_BSTR: //字符串 case VT_LPSTR: case VT_LPWSTR: strValue = (LPCTSTR)(_bstr_t)var; break; case VT_I1: //无符号字符 case VT_UI1: strValue.Format(_T("%d"), var.bVal); break; case VT_I2: //短整型 strValue.Format(_T("%d"), var.iVal); break; case VT_UI2: //无符号短整型 strValue.Format(_T("%u"), var.uiVal); break; case VT_INT: //整型 strValue.Format(_T("%d"), var.intVal); break; case VT_I4: //整型 case VT_I8: //长整型 strValue.Format(_T("%d"), var.lVal); break; case VT_UINT: //无符号整型 strValue.Format(_T("%d"), var.uintVal); break; case VT_UI4: //无符号整型 case VT_UI8: //无符号长整型 strValue.Format(_T("%d"), var.ulVal); break; case VT_VOID: strValue.Format(_T("%08x"), var.byref); break; case VT_R4: //浮点型 strValue.Format(_T("%f"), var.fltVal); break; case VT_R8: //双精度型 strValue.Format(_T("%f"), var.dblVal); break; case VT_DECIMAL: //小数 strValue.Format(_T("%f"), (double)var); break; case VT_CY: { COleCurrency cy = var.cyVal; strValue = cy.Format(); } break; case VT_BLOB: case VT_BLOB_OBJECT: case 0x2011: strValue = _T("[BLOB]"); break; case VT_BOOL: //布尔型 strValue = var.boolVal ? _T("TRUE") : _T("FALSE"); break; case VT_DATE: //日期型 { DATE dt = var.date; COleDateTime da = COleDateTime(dt); strValue = da.Format(_T("%Y-%m-%d %H:%M:%S")); } break; case VT_NULL://NULL值 case VT_EMPTY: //空 strValue = _T(""); break; case VT_UNKNOWN: //未知类型 default: strValue = _T("VT_UNKNOW"); break; } return strValue; } BYTE CConvert::ToByte(LPCTSTR lpszValue) { BYTE btValue; btValue = (BYTE)_ttoi(lpszValue); return btValue; } int CConvert::ToInt(LPCTSTR lpszValue) { int iValue; iValue = _ttoi(lpszValue); return iValue; } unsigned int CConvert::ToUInt(LPCTSTR lpszValue) { unsigned int iValue; iValue = _ttoi(lpszValue); return iValue; } long CConvert::ToLong(LPCTSTR lpszValue) { long lValue; lValue = _ttol(lpszValue); return lValue; } unsigned long CConvert::ToULong(LPCTSTR lpszValue) { unsigned long lValue; lValue = _ttol(lpszValue); return lValue; } __int64 CConvert::ToInt64(LPCTSTR lpszValue) { __int64 i64Value; i64Value = _ttoi64(lpszValue); return i64Value; } unsigned __int64 CConvert::ToUInt64(LPCTSTR lpszValue) { unsigned __int64 i64Value; i64Value = _ttoi64(lpszValue); return i64Value; } float CConvert::ToFloat(LPCTSTR lpszValue) { float fltValue; #ifdef _MBCS fltValue = (float)atof(lpszValue); #else fltValue = (float)wtof(lpszValue); #endif return fltValue; } double CConvert::ToDouble(LPCTSTR lpszValue) { double dblValue; #ifdef _MBCS dblValue = atof(lpszValue); #else dblValue = wtof(lpszValue); #endif return dblValue; } // 时间格式例子:2010-05-06 22:07:08 // 2010 05 06 22 07 08 // 2010:05:06 22:07:08 // 2010-05-06-22-07-08 // 只要是“- :”分割的时间格式都符合 COleDateTime CConvert::ToTime(LPCTSTR lpszValue) { unsigned int iArray[6] = {2010, 5, 11, 12, 00, 00}; int nIndex = 0; #if _MSC_VER >= 1310 //VC6.0不支持CStringAlt::Tokenize CString strTmp; int curPos = 0; CString strValue(lpszValue); strTmp = strValue.Tokenize(_T("- :"),curPos); while (strTmp != _T("") && nIndex <6) { iArray[nIndex++] = ToUInt(strTmp); strTmp = strValue.Tokenize(_T("- :"), curPos); }; #else TCHAR tChar[MAX_PATH] = {0}; TCHAR tCharTmp[MAX_PATH] = {0}; TCHAR seps[] = "- :"; TCHAR *next_token = NULL; ASSERT(_tcslen(lpszValue) < MAX_PATH); RtlCopyMemory(tChar, lpszValue, _tcslen(lpszValue) * sizeof(TCHAR)); next_token = _tcstok(tChar, seps); while ( next_token != NULL && nIndex <6) { iArray[nIndex++] = _ttoi(next_token); next_token = _tcstok( NULL, seps); } #endif COleDateTime time(iArray[0], iArray[1], iArray[2], iArray[3], iArray[4], iArray[5]); return time; } _variant_t CConvert::ToVariant(const COleDateTime& time) { _variant_t vtVal; vtVal.vt = VT_DATE; vtVal.date = time; return vtVal; }

 

由于_variant_t重载了以下等号运算符,所以下面类型可以直接给_variant_t赋值

 

view plain copy to clipboard print ?
  1. VARIANT    
  2.   
  3. VARIANT*    
  4.   
  5. _variant_t   
  6.   
  7. short    
  8.   
  9. long  
  10.   
  11. float  
  12.   
  13. double  
  14.   
  15. CY   
  16.   
  17. _bstr_t   
  18.   
  19. wchar_t  
  20.   
  21. char  
  22.   
  23. IDispatch*   
  24.   
  25. bool  
  26.   
  27. IUnknown*   
  28.   
  29. DECIMAL   
  30.   
  31. BYTE  
  32.   
  33. char  
  34.   
  35. unsigned short  
  36.   
  37. unsigned long  
  38.   
  39. int  
  40.   
  41. unsigned int  
  42.   
  43. __int64  
  44.   
  45. unsigned __int64  

VARIANT VARIANT* _variant_t short long float double CY _bstr_t wchar_t char IDispatch* bool IUnknown* DECIMAL BYTE char unsigned short unsigned long int unsigned int __int64 unsigned __int64

 

由于COleDateTime 重载了以下等号运算符,所以下面类型可以直接给COleDateTime 赋值

 

view plain copy to clipboard print ?
  1. VARIANT   
  2.   
  3. DATE   
  4.   
  5. time_t  
  6.   
  7. __time64_t  
  8.   
  9. SYSTEMTIME   
  10.   
  11. FILETIME   
  12.   
  13. UDATE  

VARIANT DATE time_t __time64_t SYSTEMTIME FILETIME UDATE

 

本例中CConvert类也增加了一个ToVariant函数把COleDateTime转换为_variant_t类型变量

view plain copy to clipboard print ?
  1. _variant_t varValue;   
  2. CString strValue;   
  3. COleDateTime dt;   
  4. try  
  5. {   
  6.     // 写入各字段值   
  7.     m_pRecordset->AddNew();   
  8.     // byte变量   
  9.     GetDlgItemText(IDC_EDIT1, strValue);   
  10.     varValue = CConvert::ToByte(strValue);     
  11.     m_pRecordset->PutCollect("tByte", varValue);   
  12.        
  13.     // int变量   
  14.     GetDlgItemText(IDC_EDIT2, strValue);   
  15.     varValue = (long)CConvert::ToInt(strValue);    
  16.     m_pRecordset->PutCollect("tInt", varValue);   
  17.     // long变量   
  18.     GetDlgItemText(IDC_EDIT3, strValue);   
  19.     varValue = CConvert::ToLong(strValue);     
  20.     m_pRecordset->PutCollect("tLong", varValue);   
  21.     // float变量   
  22.     GetDlgItemText(IDC_EDIT4, strValue);   
  23.     varValue = CConvert::ToFloat(strValue);    
  24.     m_pRecordset->PutCollect("tFloat", varValue);   
  25.     // double变量   
  26.     GetDlgItemText(IDC_EDIT5, strValue);   
  27.     varValue = CConvert::ToDouble(strValue);       
  28.     m_pRecordset->PutCollect("tDouble", varValue);   
  29.     // COleDateTime变量   
  30.     GetDlgItemText(IDC_EDIT6, strValue);   
  31.     dt = CConvert::ToTime(strValue);   
  32.     varValue = CConvert::ToVariant(dt);   
  33.     m_pRecordset->PutCollect("tTime", varValue);   
  34.     // bool变量   
  35.     GetDlgItemText(IDC_EDIT7, strValue);   
  36.     if (!strValue.CompareNoCase("TRUE"))   
  37.     {   
  38.         varValue = true;   
  39.     }   
  40.     else  
  41.     {   
  42.         varValue = false;   
  43.     }   
  44.     m_pRecordset->PutCollect("tBool", varValue);   
  45.     // 字符串变量   
  46.     GetDlgItemText(IDC_EDIT8, strValue);   
  47.     varValue = strValue;   
  48.     m_pRecordset->PutCollect("tText", varValue);   
  49.     m_pRecordset->Update();   
  50.        
  51.     // 更新显示其库内容   
  52.     int nCurSel = m_list.GetItemCount();   
  53.     ADOListRecord();   
  54.     SetListSel(nCurSel);   
  55.   
  56.     AfxMessageBox("插入成功!");   
  57. }   
  58. catch(_com_error& e)   
  59. {   
  60.     dump_com_error(e);   
  61. }  

_variant_t varValue; CString strValue; COleDateTime dt; try { // 写入各字段值 m_pRecordset->AddNew(); // byte变量 GetDlgItemText(IDC_EDIT1, strValue); varValue = CConvert::ToByte(strValue); m_pRecordset->PutCollect("tByte", varValue); // int变量 GetDlgItemText(IDC_EDIT2, strValue); varValue = (long)CConvert::ToInt(strValue); m_pRecordset->PutCollect("tInt", varValue); // long变量 GetDlgItemText(IDC_EDIT3, strValue); varValue = CConvert::ToLong(strValue); m_pRecordset->PutCollect("tLong", varValue); // float变量 GetDlgItemText(IDC_EDIT4, strValue); varValue = CConvert::ToFloat(strValue); m_pRecordset->PutCollect("tFloat", varValue); // double变量 GetDlgItemText(IDC_EDIT5, strValue); varValue = CConvert::ToDouble(strValue); m_pRecordset->PutCollect("tDouble", varValue); // COleDateTime变量 GetDlgItemText(IDC_EDIT6, strValue); dt = CConvert::ToTime(strValue); varValue = CConvert::ToVariant(dt); m_pRecordset->PutCollect("tTime", varValue); // bool变量 GetDlgItemText(IDC_EDIT7, strValue); if (!strValue.CompareNoCase("TRUE")) { varValue = true; } else { varValue = false; } m_pRecordset->PutCollect("tBool", varValue); // 字符串变量 GetDlgItemText(IDC_EDIT8, strValue); varValue = strValue; m_pRecordset->PutCollect("tText", varValue); m_pRecordset->Update(); // 更新显示其库内容 int nCurSel = m_list.GetItemCount(); ADOListRecord(); SetListSel(nCurSel); AfxMessageBox("插入成功!"); } catch(_com_error& e) { dump_com_error(e); }

 

参考文献:ADO数据库编程-总结2

你可能感兴趣的:(Date,Path,token,float,byte,2010)