1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951 |
- #include "StdAfx.h"
- #include "ADORecordset.h"
- #define ChunkSize 100
- CADORecordset::CADORecordset()
- {
- m_pRecordset = NULL;
- m_pCmd = NULL;
- m_strQuery = _T("");
- m_strLastError = _T("");
- m_dwLastError = 0;
- m_pRecBinding = NULL;
- m_pRecordset.CreateInstance(__uuidof(Recordset));
- m_pCmd.CreateInstance(__uuidof(Command));
- m_nEditStatus = CADORecordset::dbEditNone;
- m_nSearchDirection = CADORecordset::searchForward;
- }
- CADORecordset::CADORecordset(CADODatabase* pAdoDatabase)
- {
- m_pRecordset = NULL;
- m_pCmd = NULL;
- m_strQuery = _T("");
- m_strLastError = _T("");
- m_dwLastError = 0;
- m_pRecBinding = NULL;
- m_pRecordset.CreateInstance(__uuidof(Recordset));
- m_pCmd.CreateInstance(__uuidof(Command));
- m_nEditStatus = CADORecordset::dbEditNone;
- m_nSearchDirection = CADORecordset::searchForward;
- m_pConnection = pAdoDatabase->GetActiveConnection();
- }
- BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, const int& nOption, const int& nCursorLocation)
- {
- Close();
- if(_tcscmp(lpstrExec, _T("")) != 0)
- m_strQuery = lpstrExec;
- ASSERT(!m_strQuery.IsEmpty());
- if(m_pConnection == NULL)
- m_pConnection = mpdb;
- m_strQuery.TrimLeft();
- BOOL bIsSelect = m_strQuery.Mid(0, (int)_tcslen(_T("Select "))).CompareNoCase(_T("select ")) == 0 && (nOption == openUnknown);
- //adUseClient是需要把数据传输到本地,应用举例是断开数据库进行Recordset操作
- //adUseServer是直接在数据库中进行操作,下面这个例子在数据量大的时候,进行Find操作,比adUseClient省了很多时间,
- // Jeff.经验证,以上说法不完全对。当使用adUseServer时,只有程序与数据库都在同一电脑上时实施大数据量时才比adUseClient快。
- // Jeff.但当程序与数据库分居两地时,情况完全相反。
- try
- {
- m_pRecordset->CursorType = adOpenStatic;
- //m_pRecordset->CursorLocation = adUseClient;// //adUseServer使用adUseClient无法使用update更新AppendChunk的内容;
- m_pRecordset->CursorLocation = (CursorLocationEnum)nCursorLocation;
- if(bIsSelect || nOption == openQuery || nOption == openUnknown)
- m_pRecordset->Open((LPCTSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
- adOpenStatic, adLockOptimistic, adCmdUnknown);
- else if(nOption == openTable)
- m_pRecordset->Open((LPCTSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
- adOpenKeyset, adLockOptimistic, adCmdTable);
- else if(nOption == openStoredProc)
- {
- m_pCmd->ActiveConnection = mpdb;
- m_pCmd->CommandText = _bstr_t(m_strQuery);
- m_pCmd->CommandType = adCmdStoredProc;
- m_pConnection->CursorLocation = adUseClient;
- m_pRecordset = m_pCmd->Execute(NULL, NULL, adCmdText);
- }
- else if( nOption == openCmdText)
- {
- m_pRecordset->CursorType = adOpenKeyset;
- m_pRecordset->CursorLocation = adUseClient;
- m_pRecordset->Open(_variant_t(m_strQuery),_variant_t((IDispatch*)mpdb, TRUE)
- ,adOpenKeyset
- ,adLockOptimistic
- ,adCmdText);
- }
- else
- {
- TRACE( "Unknown parameter. %s %d", lpstrExec,nOption);
- return FALSE;
- }
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- return m_pRecordset != NULL && m_pRecordset->GetState()!= adStateClosed;
- }
- BOOL CADORecordset::Open(LPCTSTR lpstrExec, const int& nOption, const int& nCursorLocation)
- {
- ASSERT(m_pConnection != NULL);
- ASSERT(m_pConnection->GetState() != adStateClosed);
- return Open(m_pConnection, lpstrExec, nOption, nCursorLocation);
- }
- BOOL CADORecordset::OpenSchema(int nSchema, LPCTSTR SchemaID /*= _T("")*/)
- {
- try
- {
- _variant_t vtSchemaID = vtMissing;
- if(_tcslen(SchemaID) != 0)
- {
- vtSchemaID = SchemaID;
- nSchema = adSchemaProviderSpecific;
- }
- m_pRecordset = m_pConnection->OpenSchema((enum SchemaEnum)nSchema, vtMissing, vtSchemaID);
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::Requery()
- {
- if(IsOpen())
- {
- try
- {
- m_pRecordset->Requery(adExecuteRecord);
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- return TRUE;
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, double& dbValue)
- {
- double val = (double)NULL;
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- switch(vtFld.vt)
- {
- case VT_R4:
- val = vtFld.fltVal;
- break;
- case VT_R8:
- val = vtFld.dblVal;
- break;
- case VT_DECIMAL:
- val = vtFld.decVal.Lo32;
- val *= (vtFld.decVal.sign == 128) ? -1 : 1;
- val /= pow(10.0, vtFld.decVal.scale);
- break;
- case VT_UI1:
- val = vtFld.iVal;
- break;
- case VT_I2:
- case VT_I4:
- val = vtFld.lVal;
- break;
- case VT_INT:
- val = vtFld.intVal;
- break;
- case VT_CY: //Added by John Andy Johnson!!!!
- vtFld.ChangeType(VT_R8);
- val = vtFld.dblVal;
- break;
- case VT_NULL:
- case VT_EMPTY:
- val = 0;
- break;
- default:
- val = vtFld.dblVal;
- }
- dbValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, double& dbValue)
- {
- double val = (double)NULL;
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- switch(vtFld.vt)
- {
- case VT_R4:
- val = vtFld.fltVal;
- break;
- case VT_R8:
- val = vtFld.dblVal;
- break;
- case VT_DECIMAL:
- val = vtFld.decVal.Lo32;
- val *= (vtFld.decVal.sign == 128)? -1 : 1;
- val /= pow(10.0, vtFld.decVal.scale);
- break;
- case VT_UI1:
- val = vtFld.iVal;
- break;
- case VT_I2:
- case VT_I4:
- val = vtFld.lVal;
- break;
- case VT_INT:
- val = vtFld.intVal;
- break;
- case VT_CY: //Added by John Andy Johnson!!!!
- vtFld.ChangeType(VT_R8);
- val = vtFld.dblVal;
- break;
- case VT_NULL:
- case VT_EMPTY:
- val = 0;
- break;
- default:
- val = 0;
- }
- dbValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, long& lValue)
- {
- long val = (long)NULL;
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
- val = vtFld.lVal;
- lValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, long& lValue)
- {
- long val = (long)NULL;
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
- val = vtFld.lVal;
- lValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, unsigned long& ulValue)
- {
- long val = (long)NULL;
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
- val = vtFld.ulVal;
- ulValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, unsigned long& ulValue)
- {
- long val = (long)NULL;
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- if(vtFld.vt != VT_NULL && vtFld.vt != VT_EMPTY)
- val = vtFld.ulVal;
- ulValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, int& nValue)
- {
- int val = NULL;
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- switch(vtFld.vt)
- {
- case VT_BOOL:
- val = vtFld.boolVal;
- break;
- case VT_I2:
- case VT_UI1:
- val = vtFld.iVal;
- break;
- case VT_INT:
- val = vtFld.intVal;
- break;
- case VT_NULL:
- case VT_EMPTY:
- val = 0;
- break;
- default:
- val = vtFld.iVal;
- }
- nValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, int& nValue)
- {
- int val = (int)NULL;
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- switch(vtFld.vt)
- {
- case VT_BOOL:
- val = vtFld.boolVal;
- break;
- case VT_I2:
- case VT_UI1:
- val = vtFld.iVal;
- break;
- case VT_INT:
- val = vtFld.intVal;
- break;
- case VT_NULL:
- case VT_EMPTY:
- val = 0;
- break;
- default:
- val = vtFld.iVal;
- }
- nValue = val;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, CString& strValue, CString strDateFormat)
- {
- CString str = _T("");
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- switch(vtFld.vt)
- {
- case VT_R4:
- str = DblToStr(vtFld.fltVal);
- break;
- case VT_R8:
- str = DblToStr(vtFld.dblVal);
- break;
- case VT_BSTR:
- str = vtFld.bstrVal;
- break;
- case VT_I2:
- case VT_UI1:
- str = IntToStr(vtFld.iVal);
- break;
- case VT_INT:
- str = IntToStr(vtFld.intVal);
- break;
- case VT_I4:
- str = LongToStr(vtFld.lVal);
- break;
- case VT_UI4:
- str = ULongToStr(vtFld.ulVal);
- break;
- case VT_DECIMAL:
- {
- double val = vtFld.decVal.Lo32;
- val *= (vtFld.decVal.sign == 128)? -1 : 1;
- val /= pow(10.0, vtFld.decVal.scale);
- str = DblToStr(val);
- }
- break;
- case VT_DATE:
- {
- COleDateTime dt(vtFld);
- if(strDateFormat.IsEmpty())
- strDateFormat = _T("%Y-%m-%d %H:%M:%S");
- str = dt.Format(strDateFormat);
- }
- break;
- case VT_CY: //Added by John Andy Johnson!!!
- {
- vtFld.ChangeType(VT_R8);
- CString str;
- str.Format(_T("%f"), vtFld.dblVal);
- _TCHAR pszFormattedNumber[64];
- // LOCALE_USER_DEFAULT
- if(GetCurrencyFormat(
- LOCALE_USER_DEFAULT, // locale for which string is to be formatted
- 0, // bit flag that controls the function's operation
- str, // pointer to input number string
- NULL, // pointer to a formatting information structure
- // NULL = machine default locale settings
- pszFormattedNumber, // pointer to output buffer
- 63)) // size of output buffer
- {
- str = pszFormattedNumber;
- }
- }
- break;
- case VT_EMPTY:
- case VT_NULL:
- str.Empty();
- break;
- case VT_BOOL:
- str = vtFld.boolVal == VARIANT_TRUE? 'T':'F';
- break;
- default:
- str.Empty();
- return FALSE;
- }
- strValue = str;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, CString& strValue, CString strDateFormat)
- {
- CString str = _T("");
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- switch(vtFld.vt)
- {
- case VT_R4:
- str = DblToStr(vtFld.fltVal);
- break;
- case VT_R8:
- str = DblToStr(vtFld.dblVal);
- break;
- case VT_BSTR:
- str = vtFld.bstrVal;
- break;
- case VT_I2:
- case VT_UI1:
- str = IntToStr(vtFld.iVal);
- break;
- case VT_INT:
- str = IntToStr(vtFld.intVal);
- break;
- case VT_I4:
- str = LongToStr(vtFld.lVal);
- break;
- case VT_UI4:
- str = ULongToStr(vtFld.ulVal);
- break;
- case VT_DECIMAL:
- {
- double val = vtFld.decVal.Lo32;
- val *= (vtFld.decVal.sign == 128)? -1 : 1;
- val /= pow(10.0, vtFld.decVal.scale);
- str = DblToStr(val);
- }
- break;
- case VT_DATE:
- {
- COleDateTime dt(vtFld);
- if(strDateFormat.IsEmpty())
- strDateFormat = _T("%Y-%m-%d %H:%M:%S");
- str = dt.Format(strDateFormat);
- }
- break;
- case VT_CY: //Added by John Andy Johnson!!!
- {
- vtFld.ChangeType(VT_R8);
- CString str;
- str.Format(_T("%f"), vtFld.dblVal);
- _TCHAR pszFormattedNumber[64];
- // LOCALE_USER_DEFAULT
- if(GetCurrencyFormat(
- LOCALE_USER_DEFAULT, // locale for which string is to be formatted
- 0, // bit flag that controls the function's operation
- str, // pointer to input number string
- NULL, // pointer to a formatting information structure
- // NULL = machine default locale settings
- pszFormattedNumber, // pointer to output buffer
- 63)) // size of output buffer
- {
- str = pszFormattedNumber;
- }
- }
- break;
- case VT_BOOL:
- str = vtFld.boolVal == VARIANT_TRUE? 'T':'F';
- break;
- case VT_EMPTY:
- case VT_NULL:
- str.Empty();
- break;
- default:
- str.Empty();
- return FALSE;
- }
- strValue = str;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleDateTime& time)
- {
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- switch(vtFld.vt)
- {
- case VT_DATE:
- {
- COleDateTime dt(vtFld);
- 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 CADORecordset::GetFieldValue(int nIndex, COleDateTime& time)
- {
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- switch(vtFld.vt)
- {
- case VT_DATE:
- {
- COleDateTime dt(vtFld);
- 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 CADORecordset::GetFieldValue(LPCTSTR lpFieldName, bool& bValue)
- {
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- switch(vtFld.vt)
- {
- case VT_BOOL:
- bValue = vtFld.boolVal == TRUE? true: false;
- break;
- case VT_EMPTY:
- case VT_NULL:
- bValue = false;
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, bool& bValue)
- {
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- switch(vtFld.vt)
- {
- case VT_BOOL:
- bValue = vtFld.boolVal == TRUE? true: false;
- break;
- case VT_EMPTY:
- case VT_NULL:
- bValue = false;
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleCurrency& cyValue)
- {
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- switch(vtFld.vt)
- {
- case VT_CY:
- cyValue = (CURRENCY)vtFld.cyVal;
- break;
- case VT_EMPTY:
- case VT_NULL:
- {
- cyValue = COleCurrency();
- cyValue.m_status = COleCurrency::null;
- }
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, COleCurrency& cyValue)
- {
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- switch(vtFld.vt)
- {
- case VT_CY:
- cyValue = (CURRENCY)vtFld.cyVal;
- break;
- case VT_EMPTY:
- case VT_NULL:
- {
- cyValue = COleCurrency();
- cyValue.m_status = COleCurrency::null;
- }
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, _variant_t& vtValue)
- {
- try
- {
- vtValue = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::GetFieldValue(int nIndex, _variant_t& vtValue)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtValue = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::IsFieldNull(LPCTSTR lpFieldName)
- {
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- return vtFld.vt == VT_NULL;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::IsFieldNull(int nIndex)
- {
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- return vtFld.vt == VT_NULL;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::IsFieldEmpty(LPCTSTR lpFieldName)
- {
- _variant_t vtFld;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
- return vtFld.vt == VT_EMPTY || vtFld.vt == VT_NULL;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::IsFieldEmpty(int nIndex)
- {
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- try
- {
- vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
- return vtFld.vt == VT_EMPTY || vtFld.vt == VT_NULL;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::SetFieldEmpty(LPCTSTR lpFieldName)
- {
- _variant_t vtFld;
- vtFld.vt = VT_EMPTY;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldEmpty(int nIndex)
- {
- _variant_t vtFld;
- vtFld.vt = VT_EMPTY;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- DWORD CADORecordset::GetRecordCount()
- {
- DWORD nRows = 0;
- nRows = m_pRecordset->GetRecordCount();
- if(nRows == -1)
- {
- nRows = 0;
- if(m_pRecordset->EndOfFile != VARIANT_TRUE)
- m_pRecordset->MoveFirst();
- while(m_pRecordset->EndOfFile != VARIANT_TRUE)
- {
- nRows++;
- m_pRecordset->MoveNext();
- }
- if(nRows > 0)
- m_pRecordset->MoveFirst();
- }
- return nRows;
- }
- BOOL CADORecordset::IsOpen()
- {
- if(m_pRecordset != NULL && IsConnectionOpen())
- return m_pRecordset->GetState() != adStateClosed;
- return FALSE;
- }
- void CADORecordset::Close()
- {
- if(IsOpen())
- {
- if (m_nEditStatus != dbEditNone)
- CancelUpdate();
- m_pRecordset->PutSort(_T(""));
- m_pRecordset->Close();
- }
- }
- BOOL CADORecordset::RecordBinding(CADORecordBinding &pAdoRecordBinding)
- {
- HRESULT hr;
- m_pRecBinding = NULL;
- //Open the binding interface.
- if(FAILED(hr = m_pRecordset->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*)&m_pRecBinding )))
- {
- _com_issue_error(hr);
- return FALSE;
- }
- //Bind the recordset to class
- if(FAILED(hr = m_pRecBinding->BindToRecordset(&pAdoRecordBinding)))
- {
- _com_issue_error(hr);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CADORecordset::GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo* fldInfo)
- {
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- return GetFieldInfo(pField, fldInfo);
- }
- BOOL CADORecordset::GetFieldInfo(int nIndex, CADOFieldInfo* fldInfo)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- return GetFieldInfo(pField, fldInfo);
- }
- BOOL CADORecordset::GetFieldInfo(FieldPtr pField, CADOFieldInfo* fldInfo)
- {
- memset(fldInfo, 0, sizeof(CADOFieldInfo));
- _tcscpy_s(fldInfo->m_strName, (LPCTSTR)pField->GetName());
- fldInfo->m_lDefinedSize = pField->GetDefinedSize();
- fldInfo->m_nType = pField->GetType();
- fldInfo->m_lAttributes = pField->GetAttributes();
- if(!IsEof())
- fldInfo->m_lSize = pField->GetActualSize();
- return TRUE;
- }
- BOOL CADORecordset::GetChunk(LPCTSTR lpFieldName, CString& strValue)
- {
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- return GetChunk(pField, strValue);
- }
- BOOL CADORecordset::GetChunk(int nIndex, CString& strValue)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- return GetChunk(pField, strValue);
- }
- BOOL CADORecordset::GetChunk(FieldPtr pField, CString& strValue)
- {
- CString str = _T("");
- long lngSize, lngOffSet = 0;
- _variant_t varChunk;
- lngSize = pField->ActualSize;
- str.Empty();
- while(lngOffSet < lngSize)
- {
- try
- {
- varChunk = pField->GetChunk(ChunkSize);
- str += varChunk.bstrVal;
- lngOffSet += ChunkSize;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- lngOffSet = 0;
- strValue = str;
- return TRUE;
- }
- BOOL CADORecordset::GetChunk(LPCTSTR lpFieldName, LPVOID lpData)
- {
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- return GetChunk(pField, lpData);
- }
- BOOL CADORecordset::GetChunk(int nIndex, LPVOID lpData)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- return GetChunk(pField, lpData);
- }
- BOOL CADORecordset::GetChunk(FieldPtr pField, LPVOID lpData)
- {
- long lngSize;
- _variant_t varChunk;
- lngSize = pField->ActualSize;
- #if 0
- HRESULT hr;
- UCHAR chData;
- long lngOffSet = 0;
- long lBytesCopied = 0;
- while(lngOffSet < lngSize)
- {
- try
- {
- varChunk = pField->GetChunk(ChunkSize);
- //Copy the data only upto the Actual Size of Field.
- for(long lIndex = 0; lIndex <= (ChunkSize - 1); lIndex++)
- {
- hr= SafeArrayGetElement(varChunk.parray, &lIndex, &chData);
- if(SUCCEEDED(hr))
- {
- //Take BYTE by BYTE and advance Memory Location
- //hr = SafeArrayPutElement((SAFEARRAY FAR*)lpData, &lBytesCopied ,&chData);
- ((UCHAR*)lpData)[lBytesCopied] = chData;
- lBytesCopied++;
- }
- else
- {
- OutputDebugString(_T("获取安全元素值出错\n"));
- break;
- }
- }
- lngOffSet += ChunkSize;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- lngOffSet = 0;
- #else
- try
- {
- varChunk = pField->GetChunk(lngSize);
- if ( varChunk.vt == (VT_ARRAY | VT_UI1))
- {
- BYTE *pBuff = NULL;
- SafeArrayAccessData(varChunk.parray, (void**)&pBuff);
- memcpy(lpData, pBuff, lngSize);
- SafeArrayUnaccessData(varChunk.parray);
- }
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- #endif
- return TRUE;
- }
- LONG CADORecordset::GetChunkEx(LPCTSTR lpFieldName, HGLOBAL *hglobal)
- {
- BYTE* lpData = NULL;
- _variant_t varChunk;
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- long lngSize = pField->ActualSize;
- *hglobal = GlobalAlloc(GMEM_MOVEABLE, lngSize);
- if ( *hglobal == NULL )
- return 0;
- BYTE* pBuf = NULL;
- lpData = (BYTE*)GlobalLock(*hglobal);
- varChunk = pField->GetChunk(lngSize);
- if ( varChunk.vt == (VT_ARRAY | VT_UI1) )
- {
- SafeArrayAccessData(varChunk.parray, (void**)&pBuf);
- memcpy(lpData, pBuf, lngSize);
- SafeArrayUnaccessData(varChunk.parray);
- }
- return lngSize;
- }
- LONG CADORecordset::GetChunkEx(int nIndex, HGLOBAL *hglobal)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- BYTE* lpData = NULL;
- _variant_t varChunk;
- long lngSize = pField->ActualSize;
- *hglobal = GlobalAlloc(GMEM_MOVEABLE, lngSize);
- if ( *hglobal == NULL )
- return 0;
- BYTE* pBuf = NULL;
- lpData = (BYTE*)GlobalLock(*hglobal);
- varChunk = pField->GetChunk(lngSize);
- if ( varChunk.vt == (VT_ARRAY | VT_UI1) )
- {
- SafeArrayAccessData(varChunk.parray, (void**)&pBuf);
- memcpy(lpData, pBuf, lngSize);
- SafeArrayUnaccessData(varChunk.parray);
- }
- return lngSize;
- }
- LONG CADORecordset::GetChunkToFile(LPCTSTR lpFieldName, TCHAR* strFileName)
- {
- CFile getFile;
- CFileException e;
- BYTE *lpData = NULL;
- LONG lSize = 0;
- INT nRetCode = 0;
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- lSize = pField->ActualSize;
- if (lSize > 0)
- {
- DeleteFile(strFileName);
- if (getFile.Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &e)) // 打开了一个文件;
- {
- if (lpData = new BYTE[lSize + 1]) // 重新申请必要的存储空间;
- {
- nRetCode = GetChunk(pField, (LPVOID)lpData);
- if (nRetCode > 0)
- {
- getFile.Write(lpData, lSize);
- }
- if (lpData)
- delete[]lpData;
- lpData = NULL;
- }
- getFile.Close();
- }
- }
-
- return nRetCode;
- }
- LONG CADORecordset::GetChunkToFile(int nIndex, TCHAR* strFileName)
- {
- CFile getFile;
- CFileException e;
- BYTE *lpData = NULL;
- LONG lSize = 0;
- INT iRetCode = 0;
- if (getFile.Open(strFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &e))//打开了一个文件;
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- lSize = pField->ActualSize;
- if (lSize > 0)
- {
- if (lpData = new BYTE[lSize + 1]) //重新申请必要的存储空间;
- {
- iRetCode = GetChunk(pField, (LPVOID)lpData);
- if (iRetCode > 0)
- {
- getFile.Write(lpData, iRetCode);
- }
- delete [] lpData;
- lpData = 0;
- }
- }
- getFile.Close();
- }
- return iRetCode;
- }
- BOOL CADORecordset::AppendChunk(LPCTSTR lpFieldName, LPVOID lpData, UINT nBytes)
- {
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- return AppendChunk(pField, lpData, nBytes);
- }
- BOOL CADORecordset::AppendChunk(int nIndex, LPVOID lpData, UINT nBytes)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- return AppendChunk(pField, lpData, nBytes);
- }
- BOOL CADORecordset::AppendChunkFromFile(LPCTSTR lpFieldName, TCHAR* strFileName)
- {
- CFile appendFile;
- CFileException e;
- LONG lSize = 0;
- BYTE *lpData = NULL;
- BOOL bRetCode = FALSE;
- if (appendFile.Open(strFileName, CFile::modeRead|CFile::typeBinary, &e)) //打开了一个文件;
- {
- lSize = (LONG)appendFile.GetLength(); //先得到文件长度;
- lpData = new BYTE[lSize]; //按文件的大小在堆上申请一块内存;
- if (appendFile.Read(lpData, lSize) > 0) //把文件读到lpData(堆上申请一块内存);
- {
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- bRetCode = AppendChunk(pField, (LPVOID)lpData, lSize);
- }
- delete [] lpData;
- lpData = NULL;
- appendFile.Close();
- }
- return bRetCode;
- }
- BOOL CADORecordset::AppendChunkFromFile(int nIndex, TCHAR* strFileName)
- {
- CFile appendFile;
- CFileException e;
- LONG lSize = 0;
- BYTE *lpData = NULL;
- BOOL bRetCode = FALSE;
- if (appendFile.Open(strFileName, CFile::modeRead|CFile::typeBinary, &e)) //打开了一个文件;
- {
- lSize = (LONG)appendFile.GetLength(); //先得到文件长度;
- lpData = new BYTE[lSize]; //按文件的大小在堆上申请一块内存;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- FieldPtr pField = m_pRecordset->Fields->GetItem(vtIndex);
- if (appendFile.Read(lpData, lSize) > 0) //把文件读到lpData(堆上申请一块内存);
- {
- bRetCode = AppendChunk(pField, (LPVOID)lpData, lSize);
- }
- delete [] lpData;
- lpData = NULL;
- appendFile.Close();
- }
- return bRetCode;
- }
- BOOL CADORecordset::AppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
- {
- HRESULT hr;
- _variant_t varChunk;
- long lngOffset = 0;
- UCHAR chData;
- SAFEARRAY FAR *psa = NULL;
- SAFEARRAYBOUND rgsabound[1];
- try
- {
- //Create a safe array to store the array of BYTES
- rgsabound[0].lLbound = 0;
- rgsabound[0].cElements = nBytes;
- psa = SafeArrayCreate(VT_UI1,1,rgsabound);
- while(lngOffset < (long)nBytes)
- {
- chData = ((UCHAR*)lpData)[lngOffset];
- hr = SafeArrayPutElement(psa, &lngOffset, &chData);
- if(FAILED(hr))
- return FALSE;
- lngOffset++;
- }
- lngOffset = 0;
- //Assign the Safe array to a variant.
- varChunk.vt = VT_ARRAY|VT_UI1;
- varChunk.parray = psa;
- hr = pField->AppendChunk(varChunk);
- if(SUCCEEDED(hr))
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- return FALSE;
- }
- CString CADORecordset::GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull, long numRows)
- {
- _bstr_t varOutput;
- _bstr_t varNull(_T(""));
- _bstr_t varCols(_T("\t"));
- _bstr_t varRows(_T("\r"));
- if(_tcslen(lpCols) != 0)
- varCols = _bstr_t(lpCols);
- if(_tcslen(lpRows) != 0)
- varRows = _bstr_t(lpRows);
- if(numRows == 0)
- numRows =(long)GetRecordCount();
- varOutput = m_pRecordset->GetString(adClipString, numRows, varCols, varRows, varNull);
- return (LPCTSTR)varOutput;
- }
- void CADORecordset::Edit()
- {
- m_nEditStatus = dbEdit;
- }
- BOOL CADORecordset::AddNew()
- {
- m_nEditStatus = dbEditNone;
- if(m_pRecordset->AddNew() != S_OK)
- return FALSE;
- m_nEditStatus = dbEditNew;
- return TRUE;
- }
- BOOL CADORecordset::AddNew(CADORecordBinding &pAdoRecordBinding)
- {
- try
- {
- if(m_pRecBinding->AddNew(&pAdoRecordBinding) != S_OK)
- {
- return FALSE;
- }
- else
- {
- m_pRecBinding->Update(&pAdoRecordBinding);
- return TRUE;
- }
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::Update()
- {
- BOOL bret = TRUE;
- if(m_nEditStatus != dbEditNone)
- {
- try
- {
- if(m_pRecordset->Update() != S_OK)
- bret = FALSE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- bret = FALSE;
- }
- if(!bret)
- m_pRecordset->CancelUpdate();
- m_nEditStatus = dbEditNone;
- }
- return bret;
- }
- void CADORecordset::CancelUpdate()
- {
- m_pRecordset->CancelUpdate();
- m_nEditStatus = dbEditNone;
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, CString strValue)
- {
- _variant_t vtFld;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- if(!strValue.IsEmpty())
- vtFld.vt = VT_BSTR;
- else
- vtFld.vt = VT_NULL;
- vtFld.bstrVal = strValue.AllocSysString();
- BOOL bret = PutFieldValue(vtIndex, vtFld);
- SysFreeString(vtFld.bstrVal);
- return bret;
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, CString strValue)
- {
- _variant_t vtFld;
- if(!strValue.IsEmpty())
- vtFld.vt = VT_BSTR;
- else
- vtFld.vt = VT_NULL;
- vtFld.bstrVal = strValue.AllocSysString();
- BOOL bret = PutFieldValue(lpFieldName, vtFld);
- SysFreeString(vtFld.bstrVal);
- return bret;
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, int nValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_I2;
- vtFld.iVal = nValue;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, int nValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_I2;
- vtFld.iVal = nValue;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, long lValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_I4;
- vtFld.lVal = lValue;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, long lValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_I4;
- vtFld.lVal = lValue;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, unsigned long ulValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_UI4;
- vtFld.ulVal = ulValue;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, unsigned long ulValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_UI4;
- vtFld.ulVal = ulValue;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, double dblValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_R8;
- vtFld.dblVal = dblValue;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, double dblValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_R8;
- vtFld.dblVal = dblValue;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, COleDateTime time)
- {
- _variant_t vtFld;
- vtFld.vt = VT_DATE;
- vtFld.date = time;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleDateTime time)
- {
- _variant_t vtFld;
- vtFld.vt = VT_DATE;
- vtFld.date = time;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, bool bValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_BOOL;
- vtFld.boolVal = bValue;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, bool bValue)
- {
- _variant_t vtFld;
- vtFld.vt = VT_BOOL;
- vtFld.boolVal = bValue;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, COleCurrency cyValue)
- {
- if(cyValue.m_status == COleCurrency::invalid)
- return FALSE;
- _variant_t vtFld;
- vtFld.vt = VT_CY;
- vtFld.cyVal = cyValue.m_cur;
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleCurrency cyValue)
- {
- if(cyValue.m_status == COleCurrency::invalid)
- return FALSE;
- _variant_t vtFld;
- vtFld.vt = VT_CY;
- vtFld.cyVal = cyValue.m_cur;
- return PutFieldValue(lpFieldName, vtFld);
- }
- BOOL CADORecordset::SetFieldValue(int nIndex, _variant_t vtValue)
- {
- _variant_t vtIndex;
- vtIndex.vt = VT_I2;
- vtIndex.iVal = nIndex;
- return PutFieldValue(vtIndex, vtValue);
- }
- BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, _variant_t vtValue)
- {
- return PutFieldValue(lpFieldName, vtValue);
- }
- BOOL CADORecordset::SetBookmark()
- {
- if(m_varBookmark.vt != VT_EMPTY)
- {
- m_pRecordset->Bookmark = m_varBookmark;
- return TRUE;
- }
- return FALSE;
- }
- BOOL CADORecordset::Delete()
- {
- if(m_pRecordset->Delete(adAffectCurrent) != S_OK)
- return FALSE;
- if(m_pRecordset->Update() != S_OK)
- return FALSE;
- m_nEditStatus = dbEditNone;
- return TRUE;
- }
- BOOL CADORecordset::Find(LPCTSTR lpFind, int nSearchDirection)
- {
- m_strFind = lpFind;
- m_nSearchDirection = nSearchDirection;
- ASSERT(!m_strFind.IsEmpty());
- if(m_nSearchDirection == searchForward)
- {
- m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchForward, "");
- if(!IsEof())
- {
- m_varBookFind = m_pRecordset->Bookmark;
- return TRUE;
- }
- }
- else if(m_nSearchDirection == searchBackward)
- {
- m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchBackward, "");
- if(!IsBof())
- {
- m_varBookFind = m_pRecordset->Bookmark;
- return TRUE;
- }
- }
- else
- {
- TRACE("Unknown parameter. %d", nSearchDirection);
- m_nSearchDirection = searchForward;
- }
- return FALSE;
- }
- BOOL CADORecordset::FindFirst(LPCTSTR lpFind)
- {
- m_pRecordset->MoveFirst();
- return Find(lpFind);
- }
- BOOL CADORecordset::FindNext()
- {
- if(m_nSearchDirection == searchForward)
- {
- m_pRecordset->Find(_bstr_t(m_strFind), 1, adSearchForward, m_varBookFind);
- if(!IsEof())
- {
- m_varBookFind = m_pRecordset->Bookmark;
- return TRUE;
- }
- }
- else
- {
- m_pRecordset->Find(_bstr_t(m_strFind), 1, adSearchBackward, m_varBookFind);
- if(!IsBof())
- {
- m_varBookFind = m_pRecordset->Bookmark;
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CADORecordset::PutFieldValue(LPCTSTR lpFieldName, _variant_t vtFld)
- {
- if(m_nEditStatus == dbEditNone)
- return FALSE;
- try
- {
- m_pRecordset->Fields->GetItem(lpFieldName)->Value = vtFld;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::PutFieldValue(_variant_t vtIndex, _variant_t vtFld)
- {
- if(m_nEditStatus == dbEditNone)
- return FALSE;
- try
- {
- m_pRecordset->Fields->GetItem(vtIndex)->Value = vtFld;
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::Clone(CADORecordset &pRs)
- {
- try
- {
- pRs.m_pRecordset = m_pRecordset->Clone(adLockUnspecified);
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::SetFilter(LPCTSTR strFilter)
- {
- ASSERT(IsOpen());
- try
- {
- m_pRecordset->PutFilter(strFilter);
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::SetSort(LPCTSTR strCriteria)
- {
- ASSERT(IsOpen());
- try
- {
- m_pRecordset->PutSort(strCriteria);
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::SaveAsXML(LPCTSTR lpstrXMLFile)
- {
- HRESULT hr;
- ASSERT(IsOpen());
- try
- {
- hr = m_pRecordset->Save(lpstrXMLFile, adPersistXML);
- return hr == S_OK;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CADORecordset::OpenXML(LPCTSTR lpstrXMLFile)
- {
- HRESULT hr = S_OK;
- if(IsOpen())
- Close();
- try
- {
- hr = m_pRecordset->Open(lpstrXMLFile, "Provider=MSPersist;", adOpenForwardOnly, adLockOptimistic, adCmdFile);
- return hr == S_OK;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- BOOL CADORecordset::Execute(CADOCommand* pAdoCommand)
- {
- if(IsOpen())
- Close();
- ASSERT(!pAdoCommand->GetText().IsEmpty());
- try
- {
- m_pConnection->CursorLocation = adUseClient;
- m_pRecordset = pAdoCommand->GetCommand()->Execute(NULL, NULL, pAdoCommand->GetType());
- return TRUE;
- }
- catch(_com_error &e)
- {
- dump_com_error(e);
- return FALSE;
- }
- }
- void CADORecordset::dump_com_error(_com_error &e)
- {
- CString ErrorStr;
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- ErrorStr.Format(_T("CADORecordset 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 = _T("Query = " + GetQuery() + '\n' + ErrorStr);
- m_dwLastError = e.Error();
- #ifdef _DEBUG
- AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
- #endif
- }
|