Ado's first close contact

Source: Internet
Author: User
Tags dsn

The first intimate contact with ADO-one of the ADO development practices
Author: Wang Jun, Wenling telecom company, Zhejiang Province

I. Introduction to ADO
ADO (ActiveX Data Object) is a new interface for Microsoft database application development. It is a high-level database access technology built on top of ole db. Do not worry about this, even if you are, com can also easily deal with ADO without understanding it, because it is very easy to use, and even easier to use than the ODBC APIs, Dao, and rdo you used in the past. This article describes in detail how to use ADO for database application development under VC and provides sample code.
Sample Code

Ii. Basic Process
Everything is difficult at the beginning. Any new technology is the most important thing for beginners to learn about is getting started. Let's take a look at the basic process of ADO database development!
(1) initialize the com library and introduce the ADO library definition file.
(2) connect to the database using the connection object
(3) Use the established connection to execute SQL commands through the connection and command objects, or use the recordset object to obtain the result record set for query and processing.
(4) Close the connection and release the object after use.

Preparations:
For everyone to test the example provided in this article, we use the ACCESS database, you can also find this test. mdb directly in the sample code we provide.
Next we will introduce the above steps in detail and provide relevant code.
[1]Com library Initialization
We can use afxoleinit () to initialize the com library, which is usually completed in the overload function of cwinapp: initinstance (). See the following code:

