Database programming in Visual C ++ (below)

xiaoxiao2021-03-06  53

Database programming (below) 5. Record the traversal, update

According to the use of the UserS table we just entered by executing the SQL command, it contains four fields: ID, username, old, birthday code implementation: open record set, traverse all records, delete the first record, add three records, move The cursor to the second record, change its age, 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))))))); // // get the value of the first column, start counting from 0, // / You can also The name of the column, such as the next line vusername = m_precordset-> getCollect ("username"); // Once the value of the username field VOLD = m_precordset-> getCollect ("OLD"); vbirthday = m_precordset-> getCollect ("birthday" ); /// In the output of the OUTPUT window in the DEBUG mode, the record set in the record set (VID.VT! = VT_NULL & VUSERNAME.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 record and assignment for (int i = 0; i <3; i ) {m_precordset-> addnew (); /// add new record m_ PrecordSet-> Putcollect ("ID", _ variant_t ((i 10))); m_precordset-> Putcollect ("UserName", _ variant_t ("Yeltsin")); 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); /// from the first A record moves down to move a record, that is, moved to the second record m_precordset-> Putcollect (_VARIANT_T ("OLD"), _ variant_t (long) 45)); // / / modify its age m_precordset-> Update () ; // Save to the library Note:

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

New Post(0)