Use ADO to write database applications

Source: Internet
Author: User
Tags export class

4.1 Overview
Visual C ++ encapsulates DAO. The mfc dao class encapsulates most of DAO (Database Access Object) functions, from the Visual C ++ program, you can use the mfc dao class provided by Visual C ++ to conveniently access the Microsoft Jet Database, and compile a simple and Visaul C ++ database application.
Database Access Object (DAO) provides a mechanism for creating and manipulating databases through program code. Multiple DAO objects constitute an architecture in which each DAO object works collaboratively. DAO supports the following four Database options:
1. Open the ACCESS database (MDB File)-The MDB file is a self-contained database, which includes the query definition, security information, indexes, relations, and actual data tables. You only need to specify the path name of the MDB file.
2. Open the ODBC data source directly-there is an important restriction here. You can only use a data source with your own ODBC driver DLL.
3. Use the Jet engine to search for ISAM data sources (including dBase, FoxPro, Paradox, Btrieve, Excel, and text files)-even if the ODBC data source is already set, to use the Jet engine to access one of these file types, you must also open the file in the form of an ISAM data source, rather than the ODBC data source.
4. attach an External table to the ACCESS database-this is actually the preferred method to ACCESS the ODBC data source using DAO. First, use ACCESS to add the ODBC table to an MDB file, and then use DAO to open the MDB file according to the method described in the first option. You can also use ACCESS to attach an IASM file to an MDB file.
4.2 Application DAO Programming
4.21 open a database
The CDaoWorkspace object represents a DAO Workspace object, which is at the highest level in the mfc dao architecture. It defines a user's session to the same database and contains the opened database, completes database transactions. We can use an implicit workspace object.
The CDaoDatabase object represents a connection to the database. In MFC, It is encapsulated by CDaoDatabase.
When constructing a CDaoDatabase object, there are two methods:
1. Create a CDaoDatabase object and pass a pointer to a CdaoWorkspace object that has been opened.
2. Create a CDaoDatabase object without explicitly specifying the workspace to use. In this case, MFC creates a temporary CDaoWorkspace object.
The following code is used:
CDaoDatabase db;
Db. Open ("test. mdb", FALSE, FALSE, _ T ("");
Parameter 1 includes the full path name of the file to be opened.
4.22 query records
A dao recordset object represents a set of data records. This set is a database table or all records in the running results of a query. There are three types of CDaoRecorset objects: tables, dynamic sets, and snapshots.
In general, we can use the export class of CDaoRecordset in the application, which is generally generated through ClassWizard or AppWizard. However, we can also directly use the object generated by the CDaoRecordset class. In this case, we can dynamically bind the data member of the recordset object.
The following code is used:
COleVariant var;
Long id;
CString str;
CDaoRecordset m_Set (& db );
M_Set.Open ("SQL statement queried ");
While (! M_Set.IsEOF ())
M_Set.GetFieldValue ("ID", var );
Id = V_I4 (var );
M_Set.GetFieldValue ("Name", var );
Str = var. pbVal;
M_Set.MoveNext ();
M_Set.Close ();
4.23 add record
The AddNew function is used to add records. SetFieldValue is used to assign values.
The following code is used:
M_pDaoRecordset-> AddNew ();
Sprintf (strValue, "% s",> m_UserName );
M_pDaoRecordset-> SetFieldValue ("UserName", strValue );
Sprintf (strValue, "% d", m_PointId );
M_pDaoRecordset-> SetFieldValue ("PointId", strValue );
DataSrc. SetDateTime (m_UpdateTime. GetYear), m_UpdateTime. GetMonth), m_UpdateTime. GetDay (),
M_UpdateTime. GetHour (), m_UpdateTime. GetMinute (), m_UpdateTime. GetSecond ());
ValValue = dataSrc;
M_pDaoRecordset-> SetFieldValue ("UpdateTime", valValue );
Sprintf (strValue, "% f", m_pRecordset-> m_OldValue );
M_pDaoRecordset-> SetFieldValue ("OldValue", strValue );
Sprintf (strValue, "% f", m_pRecordset-> m_NewValue );
M_pDaoRecordset-> SetFieldValue ("NewValue", strValue );
M_pDaoRecordset-> Update ();
In this case, you must note that the date and time data must be assigned values using the SetDataTime function. COleVariant data is used here. For specific usage instructions, refer to the help documentation.
4.24 modify records
Use the Edit () function to locate the record to be modified, call the Edit function, and call the Update function after modification.
The following code is used:
M_Set.Edit ();
M_Set.SetFieldValue ("column name", "string ");
M_Set.Update ();
4.25 delete records
The Delete () function is used to Delete records. You do not need to call the Update () function after use.
4.26 statistical records
You can use the following code to count the number of records:
COleVariant varValue;
CDaoRecordset m_Set (& db );
M_Set.Open (dbOpenDynaset, "SQL statement ");
VarValue = m_Set.GetFieldValue (0 );
M_lMaxCount = V_I4 (& varValue );
M_Set.Close ();
To count the total records in a table, you can use the CDaoTableDef object, as shown in the following code:
CDaoTableDef m_Set (& gUseDB );
Count = m_Set.GetRecordCount ();
M_Set.Close ();
You cannot use GetRecordCount () of the CDaoRecordset object to retrieve the number of records.
4.3 conclusion
With DAO technology, we can easily access the Microsoft Jet Engine database. Because Microsoft Jet does not support multithreading, We must restrict all DAO calls to the main thread of the application.

Related Article

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: 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.