Crecordset separately

zhaozj2021-02-17  57

Under normal circumstances, AppWizard automatically generates CRecordset derived classes in the database application, and linked the tables in a derived class and a data source can also be linked to the sub-window on the view. But sometimes do this will affect the flexibility of the program, and we can use the CRecordset class separately. With the CRecordset class, we can perform SQL statements and read data in the result set.

First we need to include the header file AFXDB.H, you can add #include to the stdafx.h file. In addition, there must be another CDATABASE object when using CRecordset, which is the management of the data source connection. A CRecordset object can then be generated, using Bool CRecordset :: Open (uint nopertype = AFX_DB_USE_DEFAULT_TYPE, LPCTSTSTSTSZSZSQL = NULL, DWORD DWOPTIONS = NONE) can execute SQL statements.

However, after the execution is successful, the following functions can be called scrolling the cursor and read the data.

MoveFirst Move the cursor to the first record Movenext Move the cursor to the latter MoveNext MovePrev Move the cursor to the previous record The MOVELAST Move the cursor to the last record at the ISBOF Detect the cursor on the first record ISEOF Detect the cursor in the last one Record GetFieldValue gets the data in the result

Below is specific code:

/ *

Suppose cdatabase m_dbconn is a member variable

Suppose there is a table having the following SQL statement generation: Create Table Table1 (LOC_ID NOT NULL)

* /

Void Cyourclass :: ConnectToDB ()

{//Connect to the database

BOOL fok = m_dbconn.open ("test");

Trace ("Connect Fok =% D / N", M_DBCONN);

}

Void cyorclass :: select ()

{/ Execution SELECT statement

CRecordset Rec (& M_DBCONN);

Bool fok = rec.open (crecordset :: forwardonly, "select loc_id from table1 order by loc_id");

Trace ("SELECT FOK =% D / N", FOK);

Trace ("Returns:% D / N", Rec.GetrowsetSize ());

CString SzResult;

While (! Rec.iseof ())

{

Rec.GetfieldValue (int) 0, SzResult);

Rec.movenext ();

Trace ("FETCH:% S / N", SzResult);

}

}

In addition, CRecordset :: getFieldValue has many prototypes, you can get data by specifying the list or field name:

Void getfieldValue (LPCTSTSTR LPSZNAME, CDBVARIANT & VARVALUE, Short NfieldType = Default_field_type);

Void getfieldValue (Short Nindex, Cdbvariant & Varvalue, Short NfieldType = Default_field_type);

Void getfieldValue (LPCTSTSTSTSTSZNAME (CSTRING & STRVALUE);

Void getfieldValue (Short Nindex, Cstring & Strval);

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

New Post(0)