A simple and flexible database operation

xiaoxiao2021-03-06  42

A simple and flexible class of database operations: Purple Yuan

*********************************************************** *********************************************************** ** This article is taken from the VB programming paradise about ADO introduction. Scope of application: Database applicable object: C , primary ********************************************************** *********************************************************** *********** 1, ADO Introduction ActiveX Data Objects (ADO) is Microsoft's latest data access technology. It is designed to work together with new data access layer OLE DB Provider to provide universal data access (Universal Data Access). OLE DB is a low-level data access interface that uses it to access various data sources, including traditional relational databases, and email systems and custom commercial objects. ADO provides us with a familiar, high-level AUTOMATION package interface for OLE DB. For those programmers who are familiar with RDO, you can compare the OLE DB as an ODBC driver. Like the RDO object is the ODBC driver interface, the ADO object is an OLE DB interface; like different database systems require their own ODBC drivers, different data sources require their own OLE DB provider (OLE DB Provider). Currently, although the OLE DB provider is relatively small, Microsoft is actively promoting this technology and intends to replace ODBC with OLE DB. Second, where can I get ADO? The latest version of the latest version is already available from Microsoft's website. So far, Microsoft's website is still the best place for ADO's latest information. ADO is provided as part of OLE DB SDK. You can download from the following URL: http://www.microsoft.com/data/oledb/download.htm 3. How to use ADO Use ADO Have many ways, the most common method is to directly use Microsoft ADO in the Activate control Data Control, which is easy to use. But everyone may think that there is a lot of shortcomings, such as it is not convenient enough, the interface of the function is difficult to understand, in short, it is not flexible. Today I introduce a class, which is essentially a packaging of ADO, which is quite flexible. The detailed code is included in the included compressed package ADO.ZIP. The author of this class is Carlos Antollini. The current version is 2.04, supporting ADO1.5, with two files, ADO.H and Ado.cpp. I only made a small amount of changes. Introduction to the library: This library is mainly composed of 6 parts, named CADOCOMMAND, CADODATABASE (database main body), CADOEXCEPTION, CADOFIELDINFO (field value), CADOPARAMETER, CADORECORDSET (Recorder).

The most important of these is the Cadocommand class, Cadodatabase class, and CadorecordSet. To use this library, you may need to install the following software (I don't have strict test, so actually the situation may be slightly transported): 1, MDAC2.8 2, Microsoft Visual Studio 6.0 Usage and Example I will connect to SQL in one Examples of the Server database do demonstrate, gradually show how to use this library.

In the first step, create a dialog engineering, add the second step in the second step in the picture, add ado.h and ado.cpp, and copy MSADO15.DLL to the directory where the project .dsp is located Three steps, create a database with Access, add an Account table, add a few records fourth step, modify the code as follows: // Main dialog box header file #include "ado.h" Class Cadodemodlg: public cdialog {//// Construction public: Cadodemodlg (CWND * PParent = NULL); // Standard Constructor / / omitting system code private: // Take the current value void getcurrentval (void); // Set the data of the control of the current window Void setControlData (CString Straccount, CSTRING STRNICK, INT NKNIFE, INT NFLOWER, INT NKNIFE); // Initialization Database BOOL INITDATABASE (VOID); CADORECORDSET M_PRS; // Data Sheet Cadodatabase M_PDB; // Database INT NRECORDEX_; // Current Record Index}; // Main Directive file // adodemodlg.cpp: Implementation File Cadodemodlg :: Cadodemodlg (CWND * PParent / * = null * /): cdialog (Cadodemodlg :: IDD, PParent) // Constructor {// ... omitted system code NRECORDEX_ = 0;} Bool CadodeModlg :: OnIndialog () // Initialization dialog {cdialog :: oninitdialog (); // ... omitted system code initdatabase (); // Initialization database getCurrentVal (); // Take the current record value Return True; // Return True UnsS you set the focus to a contriation Ol} void cadodemodlg :: OnDestroy () // When the window is turned off, turn off the database {cdialog :: Ondestroy (); if (m_prs.isopen ()) {m_prs.close ();} if (m_pdb.isopen ()) {m_pdb.close ();}} BOOL CADEMODLG :: InitDatabase () {// Initialization Database // StrConnection, Database Connection String, ADO Supports 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 Database {AFXMESSAGEBOX (M_PDB.GetLastErrorString ()); returnaf; } m_prs = cadorecordset (& m_pdb); // Initialization record set cstring strsql = ""; strsql.format ("select * from account");

Cadocommand cmd (& m_pdb, _t (")); cmd.settext (strsql); cmd.settype (adcmdText); 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) {// // content account m_Account .SetWindowText (strAccount displayed on the control settings ); // nickname m_nick .SetWindowText (strnick); cstract strnumtext; // Post number strnumtext .format ("% d", narticle); m_articlecount .SetWindowText (strnumtext); // Flower StrnumText .format ("% D" , nflower; m_flower .SetWindowText (StrnumText); // Flying knife strnumtext .format ("% d", nknife); m_knife .SetWindowText (StrnifeText);} void CadodeModlg :: onprev () {// Processing of the previous button IF (! m_prs.isopen ()) // Database Didn't open Return; if (m_prs.isbof ()) // to the top Return; if (0 == nRecordEx_) Return; NRecordIndex_ -; M_PRS.MOvePrevious (); getCurrentVal (); _PRS.ISEOF () || m_prs.isbof ()) // Upset Return; IF ((int) m_prs.getRecordCount () - 1) == nRecordIndex_) Return; NRecordIndex_ ; M_PRS.MOVENEXT (); getCurrentVal (); void Cadodemodlg :: getCurrentVal () {// Take the current recorded data if (! m_prs.isopen ()) // database did not open Return; cstring straccount; m_prs .GetfieldValue ("account", straccount, "" CSTRING STRNICK; M_PRS .GETFIELDVALUE ("Nickname", strnick, ""); cstring strarticleCount; M_PRS .GETFIELDVALUE ("Posted Quantity", StrarticleCount, ""); CSTRING STRFLOWER; M_PRS .GETFIELDVALUE ("Number of Flowers", Strflower, ""); CSTRING STRKNIFE; M_PRS .GETFIELDVALUE ("Number of flying knives", strKnife, "");

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

New Post(0)