123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include "StdAfx.h"
- #include "ADOParameter.h"
- CADOParameter::CADOParameter(int nType, long lSize, int nDirection, CString strName)
- {
- m_pParameter = NULL;
- m_pParameter.CreateInstance(__uuidof(Parameter));
- m_strName = _T("");
- m_pParameter->Direction = (ParameterDirectionEnum)nDirection;
- m_strName = strName;
- m_pParameter->Name = m_strName.AllocSysString();
- m_pParameter->Type = (DataTypeEnum)nType;
- m_pParameter->Size = lSize;
- m_nType = nType;
- }
- BOOL CADOParameter::SetValue(int nValue)
- {
- _variant_t vtVal;
- ASSERT(m_pParameter != NULL);
- vtVal.vt = VT_I2;
- vtVal.iVal = nValue;
- try
- {
- if(m_pParameter->Size == 0)
- m_pParameter->Size = sizeof(int);
- m_pParameter->Value = vtVal;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::SetValue(long lValue)
- {
- _variant_t vtVal;
- ASSERT(m_pParameter != NULL);
- vtVal.vt = VT_I4;
- vtVal.lVal = lValue;
- try
- {
- if(m_pParameter->Size == 0)
- m_pParameter->Size = sizeof(long);
- m_pParameter->Value = vtVal;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::SetValue(double dblValue)
- {
- _variant_t vtVal;
- ASSERT(m_pParameter != NULL);
- vtVal.vt = VT_R8;
- vtVal.dblVal = dblValue;
- try
- {
- if(m_pParameter->Size == 0)
- m_pParameter->Size = sizeof(double);
- m_pParameter->Value = vtVal;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::SetValue(CString strValue)
- {
- _variant_t vtVal;
- ASSERT(m_pParameter != NULL);
- if(!strValue.IsEmpty())
- vtVal.vt = VT_BSTR;
- else
- vtVal.vt = VT_NULL;
- vtVal.bstrVal = strValue.AllocSysString();
- try
- {
- if(m_pParameter->Size == 0)
- m_pParameter->Size = sizeof(char) * strValue.GetLength();
- m_pParameter->Value = vtVal;
- ::SysFreeString(vtVal.bstrVal);
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- ::SysFreeString(vtVal.bstrVal);
- return FALSE;
- }
- }
- BOOL CADOParameter::SetValue(COleDateTime time)
- {
- _variant_t vtVal;
- ASSERT(m_pParameter != NULL);
- vtVal.vt = VT_DATE;
- vtVal.date = time;
- try
- {
- if(m_pParameter->Size == 0)
- m_pParameter->Size = sizeof(DATE);
- m_pParameter->Value = vtVal;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::SetValue(_variant_t vtValue)
- {
- ASSERT(m_pParameter != NULL);
- try
- {
- if(m_pParameter->Size == 0)
- m_pParameter->Size = sizeof(VARIANT);
- m_pParameter->Value = vtValue;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::GetValue(int& nValue)
- {
- _variant_t vtVal;
- int nVal = 0;
- try
- {
- vtVal = m_pParameter->Value;
- switch(vtVal.vt)
- {
- case VT_BOOL:
- nVal = vtVal.boolVal;
- break;
- case VT_I2:
- case VT_UI1:
- nVal = vtVal.iVal;
- break;
- case VT_INT:
- nVal = vtVal.intVal;
- break;
- case VT_NULL:
- case VT_EMPTY:
- nVal = 0;
- break;
- default:
- nVal = vtVal.iVal;
- }
- nValue = nVal;
- return TRUE;
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::GetValue(long& lValue)
- {
- _variant_t vtVal;
- long lVal = 0;
- try
- {
- vtVal = m_pParameter->Value;
- if(vtVal.vt != VT_NULL && vtVal.vt != VT_EMPTY)
- lVal = vtVal.lVal;
- lValue = lVal;
- return TRUE;
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::GetValue(double& dbValue)
- {
- _variant_t vtVal;
- double dblVal;
- try
- {
- vtVal = m_pParameter->Value;
- switch(vtVal.vt)
- {
- case VT_R4:
- dblVal = vtVal.fltVal;
- break;
- case VT_R8:
- dblVal = vtVal.dblVal;
- break;
- case VT_DECIMAL:
- dblVal = vtVal.decVal.Lo32;
- dblVal *= (vtVal.decVal.sign == 128)? -1 : 1;
- dblVal /= pow(10.0, vtVal.decVal.scale);
- break;
- case VT_UI1:
- dblVal = vtVal.iVal;
- break;
- case VT_I2:
- case VT_I4:
- dblVal = vtVal.lVal;
- break;
- case VT_INT:
- dblVal = vtVal.intVal;
- break;
- case VT_NULL:
- case VT_EMPTY:
- dblVal = 0;
- break;
- default:
- dblVal = 0;
- }
- dbValue = dblVal;
- return TRUE;
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::GetValue(CString& strValue, CString strDateFormat)
- {
- _variant_t vtVal;
- CString strVal = _T("");
- try
- {
- vtVal = m_pParameter->Value;
- switch(vtVal.vt)
- {
- case VT_R4:
- strVal = DblToStr(vtVal.fltVal);
- break;
- case VT_R8:
- strVal = DblToStr(vtVal.dblVal);
- break;
- case VT_BSTR:
- strVal = vtVal.bstrVal;
- break;
- case VT_I2:
- case VT_UI1:
- strVal = IntToStr(vtVal.iVal);
- break;
- case VT_INT:
- strVal = IntToStr(vtVal.intVal);
- break;
- case VT_I4:
- strVal = LongToStr(vtVal.lVal);
- break;
- case VT_DECIMAL:
- {
- double val = vtVal.decVal.Lo32;
- val *= (vtVal.decVal.sign == 128)? -1 : 1;
- val /= pow(10.0, vtVal.decVal.scale);
- strVal = DblToStr(val);
- }
- break;
- case VT_DATE:
- {
- COleDateTime dt(vtVal);
- if(strDateFormat.IsEmpty())
- strDateFormat = _T("%Y-%m-%d %H:%M:%S");
- strVal = dt.Format(strDateFormat);
- }
- break;
- case VT_EMPTY:
- case VT_NULL:
- strVal.Empty();
- break;
- default:
- strVal.Empty();
- return FALSE;
- }
- strValue = strVal;
- return TRUE;
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::GetValue(COleDateTime& time)
- {
- _variant_t vtVal;
- try
- {
- vtVal = m_pParameter->Value;
- switch(vtVal.vt)
- {
- case VT_DATE:
- {
- COleDateTime dt(vtVal);
- time = dt;
- }
- break;
- case VT_EMPTY:
- case VT_NULL:
- time.SetStatus(COleDateTime::null);
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADOParameter::GetValue(_variant_t& vtValue)
- {
- try
- {
- vtValue = m_pParameter->Value;
- return TRUE;
- }
- catch(_com_error& e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- void CADOParameter::dump_com_error(_com_error &e)
- {
- CString ErrorStr;
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- ErrorStr.Format(_T("CADOParameter Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n"),
- e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription );
- m_strLastError = ErrorStr;
- m_dwLastError = e.Error();
- #ifdef _DEBUG
- AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
- #endif
- }
|