Bof, EOF, And Bookmark Properties Example (VC ++)

xiaoxiao2021-03-06  41

MDAC 2.5 SDK - ADO

Bof, EOF, And Bookmark Properties Example (VC )

The first function in this example uses the BOF and EOF properties to display a message if a user tries to move past the first or last record of a Recordset. It uses the Bookmark property to let the user flag a record in a Recordset and return to IT LATER.

...................... ..

#import "c: / program files / compon files / system / ado / msado15.dll" /

NO_NAMESPACE RENAME ("EOF", "endoffile")

#include

#include

#include

#include "bofbeofbookmark.h"

// Function Declarations

Inline void testhr (hResult x) {if failed (x) _com_issue_error (x);

Void Bofx (Void);

Void Bookmarkx (Void);

Void PrintProvideRrror (_ConnectionPtr PConnection);

Void PrintComerror (_COM_ERROR & E);

///

///

// bofx function ////

///

///

void main ()

{

IF (Failed (:: Coinitialize (null)))

Return;

Bofx ();

// clear the screen for the next dispial

SYSTEM ("CLS");

Bookmarkx ();

Printf ("Press Any Key To Continue ...");

Getch ();

:: Couninitialize ();

}

///

///

// bofx function ////

///

///

Void Bofx (Void)

{

// define ado object Pointers.

// Initialize Pointers on Define.

// Thase Are In The AdoDB :: Namespace

_RecordSetPtr Rstpublishers = null; // define Other Variables

HRESULT HR = S_OK;

Iadorecordbinding * picrs = null; // interface Pointer Declared.

CPBLISHERS PUBLS;

BSTR_T STRCNN ("provider = SQLOLEDB; DATA SOURCE = SRV;"

"InTIAL CATALOG = PUBS; User ID = SA; Password =;");

_BSTR_T STRMESSAGE;

_variant_t varbookmark;

INT INTCOMMAND = 0;

_VARIANT_T TEMPPUBLISHER;

Try

{

// Open RecordSet with data from Publishers Table.

TESTHR (Rstpublishers.createInstance));

Rstpublishers-> cursortype = adopenStatic;

// Use client cursor to enable AbsolutePosition Property.

Rstpublishers-> CursorLocation = aduseclient;

Rstpublishers-> Open ("SELECT PUB_ID, PUB_NAME FROM PUBLISHERS"

"Order by Pub_Name", Strcnn, AdopenStatic,

Adlockbatchoptimistic, adcmdtext);

// Open an iporecordbinding interface Pointer

// Which Will BE Used for Binding Recordset to a class

TESTHR (Rstpublishers-> QueryInterface

__UUIDOF (Iadorecordbinding), (LPVOID *) & PICRS)))));

// bind the recordset to a c class here

TESTHR (Picrs-> Bindtorecordset);

Rstpublishers-> MoveFirst ();

While (True) // Continuous Loop.

{

// Display Information About The Current Record? // DISPLAY INFORMATION

// and get User INPUT

Printf ("Publisher:% S / N Record% D OF% D / N / N",

PUBLS.LP_PUBNAMESTATUS == ADFLDOK?

PUBLS.M_SZP_PUBNAME: "",

Rstpublishers-> AbsolutePosition,

Rstpublishers-> RecordCount);

Printf ("Enter Command: / N");

Printf ("[1 - next / 2 - previous // n");

Printf ("3 - Set Bookmark / 4 - Go to Bookmark // N");

Printf ("5 - quit] / n");

Scanf ("% D", & intcommand;

IF (IntCommand <1) || (intcommand> 4)))

Break; // out of range entry exits program loop.

Switch (int Commommand)

