12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952 |
- #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;
- DeleteFile(strFileName);
- if ( getFile.Open(strFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &e) ) // 打开了一个文件;
- {
- FieldPtr pField = m_pRecordset->Fields->GetItem(lpFieldName);
- lSize = pField->ActualSize;
-
- if (lSize > 0)
- {
- 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
- }
|