Active Data Object in Visual C ++ (ADO) ZHUAN

xiaoxiao2021-03-05  24

Screen capture of the program: Summary: This paper briefly introduces Microsoft's Activity Data Object (ADO) model, combined with instances to use the ADO manipulation database in the Visual C environment, analyze the characteristics of ADO and open database connections (ODBC) Differences and application prospects. Left: ADO Object Model Keywords: Active Data Object Database Visual C 1 ADO is a component of the Microsoft's entire COM strategy system (ADO) is a set of COM components provided by Microsoft. ADO is built on the COM architecture advocated by Microsoft, and all of its interfaces are automated interfaces, so the ADO can be accessed through the interface in the development language of C , VisualBasic, Delphi and other support COM. ADO implements data that can access relational databases, text files, non-relational databases, index servers, and active directory services, etc. in the same way by using OLE DB, and expand the range of data sources used in applications, thus The preferred choice for accessing the data source components in the entire COM strategy system is an alternative product of ODBC. 2 ADO object model Compared to Microsoft's other data access models DAO and RDO, the ADO object model is very refined, consisting of three main objects Connection, Command, Recordset, and several auxiliary objects, which are shown in the figure. The Connection object provides the association between the OLE DB data source and the dialog object, which handles the authentication of the user's identity through the user name and password, and provides support for transaction processing; it also provides execution method to simplify the connection and data search of the data source. Process. The Command object encapsulates the data source that can be interpreted, which can be any content that the SQL command, the stored procedure, or the underlying data source can be understood. Record Set is used to represent the table data returned from the data source, which encapsulates the navigation, record update, record deletion, and new records of the record collection, and provides a bulk update record. Other auxiliary objects provide columns from encapsulation ADO errors, package command parameters, and packaging records. 3 ADO Characteristics (1) Since many underlying work is encapsulated, ADO is used almost as convenient to use ODBC. (2) ADO is not only the main function of ODBC, but also has a large number of data sources for ADO applicable. (3) When defining the ADO recordset and the database table field binding class, the field variables, status variables are required, and the order must be the same. This is more complex than using ODBC in FMC. However, in the macro bound to the Database field and the ADO record set field variable, the data type provided by the ADO is much more than the RFX in the FMC (such as the datetime type, which can only be converted to a CString type) in the ODBC). (4) ADO allows multiple Record Set instances under the same Connection instance. (5) ADO allows for batch update (Update Batch method), which will greatly reduce the network burden and improve database processing efficiency. 4 ADO uses the use of Microsoft to operate the SQL Server database using the ADO interface in Visual C with ADO2 provided in MicrSoft Studio 6. Using ADO in compilation advanced languages, it is difficult to use ADO in some scripting languages ​​(such as Visual Basic Scropt and JavaScript).

The basic steps of using ADO's Connection objects and its Record Set objects are given under a Visual C : (1) Introducing Ado2 Components: #import "c: adomsado15.dll" NO_NAMESPACE RENAME ("EOF", "ENDOFFILE ") (2) Define the derived class of CADORECORDBINDING, used for the interaction of the program and database table field, which can be found in ICRSINT.H. Example: Class CintLive: Public Cadorecordbinding {public: dbtimestamp m_datetime; // Defines the ADO record set field variable (corresponding to the database table field) long m_key; long m_value; long m_quality; word m_stsdatetime; // Define ADO record set status Variable Word m_stskey; WORD m_stsvalue; WORD m_stsquality; BEGIN_ADO_BINDING (CIntlive) // record database fields set ADO field variable bindings ADO_VARIABLE_LENGTH_ENTRY2 (1, adDBTimeStamp, m_datetime, sizeof (m_datetime), m_stsdatetime, true) ADO_NUMERIC_ENTRY (2, adInteger, m_key, 10,0, m_stskey, true) ADO_NUMERIC_ENTRY (3, adInteger, m_value, 10,0, m_stsvalue, true) ADO_NUMERIC_ENTRY (4, adInteger, m_quality, 10,0, m_stsquality, true) END_ADO_BINDING ()}; (3) call CoInitialize Initialization COM :: Coinitialize (NULL); (4) Declares the ADO's Connection object pointer and the Recordset object pointer and initialize. (Type name is defined in msado15.dll) example: _ConnectionPtr PConnection1 = null; _recordsetptr rstado1 = null; (5) Define instances of Cadorecordbinding derived class and its Bind interface pointer. Example: cintlive m_intdata; iporecordbinding * RstadObind1 = null; (6) Generates an instance of the Connection object instance and the Record Set object. Example: PConnection1.createInstance (_UUIDOF (Connection)); RSTADO1.CREATEINSTANCE (__ uuidof (recordset)); (7) Connect to the database and open the Record set object, where the Open function's parameters can be found in Microsoft MSDN ADO corresponding objects Basic description of the parameters.