{

//Move forward or backward, trapping for bof or eof

Case 1:

RSTPUBLISHERS-> MOVENEXT ();

IF (Rstpublishers-> endoffile)

{

Printf ("/ NCANNOT MOVE PAST The Last Record."

"TRY AGAIN ... / N");

Rstpublishers-> movelast ();

}

Break;

Case 2:

Rstpublishers-> movPrevious ();

IF (Rstpublishers-> BOF)

{

Printf ("/ NCANNOT MOVE BEFORE THE FIRST RECORD."

"TRY AGAIN ... / N");

Rstpublishers-> MoveFirst ();

}

Break;

// Store The Bookmark of The Current Record.

Case 3:

Varbookmark = RSTPUBLISHERS-> BOOKMARK;

// go to the record indeicated by the

// stored bookmark

Break;

Case 4:

// Check for WHETHER BOOKMARK SET for A Record

IF (varbookmark.vt == vt_empty)

Printf ("NO Bookmark Set! / N");

Else

Rstpublishers-> Bookmark = varbookmark;

Break;

DEFAULT:

Break;

}

}

// Clean Up Objects Before Exit.

Rstpublishers-> close ();

// Release The Iadorecordset Interface Here

IF (PICRS)

Picrs-> Release ();

}

Catch (_COM_ERROR & E)

{

Printf ("Error In Bofx ... / N");

// Notify the user of errors if any.

_VARIANT_T VTCONNECT = RSTPUBLISHERS-> getActiveConnection ();

// getActiveConnection Returns Connect Stringiffiffiffiffiff IF Connection

// is Not Open, Else Returns Connection Object.

Switch (vtconnect.vt)

{

Case VT_BSTR:

Printf ("Error: / N");

Printf ("CODE =% 08LX / N", E.Error ());

Printf ("Message =% S / N", E.ERRORMESSAGE ());

Printf ("Source =% S / N", (LPCSTR) E.Source ());

Printf ("Description =% S / N", (LPCSTR) E.DESCRIPTION ());

Break; Case vt_dispatch:

PRINTPROVIDERROR (VTCONNECT);

Break;

DEFAULT:

Printf ("ERRORS OCCURED.");

Break;

}

Printf ("Press Any Key To Continue ...");

Getch ();

}

}

///

///

// Bookmarkx Function //

///

///

Void Bookmarkx (Void)

