EXECUTE is executed, return a pointer to the record set, below we give specific code and instructions. _variant_t RecordsAffected; /// execute SQL commands: CREATE TABLE create table users, users contains four fields: plastic ID, string username, plastic old, date type birthdaym_pConnection-> Execute ( "CREATE TABLE users (ID INTEGER, username TEXT, Old Integer, Birthday DateTime) ", & Recordsaffected, AdcmdText); /// Add record M_PConnection-> Execute (" Insert Into Users (ID, Username, Old, Birthday) VALUES (1, '' '' ') 'Washington' '' '' '' ', 25,' '' '' '' '1970/1/1' '' '' '' ') ", & RecordsAffected, adCmdText); /// old records all fields The value is added to one m_pconnection-> execute ("Update Users Set Old = OLD 1", & RecordSaffected, AdcmdText); // / / executes the SQL statistical command to get record set M_PRecordset = m_pconnection-> Execute ("Select Count (*) From users "," & recordsaffected, adcmdtext); _VARIANT_T VINDEX = (long) 0; _variant_t vcount = m_precordset-> getCollect (VINDEX); /// get the value of the first field is placed in vcount variable m_precordset-> Close ); /// Close record set cstring message; "" a total of% D record ", vcount.lval); afXMESSAGEBOX (Message); /// Display the current record number
m_pCommand.CreateInstance ( "ADODB.Command");; (2) to SQL commands executed using the Command object _CommandPtr m_pCommand _ variant_t vNULL; vNULL.vt = VT_ERROR; vNULL.scode = DISP_E_PARAMNOTFOUND; /// no parameters defined m_pCommand-> ActiveConnection = m_pconnection; /// Very critical sentence, assign the established connection to it m_pcommand-> commandtext = "select * from users"; /// command string m_precordset = m_pcommand-> Execute (& Vnull, & vnull, adcmdtext) ; // / / execute command, get record set
In this code, we just use the Command object to perform the SELECT query statement, and the Command object can really reflect its role in the call to the stored procedure. Next time we will introduce it.
(3) Searching directly with the Recordset object, for example
m_pRecordset-> Open ( "SELECT * FROM users", _ variant_t ((IDispatch *) m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); Open prototype method is such: HRESULT Recordset15 :: Open (const _variant_t & Source, const _variant_t & ActiveConnection, enum CursorTypeEnum CursorType, enum LockTypeEnum LockType, long Options) wherein: ①Source ②ActiveConnection query string data is already established connections (connection object we need to construct a pointer to the object _variant_t) ③CursorType cursor type, it can It is one of the following values. Please see this enumeration structure: enum cursortypeenum {adopenunSpecified = -1, /// does not specify adopenforwardonly = 0, /// front rolled static cursor. This cursor can only browse the record in front, such as scrolling forward with MoveNext, this way can improve the browsing speed. But such as Bookmark, RecordCount, AbsolutePosition, AbsolutePosition, AbsolutePage can't use AdopenKeyset = 1, /////> Records that use this cursor can not see new, delete operations, but for updating the original records, you are visible to you of. AdoPENDYNAMIC = 2, // / Dynamic Cursor. The operation of all databases will be immediately reacted on each user recordset. AdopenStatic = 3 /// Static cursor. It produces a static backup for your recordset, but the new, delete, and update operations of other users are invisible to your record set. }; 4LockType lock type, it can be one of the following values, please see the following enumeration: enum locktypeenum {adlockunSpecified = -1, /// Non-specified AdlockReadOnly = 1, /// read-only record set AdlockPESSIMISTIC = 2, pessimistic Locking method. Data locks all other actions at the time of update, this is the safest lock mechanism AdlockOptimistic = 3, optimistic locking mode. Lock the record only when you call the UPDATE method. You can still do data update, insert, delete, etc. before this, AdlockBatchOptimistic = 4, optimistic batch update. When editing, the record does not lock, change, insert, and delete it is done in batch mode. }; 5Options Please refer to the introduction of the Execute method for the Connection object herein
[5] The traversal of the record set, update based on the UserS table we just created by executing the SQL command, it contains four fields: ID, username, old, birthday code implementation: Open the record set, traverse all records, delete A record, add three records, move the cursor to the second record, change its age, save 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) // This is AdoEOF instead of EOF? Remember Rename ("EOF", "AdoEOF") This sentence? {vid = m_precordset-> getCollect (_variant_t ((_VARIANT_T) LONG) 0)); // Narrows the value of the first column, start counting from 0, you can also give a list of columns, such as the next line vusername = m_precordset-> getCollect ("username"); /// get Username Field value VOLD = m_precordset-> getCollect ("old"); vbirthday = m_precordset-> getCollect ("birthday"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// The Output window output record IF in the DEBUG mode (VID.VT! = VT_NULL && vusername.vt! = vt_null && vold.vt! = vt_null && vbirdday.vt! = vt_null) Trace ("ID:% D, Name:% s, Age:% D, birthday:% s / r / n", Vid.lval, (lpctstr) (_ BSTR_T) VUSERNAME, VOLD.LVAL, (LPCTSTSTR); m_precordset-> MoveNext (); /// Move to Next Record} m_precordset-> MoveFirst (); // / Move to the first record m_precordset-> delete (adAffectCurrent); /// Remove the current record // Add three new records and assign the value for (int i = 0; i <3; i ) {m_precordset-> addnew (); /// Add new record m_precordset-> pu TCOLLECT, _ 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 record Move a record down, that is, move to the second record m_precordset-> Putcollect (_VARIANT_T ("OLD"), _ variant_t (long) 45)); // ()))); // Modify its age m_precordset-> Update (); // / Save to the library
[6] Closing the record set and connection record set or connection can use the Close method to turn off m_precordset-> close (); /// Close record set m_pconnection-> close (); /// Close connection to this, I think you have already Familiar with the approximate process of the ADO operation database, maybe you have already bamboo, maybe you still have a bit of Hu, don't be tight! It is recommended that you try to write a few examples, which will better familiarize yourself with ADO. Finally, I wrote a small example for everyone, and read all records in the list control, and can add, delete, and modify the record. Click here to download sample code
Postscript: Limited to many of the contents of the space ADO have not yet been introduced, next time we will introduce the property, method of the Recordset object, method and solve several critical technologies: binding mode handle record set data, stored procedure call, transaction processing, diagram Save and read in the database, use with the table control, etc.