**************************************** **************************************** ********************** The description of ADO in this article is taken from the VB programming Park. Applicability: Database Intended audience: C ++, beginner **************************************** **************************************** ********************** I. Introduction to ADO ActiveX Data Objects (ADO) is Microsoft's latest data access technology. It is designed to work with the new data access layer ole db provider to provide universal data access ). Ole db is a low-level data access interface that can be used to access various data sources, including traditional relational databases, as well as e-mail systems and custom business objects. ADO provides us with a familiar and high-level automation encapsulation interface for ole db. For programmers familiar with rdo, you can compare ole db to ODBC drivers. Just as rdo objects are ODBC driver interfaces, ADO objects are ole db interfaces, just as different database systems need their own ODBC drivers, different data sources require their own ole db Provider ). At present, although ole db providers are relatively small, Microsoft is actively promoting this technology and intends to replace ODBC with ole db. 2. Where can I get ado? Currently, the latest version can be downloaded from the Microsoft website for free. So far, the Microsoft website is still the best place for you to get the latest information about ADO. ADO is provided as part of the ole db sdk. You can download from the following URL: Http://www.microsoft.com/data/oledb/download.htm Iii. How to use ADO There are many ways to use ADO. The most common method is to directly use Microsoft ADO Data Control in the activate control, which is convenient to use. However, you may think that it also has many disadvantages, such as being inconvenient to use and obscure to function interfaces. In short, it is not flexible. Today, I will introduce you to a class, which is essentially a package for ADO and is quite flexible to use. The detailed code is included in the attached compressed package ado.zip. This class is created by Carlos antollini. The current version is 2.04. It supports ado1.5 and has two files, ADO. h and ADO. cpp. I only made a few changes. Database introduction: This library consists of six parts:Cadocommand(Command processing ),Cadodatabase(Database subject), cadoexception (accidental handling), cadofieldinfo (field value), cadoparameter (parameter ),Cadorecordset(Record set ). Among them, the most important is the cadocommand class, cadodatabase class and cadorecordset. To use this library, you may need to install the following software (I have not strictly tested it, so the actual situation may be slightly different ): 1, mdac2.8 2. Microsoft Visual Studio 6.0 Usage and Examples Next, I will demonstrate how to use this database using an example of connecting to the SQL Server database. Step 1: Create a dialog box project to add several widgets to the Main Dialog Box Step 2: Add ADO. h and ADO. cpp and copy msado15.dll to the directory where the project. DSP is located. Step 3: Use Access to create a database, add an account table, and add several records Step 4: Modify the Code as follows: // Header file of the Main Dialog Box # Include "ADO. H" Class cadodemodlg: Public cdialog { // Construction Public: Cadodemodlg (cwnd * pparent = NULL); // standard Constructor // Omitting the system code PRIVATE: // Obtain the current value Void getcurrentval (void ); // Set the control data of the current window Void setcontroldata (cstring straccount, cstring strnick, int narticle, int nflower, int nknife ); // Initialize the database Bool initdatabase (void ); Cadorecordset m_prs; // data table Cadodatabase m_pdb; // Database Int nrecordindex _; // index of the current record }; // Implementation file of the Main Dialog Box // Adodemodlg. cpp: implementation file Cadodemodlg: cadodemodlg (cwnd * pparent/* = NULL */) : Cdialog (cadodemodlg: IDD, pparent) // Constructor { //... Omitting system code Nrecordindex _ = 0; } Bool cadodemodlg: oninitdialog () // initialization dialog box { Cdialog: oninitdialog (); //... Omitting system code Initdatabase (); // initialize the database Getcurrentval (); // get the current record value Return true; // return true unless you set the focus to a control } Void cadodemodlg: ondestroy () // when closing the window, close the database { Cdialog: ondestroy (); If (m_prs.isopen ()) { M_prs.close (); } If (m_pdb.isopen ()) { M_pdb.close (); } } Bool cadodemodlg: initdatabase () {// Initialize the database // Strconnection, database connection string, and ADO support various connection strings Cstring strconnection = "provider = Microsoft. Jet. oledb.4.0; Data Source = E: // workspace // Adodemo // dB. mdb; persist Security info = false "; If (! M_pdb.open (strconnection) // open the database { Afxmessagebox (m_pdb.getlasterrorstring ()); Return false; } M_prs = cadorecordset (& m_pdb); // initialize the record set Cstring strsql = ""; Strsql. Format ("select * from account "); Cadocommand cmd (& m_pdb, _ T ("")); Cmd. settext (strsql ); Cmd. settype (ad1_text ); If (! M_prs.execute (& cmd) // use SQL statements to generate record sources { Afxmessagebox (m_pdb.getlasterrorstring ()); Return false; } Return true; } Void cadodemodlg: setcontroldata (cstring straccount, cstring strnick, int narticle, int nflower, int nknife) {// Set the content displayed on the Control // Account M_account. setwindowtext (straccount ); // Nickname M_nick. setwindowtext (strnick ); Cstring strnumtext; // Number of posts Strnumtext. Format ("% d", narticle ); M_articlecount. setwindowtext (strnumtext ); // Flowers Strnumtext. Format ("% d", nflower ); M_flower. setwindowtext (strnumtext ); // Flying knife Strnumtext. Format ("% d", nknife ); M_knife. setwindowtext (strnumtext ); } Void cadodemodlg: onprev () {// Processing of the previous button If (! M_prs.isopen () // the database is not opened Return; If (m_prs.isbof () // to the top Return; If (0 = nrecordindex _) Return; Nrecordindex _--; M_prs.moveprevious (); Getcurrentval (); } Void cadodemodlg: onnext () {// Processing function of the next button If (! M_prs.isopen () // the database is not opened Return; If (m_prs.iseof () | M_prs.isbof () // the end Return; If (INT) m_prs.getrecordcount ()-1) = nrecordindex _) Return; Nrecordindex _ ++; M_prs.movenext (); Getcurrentval (); } Void cadodemodlg: getcurrentval () {// Retrieve the data of the current record If (! M_prs.isopen () // the database is not opened Return; Cstring straccount; M_prs. getfieldvalue ("Account", straccount ,""); Cstring strnick; M_prs. getfieldvalue ("nickname", strnick ,""); Cstring strarticlecount; M_prs. getfieldvalue ("Number of posts", strarticlecount ,""); Cstring strflower; M_prs. getfieldvalue ("Flowers", strflower ,""); Cstring strknife; M_prs. getfieldvalue ("Flying knife count", strknife ,""); Setcontroldata (straccount, strnick, atoi (strarticlecount), atoi (strflower), atoi (strknife )); } Program description This demo is very simple. It only shows how to query a database. In fact, the database program is really nothing special, mainly using SQL statements for operations. The program focuses on three functions: 1, bool initdatabase (void) This function initializes the database. Cstring strconnection = "provider = Microsoft. Jet. oledb.4.0; Data Source = E: // workspace // Adodemo // dB. mdb; persist Security info = false "; The preceding statement generates a connection string. If (! M_pdb.open (strconnection )) Use the statements generated above to open the database M_prs = cadorecordset (& m_pdb ); If the database is successfully opened, open the table of the database. Cstring strsql = ""; Strsql. Format ("select * from account "); SQL statement. Cadocommand cmd (& m_pdb, _ T ("")); Cmd. settext (strsql ); Cmd. settype (ad1_text ); Use SQL statements to generate operation commands If (! M_prs.execute (& cmd) // use SQL statements to generate record sources After this operation, the data table is valid. 2. onprev and onnext are similar. Just use onprev to explain them. If (! M_prs.isopen () // the database is not opened Return; If (m_prs.isbof () // to the top Return; The code above first checks whether the database is valid and whether the table pointer has reached the top If (0 = nrecordindex _) Return; Nrecordindex is a bit special. It is actually an auxiliary variable, mainly because there is a record between the header (BOF) and the end (EOF) of the table and the real record. The recording interval, so nrecordindex is used for auxiliary judgment. Nrecordindex _--; For the previous record, nrecordindex minus 1 M_prs.moveprevious (); The table pointer moves a record forward. Getcurrentval (); Set the data of the current record Advanced principles of the ADO database: This library is actually an encapsulation of Microsoft ADO Data Control. Its essence is Microsoft ADO Data Control. It turns some obscure functional functions in this component into a simple and easy-to-understand function, and it is more flexible to use. Basically, Microsoft ADO data control can achieve what Microsoft ADO data control can do. Microsoft ADO data control is more convenient to do. Take the cadodatabase class as an example. Its core is: _ connectionptr m_pconnection! |