使用ADO操作数据库时一个好用的VARIANT类!

类别:VC语言 点击:0 评论:0 推荐:

 

inline CString VTOCSTR(VARIANT *v)
{
 if(v->vt==VT_BSTR)
 {
  CString str((LPCWSTR)v->bstrVal);
  return str;
 }
 else
 {
  return CString("");
 }
}

#define VTOLONG(v) ((v).vt==VT_I4 ? (LONG)(v).lVal:0L)
#define VTODATE(v) ((v).vt==VT_DATE ? (CTime)(v).iVal:0L)


class CVar : public VARIANT
 {
public:
 CVar()
  {
  VariantInit(this);
  }
 CVar(VARTYPE vt, SCODE scode = 0)
  {
  VariantInit(this);
  this->vt = vt;
  this->scode = scode;
  }
 CVar(VARIANT var)
  {
  *this = var;
  }
 ~CVar()
  {
  VariantClear(this);
  }

 // ASSIGNMENT OPS.
 CVar & operator=(PCWSTR pcwstr)
  {
  VariantClear(this);
  if (NULL == (this->bstrVal = SysAllocStringLen(pcwstr, wcslen(pcwstr))))
   throw E_OUTOFMEMORY;
  this->vt = VT_BSTR;
  return *this;
  }
 CVar & operator=(VARIANT var)
  {
  HRESULT hr;

  VariantClear(this);
  if (FAILED(hr = VariantCopy(this, &var)))
   throw hr;
  return *this;
  }

.
 operator STR() const
 {
  if(VT_BSTR == this->vt)
   return this->bstrVal;
  else
   return NULL;
 }

 operator LONG() const
 {
  if (VT_I4 == this->vt)
   return this->lVal;
  else
   return NULL;
 }
 operator DATE() const
 {
  if (VT_DATE == this->vt)
   return this->date;
  else
   return NULL;
 }
 operator CY() const
 {
  COleCurrency cyNULL;
  cyNULL.SetCurrency(0,0);
  if (VT_CY == this->vt)
   return this->cyVal;
  else
   return cyNULL;
 }
 HRESULT Clear()
 {
  return VariantClear(this);
 }
};

这是我在MSDN中整理并曾强的一个类,希望对进行ADO操作数据库的伙计们有所帮助!

本文地址:http://com.8s8s.com/it/it2568.htm