BOOL CADOTest1App::InitInstance()  {  AfxOleInit();  ...... 

[2]Use the # import command to introduce the ADO Type Library
We add the following statement to stdafx. h: (where can I find the stdafx. h file? You can find it in header files in FileView)

#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")

What is the role of this statement? The final function is similar to # include, which we are familiar with. during compilation, the system will generate msado15.tlh and ado15.tli C ++ header files to define the ADO library.

Notes:
(1) msado15.dll may not be in this directory in your environment. modify it as needed.
(2) The following warning may appear during compilation. Microsoft explained this in msdn and suggested that we ignore 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
First, we need to add a pointer to the connection object:
_ Connectionptr m_pconnection;
The following code demonstrates how to create a connection object instance, connect to a database, and capture exceptions.

Bool cadotest1dlg: oninitdialog () {cdialog: oninitdialog (); hresult hr; try {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 to the database // The provider in the connection string in the above sentence is for the Access2000 environment. For Access97, you need to change it: provider = Microsoft. jet. oledb.3.51;
} Catch (_ com_error e) // catch exception {cstring errormessage; errormessage. Format ("failed to connect to the database! /R/n error message: % s ", E. errormessage (); afxmessagebox (errormessage); // Display error message}

In this Code, we connect to the database through the open method of the connection object. The following is a prototype of this method.
Hresult connection15: open (_ bstr_t connectionstring, _ bstr_t userid, _ bstr_t password, long options)
Connectionstring is the connection string, userid is the user name, password is the login password, options is the connection option, used to specify the connection object to update the data permission,
Options can be the following constants:
Admodeunknown: default. The current permission is not set
Admoderead: Read-Only
Admodewrite: Write only
Admodereadwrite: read/write
Admodesharedenyread: blocks other connection objects and opens the connection with the read permission.
Admodesharedenywrite: blocks other connection objects and opens the connection with write permission.
Admodemo-exclusive: prevents other connection objects from opening connections.
Admodesharedenynone: allows other programs or objects to establish connections with any permissions.

We provide some common connection methods for your reference:
(1) connection to the Access2000 Database through the Jet Database Engine

m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C://test.mdb","","",adModeUnknown);

(2) Use the DSN data source to connect to any database that supports ODBC:

m_pConnection->Open("Data Source=adotest;UID=sa;PWD=;","","",adModeUnknown);

(3) do not use DSN to connect to the SQL Server database:

m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=vckbase;UID=sa;PWD=139","","",adModeUnknown);

The server is the name of the SQL Server, and the database is the name of the database.

There are many methods in addition to the open method of the connection object. First, we will introduce two useful attributes of the connection object: connectiontimeout and state.
Connectiontimeout is used to set the connection timeout time, which must be called before open, for example:

M_pconnection-> connectiontimeout = 5; // set the timeout value to 5 seconds. m_pconnection-> open ("Data Source = adotest;", "", "", admodeunknown );

The state attribute specifies the status of the current connection object. The value 0 indicates that the connection object is closed, and the value 1 indicates that the connection object has been opened. You can read this attribute for processing. For example:

If (m_pconnection-> state) m_pconnection-> close (); // if the connection has been enabled, close 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;
And create a recordset object for it: m_precordset.createinstance ("ADODB. recordset ");
SQL commands can be executed in multiple forms. The following describes how to execute these commands.

(1) Execute SQL commands using the execute method of the connection object
The prototype of the execute method is as follows:
_ Recordsetptr connection15: Execute (_ bstr_t commandtext, variant * recordsaffected, Long Options) commandtext is a command string, usually an SQL command. The recordsaffected parameter indicates the number of rows affected by the operation. The options parameter indicates the content type in commandtext. options can be one of the following values:
Adshorttext: indicates that commandtext is a text command.
Adcmdtable: indicates that commandtext is a table name.
Ad1_proc: indicates that commandtext is a stored procedure.
Adcmdunknown: Unknown

After Execute is executed, a pointer pointing to the record set is returned. The specific code is provided and described below.

_ Variant_t recordsaffected; // run the SQL command: Create Table to create users. Users contains four fields: integer ID, string username, and integer old, date type birthday m_pconnection-> execute ("create table users (ID integer, username text, old integer, birthday datetime)", & recordsaffected, ad1_text ); /// Add the record m_pconnection-> execute ("insert into users (ID, username, old, birthday) values (1, 'Washington ', 25, '2014/1/1') ", & recordsaffected, ad1_text ); /// Add the values of all record old fields to m_pconnection-> execute ("Update users set old = old + 1", & recordsaffected, ad1_text ); /// execute the SQL statistics command to obtain the set of records containing the number of records m_precordset = m_pconnection-> execute ("select count (*) from users", & recordsaffected, ad1_text ); _ variant_t vindex = (long) 0; _ variant_t vcount = m_precordset-> getcollect (vindex); // get the value of the first field and put it in the vcount variable m_precordset-> close (); /// disable the record set cstring message; message. format ("% d records in total", vcount. lval); afxmessagebox (Message); // display the number of current records


(2) Execute SQL commands using command objects

_ Commandptr m_pcommand; m_pcommand.createinstance ("ADODB. command "); _ variant_t vnull; vnull. vt = vt_error; vnull. scode = disp_e_paramnotfound; // It is defined as a very critical sentence without the parameter m_pcommand-> activeconnection = m_pconnection, assign the established connection m_pcommand-> commandtext = "select * from users"; // command string m_precordset = m_pcommand-> execute (& vnull, & vnull, ad1_text ); /// execute the command to obtain the record set

In this Code, we only use the command object to execute the SELECT query statement. The command object can truly reflect its role in calling the stored procedure. We will introduce it in detail next time.

(3) directly use the recordset object for query to obtain the record set
For example

  m_pRecordset->Open("SELECT * FROM users",_variant_t((IDispatch *)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);

The prototype of the open method is as follows:
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 a established connection (we need to use the connection object pointer to construct a _ variant_t object)
③ Cursortype the cursor type. It can be one of the following values. Please refer to this enumeration structure:
Enum cursortypeenum
{
Adopenunspecified =-1, // do not specify
Adopenforwardonly = 0, // roll the static cursor forward. The cursor can only browse the record set forward. For example, if movenext is used to scroll forward, this method can increase the browsing speed. However, neither bookmark, recordcount, absoluteposition, or absolutepage can be used.
Adopenkeyset = 1, // the record set with this cursor cannot see the new or delete operations of other users, but you can see the operations to update the original records.
Adopendynamic = 2, // dynamic cursor. All database operations are immediately reflected in each user record set.
Adopenstatic = 3 // static cursor. It generates a static backup for your record set, but the addition, deletion, and update operations of other users are invisible to your record set.
};
④ Locktype: it can be one of the following values. See the following enumeration structure:
Enum locktypeenum
{
Adlockunspecified =-1, // not specified
Adlockreadonly = 1, // read-only record set
Adlockpessimistic = 2, pessimistic locking mode. Lock all other actions when data is updated. This is the most secure locking mechanism.
Adlockoptimistic = 3, Optimistic Locking mode. The record is locked only when you call the update method. Before that, you can update, insert, or delete data.
Adlockbatchoptimistic = 4, optimistic batch update. The record is not locked during editing. Modification, insertion, and deletion are completed in batch mode.
};
⑤ For options, refer to the introduction to the execute method of the connection object in this article.

[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, 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, ad1_text); While (! M_precordset-> adoeof) // Why is it adoeof instead of EOF? Do you still remember Rename ("EOF", "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 username field value vold = m_precordset-> getcollect ("old"); vbirthday = m_precordset-> getcollect ("Birthday "); /// 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 the value for (INT I = 0; I <3; I ++) {m_precordset-> addnew (); // Add the 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 one record down from the first record, that is, 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.

[6]Close record set and Connection
You can close Record Sets or connections using the close method.

M_precordset-> close (); // close the record set m_pconnection-> close (); // close the connection

At this point, I think you are familiar with the general process of operating the database through ADO. Maybe you have a good idea. Maybe you still have something to worry about! I suggest you try to write a few examples to better familiarize yourself with ADO. Finally, I wrote a small example for you, in this example, all records are read and placed in the list control, and records can be added, deleted, or modified.
Click here to download the sample code

Note: Due to the length constraints, many content in ado has not yet been introduced. Next time, we will detail the attributes and methods of the recordset object and discuss several key technologies: the binding method processes record set data, call of stored procedures, transaction processing, storage and reading of images in the database, and use with table controls.
Goodbye next time!

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.