Example: PConnection1-> Open ("driver = {SQL Server}; server = servera; uid = sa; pwd =; database = pubs", ",", "" rsTado1-> open ("data", _variant_t (IDispatch *) PConnection1, true, adopenkeyset, advicebatchoptimistic, adcmdtable; (8) Corrupted the instance of the CADORECORDBINDING to the Bind interface of the Record Set object. Example: Rstadobind1-> BindtorecordSet; (9) Operation of the Record SET object instance. The operation method can see the Basic description of the ADO Record set object accordingly in Microsoft MSDN. Example: RSTADO1-> Move next (); // Mobile Clearance to the next Record Rstado1-> Update (_VARIANT_T ("Quality"), _ variant_t ("3"))); // Modify the value of recorded Quality fields of 3 RSTADO1-> Update Batch (Adaffect Al); // The update of all updates on the Record Set object is sent to the database (10) to close the Record Set object and release the Bind interface. Example: RSTADO1-> Close (); RSTADOBIND2-> Release (); (11) Close connection PConnection1-> Close (); (12) call Counitialize Release COM resources :: Couninitialize (); 5 Conclusions As an alternative product of ODBC, ADO does have some people. Since ADO data sources have almost covered currently common data source types, ADO is undoubtedly unique for the data source that ODBC is not supported. And ADO's batch update features are important factors for large data volume update applications under the network environment. Since ADO lacks support from a large number of third-party manufacturers, ADO is far less like ODBC, but its object-oriented features will make ADOs have broad development prospects. ADO (ActiveX Data Object) is a new interface developed by Microsoft Database. It is a high-level database access technology built on OLE DB. Please don't have to worry about it. Even if you have Ole DB, COM can easily deal with ADO, Because it is very easy to use, even the ODBC APIs, DAOs, RDO you have contacted in the past, and it is not easy to use. This article will detail how to use ADO to develop database applications in VC, and give sample code. This example code is two, the basic process is difficult, and any new technique is the most important thing for beginners to "get started" and master it. Let's take a look at the basic procedures developed by the ADO database! (1) Initialize the COM library, introduce the ADO library definition file (2) Connect the database (3) with the Connection object to use the established connection, execute the SQL command via the Connection, Command object, or use the Recordset object to obtain the results record set for querying, processing . (4) Turn off the connection release object after use. Preparation: For everyone to test the examples provided herein, we use the Access database, you can also find this Test.mdb directly in the sample code we provide. Below we will introduce the above steps and give relevant code.

[1] Initialization of the COM library We can use AFXoleinit () to initialize the COM library. This work is usually done in the overload function of cwinapp :: initInstance (). Please see the following code: BOOL Cadotest1app :: InitInstance () {Afxoleinit (); ... [2] To introduce the ADO type library with a #import instruction We add the following statement in stdafx.h: (STDAFX.H Where can this file I can find? You can find it in the Header Files in FileView #import "c: / program files / common files / system / ad / msado15.dll" no_namespace rename ("EOF", "AdoEOF") What is the role of this statement? Its ultimate role is similar to our familiar #include. When compiling, the system will generate msado15.tlh, ado15.tli two C header files to define the ADO library. Some descriptions: (1) Msado15.dll in your environment is not necessarily in this directory, please modify (2) When compiling, it will appear as follows, and the Microsoft is in MSDN. And it is recommended that we don't pay attention to this warning. msado15.tlh (405): warning C4146: unary minus operator applied to unsigned type, result still unsigned 【3】 create a Connection object and connect to the database we need to add a reference to the Connection object pointer: _ConnectionPtr m_pConnection; The following code shows how Create a Connection object instance and how to connect the database and perform an exception capture. BOOL CADOTest1Dlg :: OnInitDialog () {CDialog :: OnInitDialog (); HRESULT hr; try {hr = m_pConnection.CreateInstance ( "ADODB.Connection"); /// create Connection objects if (SUCCEEDED (hr)) {hr = m_pConnection -> Open ("provider = microsoft.jet.Oledb.4.0; data source = test.mdb", ",", ", admodeunknown); //// Connect the database // The top of the connection string in the connection string is For Access2000 environments, for Access97, it is necessary to change to: provider = microsoft.jet.Oledb.3.51;}} catch (_COM_ERROR E) /// capture exception {cString ErrorMessage; errorMAGE.Format ("Connection database failed! / R / n Error Information:% S ", E.ErrorMessage (); AFXMessageBox; /// Display Error Information} In this code we are connecting the database through the open method of the Connection object, the following is the method Prototype HRESULT Connection15 :: Open (_bstr_t userid, _bstr_t password, long option "Connectionstring is a connection string, userid is a username, Password is a login password, Options is a connection option, which is used to specify the Connection object to update the data. Permissions, Options can be as follows: AdmodeunkNown: Default.

The current license is not adModeRead settings: read-only adModeWrite: write only adModeReadWrite: can read and write adModeShareDenyRead: Connection object to prevent other read permissions to open a connection adModeShareDenyWrite: Connection object to prevent other write permissions to open a connection adModeShareExclusive: Connection object to prevent other open connection adModeShareDenyNone : Allow other programs or objects to establish a connection to any permissions We give some common connection methods for your reference: (1) Connection M_PConnection-> Open ("provcer = microsoft.jet.OleDb.4.0 via Jet database engine for Access2000 database Data Source = C: //test.mdb ",", "", admodeunknown); (2) Connecting any database supporting ODBC's database via DSN data source: m_pConnection-> open ("data source = adotest; uid = SA; PWD =; "," "," ", admodeunknown; (3) Do not connect to the SQL Server database via DSN: m_pConnection-> Open (" driver = {sqlserver}; server = 127.0.0.1; database = Vckbase; uid = sa; pwd = 139 ",", "," ", where Server is the name of the SQL server, Database is the name of the library. The connection object has many ways, and let's introduce the Connection object first. Two useful properties connect Timeout and State ConnectionTIMEOUT are used to set the timeout of the connection, you need to call before the Open, for example: m_pconnection-> connectionTimeout = 5; /// set timeout time to 5 second m_pconnection-> open ("data source = Adotest; ",", "", admodeunknown; state attribute indicates the status of the current Connection object, 0 indicates that it is turned off, 1 indicates that it is already open, for example, if (m_pconnection- > State) M_ PConnection-> Close (); // If the connection has been opened, turn off it [4] Execute the SQL command and obtain the result record set to obtain the result record set, we define a pointer to the RecordSet object: _RecordSetPtr m_Precordset; Create an instance of a RecordSet object: m_precordset.createInstance ("AdoDb.Recordset"); the execution of the SQL command can take a variety of forms, let's explain it. (1) using the Connection object's Execute method executes the SQL command prototype Execute method is as follows: _RecordsetPtr Connection15 :: Execute (_bstr_t CommandText, VARIANT * RecordsAffected, long Options) wherein CommandText is a command string, usually SQL commands.

Parameter recordsaffected is the number of rows affected after the operation is completed. The parameter Options represents the type of content in CommandText. Options can take one of the values: AdcmdText: Indications CommandText is a text command adcmdtable: indicate that CommandText is a table name AdcmdProc: indicating that CommandText is a table name AdcmdProc: Stored Procedure AdcmDunkNown: Unknown Execute End Returns a pointer to record sets, 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 birthday m_pConnection-> Execute ( "CREATE TABLE users (ID INTEGER, username TEXT , old INTEGER, birthday DATETIME) ", & RecordsAffected, adCmdText); /// Add a record to a table inside m_pConnection-> Execute (" INSERT iNTO users (ID, username, old, birthday) VALUES (1, 'Washington', 25, '1970/1/1') ", & recordsaffected, adcmdtext); // /" d onn n ion> ("Update Users Set Old = OLD 1", & Recordsaffected, AdcmdText); // / execute SQL command to get statistical record set comprising a number of records m_pRecordset = m_pConnection-> execute ( "SELECT COUNT (*) FROM users", & RecordsAffected, adCmdText); _variant_t vIndex = (long) 0; _variant_t vCount = _pRecordset-> GetCollect (vindex); /// get the value of the first field is placed in a vcount variable m_precordset-> close (); /// Close record set cstring message; message.format ("a total of% D record", vcount.lval) AFXMessageBox (Message); /// Displays the current record number (2) Executive SQL command _commandptr m_pcommand; m_pcommand.createInstance ("adoDb.command"); _variant_t vnull; vnull.vt = vt_error; vnull. Scode = DISP_E _ParamNotFound; // defined as a parameter m_pcommand-> activeConnection = m_pconnection; /// Very critical sentence, the established connection assignment 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 Command objects to execute the SELECT query statement, and Command objects can be truly in the call. Reflecting its role. Next time we will introduce it.

Method step / ************************************************************************************************************** ****************** / ADO library contains three basic interfaces: _ConnectionPTR interface, _commandptr interface and _RecordSetPTR interface. _ConnectionPTR interface returns a recordset or an empty pointer. It is usually used to create a data connection or perform a SQL statement that does not return any result, such as a stored procedure. Returning a recordset using the _ConnectionPTR interface is not a good method. Usually, like CDATABASE, use it to create a data connection, then use other objects to perform data input output operations. _CommandPTR interface returns a recordset. It provides a simple way to perform the stored procedures and SQL statements that returns the record set. When using the _CommandPTR interface, you can use the global _connectionptr interface, or you can use the connection string directly in the _commandptr interface. If you only perform one or more data access, the latter is a better choice. But if you want to access the database frequently and return a lot of records, you should use the global _connectionptr interface to create a data connection, then use the _CommandPTR interface to perform the stored procedure and SQL statements. _RecordSetPtr is a recordset object. Compared with the above two objects, it provides more control functions to records, such as record lock, cursor control, and the like. Like the _CommandPTR interface, it does not have to use a data connection that has been created, you can use a connection string instead of the Connection member variable assigned to _RecordSetptr, let it create data connections yourself. If you want to use multiple record sets, the best way is to use the Command object to use the global _connectionptr interface that has created a data connection, and then use the _recordsetPTR to perform the stored procedure and SQL statements. / *************** Basic process ********************************* *********** / (1) Initialize the COM library, introduce the ADO library definition file (2) Connect the database (3) with the Connection object to use the established connection, execute the SQL command through the connection, command object, Or use the RECORDSET object to obtain the results record for query, process. (4) Turn off the connection release object after use. / *********************** [1] Initialization of COM library ******************* *************** / We can use AFXoleinit () to initialize the COM library, this work is usually done in the overload function of cwinapp :: initInstance (), please see the following code : Bool Cadotest1app :: InitInstance () {AFXOLINIT (); ......} / **************** [2] Use #import instruction to introduce ADO type library * ****************** We add as follows: #import "C: / Program Files / Common Files / System / ADO / MSADO15 in stdafx.h. DLL "NO_NAMESPACE RENAME (" EOF "," AdoEOF ") Its ultimate role is similar to our familiar #include, when compiling, the system will generate msado15.tlh, ado15.tli two C header files to define the ADO library.

/ *************** [3] Create a Connection object and connect to the database ******************* / First we need to add pointing a pointer to the Connection object: _ConnectionPtr m_pConnection; BOOL CADOTest1Dlg :: OnInitDialog () {CDialog :: OnInitDialog (); try {HRESULT hr = m_pConnection.CreateInstance ( "ADODB.Connection"); // create a Connection object if (SUCCEEDED ( HR)) {hr = m_pconnection-> open ("provider = microsoft.jet.Oledb.4.0; data source = test.mdb", "", ",", admodeunknown); / / Connect the database / / above one sentence The provider in the string is for the Access2000 environment. For Access 97, it needs to be changed to: provider = microsoft.jet.Oledb.3.51;}}} catch (_ERROR E) // COM error gets, when the COM function is executed, if an error occurs , Can capture _Com_ERROR's exception {cstring strcomerror; strcomerror.format ("error number:% 08LX / N error message:% S / N error source:% S / N error description:% s", E.Error () , // Error number E.ErrorMessage (), // Error message (LPCSTR) E.Source (), // Error source (LPCSTR) E.DESCRIPTION ()); // Error Description :: MessageBox (Null, Strcomerror, "Error", MB_ICONEXCLAMATION);}} can also be connected using UDL files. Try {m_pconnection.createInstance (__ uuidof (connection)); m_pConnection-> Connectionstring = "file name = e.udl"; m_pconnection-> open (",", ",", null);} catch (_com_error e) { ....} In this code, we are connecting the database through the Open method of the Connection object. Below is the prototype of the method HRESULT Connection15 :: Open (_BSTR_T Connectionstring, _BSTR_T UserID, _BSTR_T Password, Long Options) ConnectionsTRING For the connection string, userid is the username, Password is the login password, Options is the connection option, which is used to specify the update of the Connection object to the data. Options can be as follows: admodeunknown: Default.

The current license is not adModeRead settings: read-only adModeWrite: write only adModeReadWrite: can read and write adModeShareDenyRead: Connection object to prevent other read permissions to open a connection adModeShareDenyWrite: Connection object to prevent other write permissions to open a connection adModeShareExclusive: Connection object to prevent other open connection adModeShareDenyNone : Allow other programs or objects to establish connectivity to any permission ◆ Common database connection method: (1) Connection M_PConnection-> Open through the Jet Database Engine to Access2000 Database ("Provider = Microsoft.jet.OleDb.4.0; Data Source = C : //test.mdb ",", "" ", admodeunknown); (2) Connect any database supporting ODBC using DSN data sources: m_pconnection-> open (" data source = adotest; uid = sa; pwd = ",", ",", admodeunknown); // m_pconnection-> open ("DSN = Test;", "," ", 0); // Connect the ODBC data source (3) called TEST DSN Connect to the SQL Server database: m_pconnection-> open ("driver = {sql server}; server = 127.0.0.1; database = vckbase; uid = sa; pwd = 139", ",", "", admodeunknown); Server is the name of the SQL server, Database is the name of the library. / Set the timeout time of 5 second m_pConnection-> Open ("Data Source = adotest;", ",", "" "" ",", ",", ",", ",", ",", "0 indicates that the status is turned off, 1 indicates that it has been opened, we can pass Read this property to make Corresponding processing, for example: if (m_pconnection-> state) m_pconnection-> close (); // If the connection has been opened, turn off it 4] / **************** Execute the SQL command and acquire the result record set *************** / For the results record set, we define a pointer to the Recordset object: _RecordSetPtr M_PRecordset; and create a RecordSet object Example: m_precordset.createInstance ("AdoDb.Recordset"); execution of SQL commands can be used in a variety of forms, below.

◆ (1) using the Connection object's Execute method executes the SQL command prototype Execute method is as follows: _RecordsetPtr Connection15 :: Execute (_bstr_t CommandText, VARIANT * RecordsAffected, long Options) wherein CommandText is a command string, usually SQL commands. Recordsaffected is the number of rows that are affected after the operation is completed. Options represents the type of content in CommandText. Options can take one of the values: AdcmdText: Indications CommandText is the text command adcmdtable: indicates that CommandText is a table name AdcmdProc: indicating that CommandText is a stored procedure AdcmDunkNown: Unknown Execute After the execution, return a pointer to the record set, below we give specific code and instructions.

Try {_variant_t ra; m_pconnection-> execute ("Creger Table, Name TEXT, Age Integer, Birthday DateTime", & ra, AdcmdText); M_PConnection-> Execute ("INSERT INTO student information (student number, Name, age, birthday) VALUES (112105, 'Cheng Hongxiu', 22, '1982-08-16') ", & ra, adcmdtext); // Add record M_PRecordset = m_pconnection-> execute (" Select Count (* " "From student information", "& ra, adcmdtext); // Execute SQL statistics command to get record set containing the number of records _Variant_t vcount = m_precordset-> getCollect ((_ variant_t) (0)); // get the first one The value of the field is placed in a vcount variable m_precordset-> close (); cstring message; message.format ("a total of% D record", vcount.lval); afxMAGEBOX (Message);} catch (_ERROR E) {...} ◆ (2) Use the Command object to execute the SQL command try {_commandptr m_pcommand; m_pcommand.createInstance ("AdoDb.command"); m_pcommand-> activeConnection = m_pconnection; // Key sentence, the established connection assignment to it m_pcommand-> Commandtext = "INSERT INTO Student Information (Student No., Name, Age, Birthday) Values ​​(112105, 'Cheng Hongxiu', 22, '1982-08-16')"; M_PCommand-> Execute (null, null, adcmdtext); m_pcommand -> CommandText = "Select count (*) from student information"; m_precordset = m_pcommand-> execute (null, null, adcmdtext) _VARIANT_T VCOUNT = m_pRecordset-> getCollect ((_ variant_t) (long) 0); // get the value of the first field CString Str; str.format ("a total of% D record", vcount.lval); afxMessageBox (STR ); M_precordset-> close ();} catch (_COM_ERROR E) {...} In this code, we just use the Command object to perform the SELECT query statement, and the Command object is actually reflected in the call to the stored procedure. Role. Next time we will introduce it.

◆ (3) Searching directly with the Recordset object, the record set, for example, M_PRecordset-> Open ("SELECT * FROM Student Information", _variant_t ((idispatch *) m_pconnection, true), adopenStatic, adlickTimistic, AdopenStatic, AdlickText); the prototype of the Open method is this: HRESULT Recordset15 :: Open (const _variant_t & Source, const _variant_t & ActiveConnection, enum CursorTypeEnum CursorType, enum LockTypeEnum LockType, long Options) where: ①Source is a data query string ②ActiveConnection is already established connections (we need connection Object pointer to construct a _variant_t object) 3CURSORTYPE cursor type, it can be one of the following values, please see this enumeration structure: Enum cursortypeenum {adopenunSpecified = -1, // Do not specify adopenforwardonly = 0, // front roller 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, both ADOPENKEYSET = 1, // The record set with this cursor does not see the new, delete operation of other users, but is visible to you for updating the original record . 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 structure: 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 lock 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 of the Connection object / ******************************************************** ********************** / According to the results of the student information table we have just implemented by executing the SQL command, it contains four fields: student number, name, age Code implementation below the birthday: Open the record set, traverse all records, delete the first record, add three records, move the cursor to the second record, change its age, save to the database.

try {_variant_t vUsername, vBirthday, vID, vOld; _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance ( "ADODB.Recordset"); m_pRecordset-> Open ( "SELECT * FROM student information", _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 give it directly Live the name, as follows VUSERNAME = m_PRecordset-> getCollect ("Name"); // Value VOLD = M_PRecordset-> getCollect ("age"); vbirthday = m_precordset-> getCollect ("birthday); Trace ("ID:% D, Name:% S, Age:% D, Birthday:% S / R / N", Vid.Lval, (LPCTSTSTR) (_ BSTR_T) VUSERNAME, VOLD.LVAL, (LPCTSTSTR) (_ BSTR_T) vbirthday); / / // In the DEBUG mode, the OUTPUT window output record set M_PRecordSet-> MoveNext (); // Move to the next record} m_precordset-> movefirst (); // Move to the first record m_precordset-> delete (ADAFFECTCURRENT); / / Delete the current record for (int i = 0; i <3; i ) // Add three new records and assign the value {m_precordset-> addnew (); // Add new record m_precordset-> Putcollect ("learn ", _Variant_t ((long) (i 10))); m_precordset-> Putcollect ( "Name", _variant_t ("Wang Bin Year")); m_precordset-> Putcollect ("age", _variant_t ((long) 21)); m_precordset-> Putcollect ("birthday", _variant_t ("1930-3-15")) } M_precordset-> Move (1, _variant_t ((long) adbookmarkfirst); // move a record from the first record, that is, move to the second record m_precordset-> Putcollect (_variant_t ("age") _VARIANT_T ((long) 45)); // Modify its age m_precordset-> update (); // Save to the library} catch (_COM_ERROR E) {} / ************ ********** [6] Close the record set and connection ************************ / record set or connection can Use a Close method to close m_precordset-> close (); // Close record set m_pConnection-> close ();

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

New Post(0)