Programming with ADO in Visual C ++ (below)

Source: Internet
Author: User
5. Record set traversal and update

According to the users table created by executing the SQL command just now, it contains four fields: ID, username, old, birthday
The following code enables you to open a record set, traverse all records, delete the first record, add three records, and move the cursor to the second record,
Change its age and save it to the database.
_ Variant_t vusername, vbirthday, vid, vold;
_ Recordsetptr m_precordset;
M_precordset.createinstance ("ADODB. recordset ");
M_precordset-> open ("select * from users ",
_ Variant_t (idispatch *) m_pconnection, true ),
Adopenstatic,
Adlockoptimistic,
Adcmdtext );
While (! M_precordset-> adoeof)
{
Vid = m_precordset-> getcollect (_ variant_t (long) 0); // obtain the value of the 1st column and count it from 0,
/// You can also directly give the column name, as shown in the following line
VUsername = m_pRecordset-> GetCollect ("username"); // obtain the value of the username field
VOld = m_pRecordset-> GetCollect ("old ");
VBirthday = m_pRecordset-> GetCollect ("birthday ");
/// Records in the OUTPUT window OUTPUT record set in DEBUG mode
If (vID. vt! = VT_NULL & vUsername. vt! = VT_NULL & vOld. vt! = VT_NULL & vBirthday. vt! = VT_NULL)
TRACE ("id: % d, name: % s, age: % d, birthday: % s/r/n ",
VID. lVal,
(LPCTSTR) (_ bstr_t) vUsername,
VOld. lVal,
(LPCTSTR) (_ bstr_t) vBirthday );
M_pRecordset-> MoveNext (); // move to the next record
}
M_pRecordset-> MoveFirst (); // move to the first record
M_pRecordset-> Delete (adAffectCurrent); // Delete the current record
/// Add three new records and assign values
For (int I = 0; I <3; I ++)
{
M_pRecordset-> AddNew (); // Add a new record
M_pRecordset-> PutCollect ("ID", _ variant_t (long) (I + 10 )));
M_pRecordset-> PutCollect ("username", _ variant_t ("Silverlight "));
M_pRecordset-> PutCollect ("old", _ variant_t (long) 71 ));
M_pRecordset-> PutCollect ("birthday", _ variant_t ("1930-3-15 "));
}
M_pRecordset-> Move (1, _ variant_t (long) adBookmarkFirst); // Move a record from the first record to the second record.
M_pRecordset-> PutCollect (_ variant_t ("old"), _ variant_t (long) 45); // modify the age
M_pRecordset-> Update (); // save it to the database.
Note: multiple queries can make the query process into a function executesql that allows m_precordset to obtain the query result of the connection pointer m_pconnection.
Void executesql (_ connectionptr m_pconnection, _ recordsetptr m_precordset, cstring strsql)
{
// Execute the SELECT statement
BSTR bstrsql = strsql. allocsysstring ();
Try
{
M_precordset-> open (bstrsql, (idispatch *) m_pconnection, adopendynamic, adlockoptimistic, ad1_text );
// Adopendynamic: Dynamic adlockoptimistic optimistic blocking method adshorttext: Text query statement
}
Catch (_ com_error error)
{
Cstring errormessage;
Errormessage. Format ("% s", (lptstr) error. Description ());
Afxmessagebox (errormessage );
}
}
// Error handling:
3127 -- the target table is not found.
3092 -- the target table already exists
For example:
Catch (const _ com_error E)
{
Afxmessagebox (E. Description ());
Long errorcode = E. wcode ();
If (3127 = errorcode) afxmessagebox ("the table does not exist ");
If (3092 = errorcode) afxmessagebox ("the table already exists ");
Return false;
}

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.