CompareBookmarks 메서드 예제(VC++)
이 예제에서는 CompareBookmarks 메서드를 보여 줍니다. 특정 책갈피가 특별한 경우가 아니면 책갈피의 상대적 값은 거의 필요하지 않습니다.
Authors 테이블에서 파생된 레코드 집합의 임의 행을 검색 대상으로 지정합니다. 그런 다음, 해당 대상을 기준으로 각 행의 위치를 표시합니다.
// BeginCompareBookmarksCpp.cpp
// compile with: /EHsc
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include <ole2.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
// Function declarations
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
void CompareBookMarksX();
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
int main() {
if ( FAILED(::CoInitialize(NULL)) )
return -1;
CompareBookMarksX();
::CoUninitialize();
}
void CompareBookMarksX() {
HRESULT hr = S_OK;
_bstr_t strCnn("Provider='sqloledb'; Data Source='My_Data_Source'; Initial Catalog='pubs'; Integrated Security='SSPI';");
// Initialize pointers on define. These are in the ADODB:: namespace.
_RecordsetPtr pRstAuthors = NULL;
_variant_t vTarget;
_bstr_t strAns;
_bstr_t strTitle;
strTitle = "CompareBookmarks Example";
try {
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
pRstAuthors->Open("SELECT * FROM authors ORDER BY au_id", strCnn, adOpenStatic, adLockReadOnly, adCmdText);
long count = pRstAuthors->RecordCount;
printf("Rows in the Recordset = %d\n", count);
if (count == 0)
exit(1); // Exit if an empty recordset
srand( (unsigned)time( NULL ) ); // Randomize
count = int(rand() % (count-1)); // Get position between 1 and count-1
if (!count)
count++;
printf("Randomly chosen row position = %d\n", count);
_variant_t vtBookMark = (short)adBookmarkFirst;
pRstAuthors->Move(count,vtBookMark); // Move row to random position
vTarget = pRstAuthors->Bookmark; // Remember the mystery row.
count = 0;
pRstAuthors->MoveFirst();
while (!(pRstAuthors->EndOfFile)) { // Loop through recordset
long result = pRstAuthors->CompareBookmarks(pRstAuthors->Bookmark, vTarget);
if (result == adCompareNotEqual)
printf("Row %d: Bookmarks are not equal. %d\n", count, result);
else if (result == adCompareNotComparable)
printf("Row %d: Bookmarks are not comparable.\n", count);
else {
switch(result) {
case adCompareLessThan:
strAns = "less than";
break;
case adCompareEqual:
strAns = "equal to";
break;
case adCompareGreaterThan:
strAns = "greater than";
break;
default:
strAns = "in error comparing to";
break;
}
printf ("Row position %d is %s the target.\n",
count,(LPCSTR)strAns);
}
count++;
pRstAuthors->MoveNext();
}
}
catch(_com_error &e) {
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstAuthors->GetActiveConnection();
// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt) {
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occurred.");
break;
}
}
// Clean up objects before exit.
if (pRstAuthors)
if (pRstAuthors->State == adStateOpen)
pRstAuthors->Close();
}
void PrintProviderError(_ConnectionPtr pConnection) {
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
if ( (pConnection->Errors->Count) > 0) {
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for (long i = 0 ; i < nCount ; i++) {
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number, pErr->Description);
}
}
}
void PrintComError(_com_error &e) {
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}