{

// define ado object Pointers.

// Initialize Pointers on Define.

// There Are IN The AdoDB :: Namespace.

_RecordSetPtr Rstauthors = NULL;

// define other variables

Iadorecordbinding * picrs = null; // interface Pointer Declared.

Cauthors Authrs;

HRESULT HR = S_OK;

_BSTR_T STRCNN ("provider = SQLOLEDB; DATA SOURCE = SRV;"

"InTIAL CATALOG = PUBS; User ID = SA; Password =;");

_variant_t vbookmark;

// Variable Declaration for Safe Arrays.

SafeArray far * PSA;

// define array / variant variant.

Vbookmark.vt = vt_Array | vt_variant;

SafeArraybound Rgsabound [1];

Rgsabound [0] .llbound = 0;

Rgsabound [0] .CELEMENTS = 11;

Long ii = 0;

Try

{

RSTAUTHORS.CREATEINSTANCE (__ uuidof (recordset);

// set the cursor location

RSTAUTHORS-> CURSORLOCATION = ADUSECLIENT;

RSTAUTHORS-> PutactiveConnection ((_ variant_t) strcnn;

// Open Authors Table

Testhr (Rstauthors-> Open ("Select * from authors", strcnn,

AdoPenStatic, AdlockBatchOptimistic, AdcmdText);

// Open an iporecordbinding interface Pointer

// Which we'll Use for binding recordset to a class

Testhr (Rstauthors-> queryinterface (__ uuidof (iporecordbinding),

(Lpvoid *) & picrs)));

// bind the recordset to a c class here

TESTHR (Picrs-> BindtorecordSet);

Printf ("Number of Records Before Filtering:% D / N", RSTAUTHORS-> RecordCount);

// Create SafeArrays to Store Array of Variant

PSA = SafeArrayCreate (VT_VARIANT, 1, RGSABOUND);

// Store Bookmark of Every Other Record Into An Array.

While ((! Rstauthors-> Endoffile) && (ii <11))

{

SafearrayPutelement (PSA, & II, & RSTAUTHORS-> BOOKMARK);

// II = II 1;

II ;

RSTAUTHORS-> Move (2);

}

Vbookmark.parray = PSA;

// filter the record with the array of bookmarks.

RSTAUTHORS-> PUT_FILTER (Vbookmark);

Printf ("Number of Records After Filtering:% D / N",

RSTAUTHORS-> RecordCount);

RSTAUTHORS-> MOVEFIRST ();

While (! RSTAUTHORS-> endoffile)

{

Printf ("% D% S / N", Rstauthors-> AbsolutePosition,

Authrs.lau_lnamestatus == Adfldok?

Authrs.m_szau_lname: "");

RSTAUTHORS-> MOVENEXT ();

}

// Release the adorecordset interface.

IF (PICRS)

Picrs-> Release ();

// Clean Up Objects Before Exit.

RSTAUTHORS-> Close ();

}

Catch (_COM_ERROR & E)

{

// Notify the user of errors if any.

_VARIANT_T VTCONNECT = RSTAUTHORS-> getActiveConnection ();

// getActiveConnection Returns Connect Stringiffiffiffiffiff IF Connection

// is Not Open, Else Returns Connection Object.

Switch (vtconnect.vt)

{

Case VT_BSTR:

Printf ("Error: / N");

Printf ("CODE =% 08LX / N", E.Error ());

Printf ("Message =% S / N", E.ERRORMESSAGE ());

Printf ("Source =% S / N", (LPCSTR) E.Source ());

Printf ("Description =% S / N", (LPCSTR) E.DESCRIPTION ());

Break;

Case vt_dispatch:

PRINTPROVIDERROR (VTCONNECT);

Break;

DEFAULT:

Printf ("ERRORS OCCURED.");

Break;

}

}

}

///

// ///printprovidererror function ////

///

///

Void PrintProvideRRROR (_ConnectionPtr PConnection)

{

// Print Provider Errors from Connection Object.

// Perr is a record object in the connection's error.

Errorptr Perr = NULL;

Long ncount = 0;

LONG i = 0;

IF ((PConnection-> Errors-> count)> 0)

{

Ncount = pConnection-> Errors-> count;

// Collection Ranges from 0 to ncount -1.

For (i = 0; i

{

Perr = pConnection-> Errors-> GetItem (i);

Printf ("Error Number:% X / T% S / N", Perr-> Number,

(Lpcstr) Perr-> description);

}

}

}

BofeofBookmark.h:

#include "icrsint.h"

// this class extracts only pubid, lastname and hire_date

Class Cpublishers: Public Cadorecordbinding

{

Begin_ado_binding (cpublishers)

// column title is the 2nd field in the recordset

ADO_VARIABLE_LENGTH_ENTRY2 (1, Advarchar, M_SZP_PUBID,

SizeOf (M_SZP_PUBID), LP_PUBIDSTATUS, FALSE

// column type is the 3rd field in the recordset

ADO_VARIABLE_LENGTH_ENTRY2 (2, Advarchar, M_SZP_Pubname,

SizeOf (M_SZP_Pubname), LP_Pubnamestatus, TRUE

END_ADO_BINDING ()

PUBLIC:

CHAR M_SZP_PUBID;

Ulong LP_PBIDSTATUS;

CHAR M_SZP_PUBNAME [40];

Ulong lp_pubnamestatus;

}

// this class extracts only authorlastname

Class Cauthors: Public Cadorecordbinding

{

Begin_ado_binding (Cauthors)

// column authorlname is the 2nd field in the recordset

ADO_VARIABLE_LENGTH_ENTRY2 (2, Advarchar, M_Szau_lname,

SizeOf (m_szau_lname), Lau_lnamestatus, False

END_ADO_BINDING ()

PUBLIC:

Char m_szau_lname [40];

Ulong lau_lnamestatus;};

转载请注明原文地址:https://www.9cbs.com/read-78163.html

New Post(0)