Use CRecordSet separately

Source: Internet
Author: User


Generally, AppWizard automatically generates a CRecordset derived class in the database application, and associates the derived class with the table in a data source with the child window in the view. But sometimes this will affect the flexibility of the program. At this time, we can use the CRecordSet class separately. Using the CRecordSet class, we can execute SQL statements and read the 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, when using CRecordset, there must be another CDatabase object. This object is used to manage data source connections. Then, you can generate a CRecordset object and run the SQL statement using BOOL CRecordset: Open (UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE, LPCTSTR lpszSQL = NULL, DWORD dwOptions = none.

After successful execution, you can call the following function to scroll the cursor to read data.

MoveFirst Move the cursor to the first record
MoveNext Move the cursor to the next record
MovePrev Move the cursor to the previous record
MoveLast Move the cursor to the last record
IsBOF Checks whether the cursor is on the first record
IsEOF Checks whether the cursor is on the last record.
GetFieldValue Data in the result
The code below is as follows:
/ *

Assuming CDatabase m_dbConn is a member variable

Suppose there is a table with the following SQL statement: CREATE TABLE table1 (loc_id not null)

* /

void CYourClass :: ConnectToDB ()

{//Connect to the database

BOOL fOK = m_dbConn.Open ("test");

TRACE ("connect fOK =% d
", m_dbConn);

}



void CYourClass :: Select ()

{// Execute the 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
", fOK);

TRACE ("The number of columns returned is:% d
", rec.GetRowsetSize ());

CString szResult;

while (! rec.IsEOF ())

{

rec.GetFieldValue ((int) 0, szResult);

rec.MoveNext ();

TRACE ("fetch:% s
", szResult);

}

} 


In addition, CRecordset: GetFieldValue has many prototype types. You can obtain data by specifying the column position or field name:

Void GetFieldValue (LPCTSTRLpszName, CDBVariant &VarValue, ShortNFieldType= DEFAULT_FIELD_TYPE );

Void GetFieldValue (shortNIndex, CDBVariant &VarValue, ShortNFieldType= DEFAULT_FIELD_TYPE );

Void GetFieldValue (LPCTSTRLpszName, CString &StrValue);

Void GetFieldValue (shortNIndex, CString &StrValue);

If you use a variable of the CDBVariant type to obtain the result, you can get any type of result. The Data Types contained in the variable are recorded in the CDBVariant: m_dwType member variable. Based on the value of the variable, you can determine the data type and reference the corresponding member variables in the CDBVariant object.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.