Write database applications using ADO

zhaozj2021-02-08  264

Use DAO

4.1 Overview

Visual C provides a package of DAO, MFC DAO classes encapsulated most of the DAO (Database Access Object), which can use Visual C programs to use the MFC DAO classes provided by Visual C to easily access Microsoft Jet databases, compile, There are Visaul C Database applications.

The Database Access Object (DAO) provides a mechanism for creating and manipulating a database through program code. Multiple DAO objects constitute an architecture, in which each DAO object works together. DAO supports the following four database options:

1. Open access database (MDB file) - MDB file is a self-contained database, which includes query definition, security information, index, relationship, and of course there is also a practical data sheet. Users only specify the path name of the MDB file.

2. Open the ODBC data source directly - there is a very important limit. You cannot find an ODBC data source as a driver as a driver; you can only use the data source with your own ODBC driver DLL.

3, use the JET engine to open the ISAM type (index order access method) data source (including DBASE, FoxPro, Paradox, Btrieve, Excel or text file) - Even if the ODBC data source has been set, use the JET engine to access these files One of the types must also be found in the way ISAM type data source instead of using an ODBC data source.

4. Additional external tables to the Access database - this is actually the preferred method of accessing the ODBC data source with DAO. First use Access to add the ODBC table to an MDB file, and then find this MDB file with DAO according to the method described in the first option. Users can also use Access to attach the IASM file to an MDB file.

4.2 Application DAO Programming

4.21 Open Database

The CDAOWORKSpace object represents a DAO Workspace object, which is at the highest in the MFC DAO architecture, defines a session of a user with a database, and contains the open database, responsible for completing the transaction processing of the database. We can use an implicit Workspace object.

The CDAODatabase object represents a connection to the database, in the MFC, is encapsulated by CDAODatabase.

There are two ways when constructing a CDAODatabase object:

1. Create a CDAODATABASE object and deliver a pointer to a CDAOWORKSPACE object that has been found.

2. Create a CDAODatabase object without explicitly specifying Workspace, at this time, MFC will create a new temporary CDAOWORKSPACE object.

The following code shows:

CDAODATABASE DB;

DB.Open ("Test.mdb", False, False, _T ("");

The parameter includes the full path name of the file to be opened.

4.22 query record

A DAO Recordset object represents a collection of data records, which is a library table or a full record in a query run. There are three types of CDAORECORSET objects: tables, dynamic sets, snapshots.

Typically, we can use CDAORECORDSET in your application, which is usually generated by ClassWizard or AppWizard. But we can also use the object generated by the CDaorecordset class directly. At this point, we can dynamically bind the data members of the Recordset object.

The following code shows:

Colevariant var; long id;

CString Str;

CDAORECORDSET M_SET (& DB);

m_set.open ("SQL statement");

While (! m_set.iseof ())

{

/ *

deal with

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

Add a record with the AddNew function, at this point with SetFieldValue to assign.

The following code shows:

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 ();

At this point, it is important to note that the datetime data is to be assigned with the setDataTime function, which is used to use ColeVariant type data, and the specific usage can be referred to the help.

4.24 Modify Record

Modify the record with the edit () function, position the record to the location you want to modify, call the Edit function, and then call the Update function after the modification is complete.

The following code shows:

m_set.edit ();

m_set.setfieldValue ("Column Name", "String");

m_set.update ();

4.25 Delete Record

Deleting a record with a delete () function, does not need to call the Update () function after use.

4.26 statistical record

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 ();

If you are a total record in a table, you can use the CDAOTableDef object, as shown below: CDaOTableDef m_set (& gusedb);

Count = m_set.getRecordcount ();

m_set.close ();

The number of records cannot be obtained with the getRecordCount () of the CDaorecordSet object.

4.3 Summary

With DAO technology, we can easily access the Microsoft Jet Engine database. Since Microsoft Jet does not support multi-thread, all DAOs that are called to the application main thread must be restricted.

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

New Post(0)