I. Introduction
Databases are one of the most popular applications. Almost every business department uses databases to record and manage various types of data. In VC, we can select database support when creating a project, select the data source and related tables, and select crecordview as the base class of our program, in this way, you can establish associations between applications and databases without having to compile any Code. However, the premise is that you know which database to use when creating a project, there are related databases. In fact, we often have many well-established applications and classes. In addition to not establishing associations with data sources, we only need to add ODBC interfaces on the basis, it can be associated with the database. This avoids repetitive operations caused by re-writing of some code unrelated to the database, and greatly improves the reusability and Utilization of the Code. Therefore, it is completely effective to establish associations with databases by adding ODBC applications in common programs.
Ii. ODBC technology
ODBC (Open Database conectiopen database interconnection) technology, as a standard application interface (API) for Microsoft to access databases) and an important part of windows open service architecture (OSA) has been widely recognized by many windows programmers. ODBC depends on the driver provided by the database manufacturer. When using the odbc api, the Windows ODBC management program passes the database access request to the correct driver, the driver then uses SQL statements to instruct the DBMS to access the database. Therefore, the existence of ODBC provides us with powerful capabilities and flexibility to develop database applications.
3. program example
(1) Open visual C ++ and click "New…" on the "file" menu... ", And then select the project of the" MFC Appwizard (exe) "Class in the pop-up" new "dialog box," Project name "is normal, press OK, in the next step 1, select "Single Document" for single document support. The following options are used except for selecting "cformview" as the base class of the Project Depending on the class, press the finish key, and the system generates a new normal project.
We will regard this project as the original project. Next we will add ODBC applications to it on the basis of this project so that the project can be associated with the data source, allows you to operate and manage data in a database.
(2) Open ODBC (32 bits) on the Control Panel to register the data source. To enable ODBC to work with the database, you must register the database with the ODBC driver manager. This can be done by defining a DSN or data source name. In the displayed "ODBC data source Manager", select the "user DSN" attribute page and click "Add... "Button. Select "Microsoft Access Driver (*. mdb)" as the data source drive and click "finish. In the displayed "ODBC Microsoft Access 97 setup" dialog box, add RP97 to the "Data Source Name:" column, "Deion:", which only serves as the comment description. You can leave this field empty, then click "select... "Button, select the data source to be registered, and click" OK "to complete the registration of the data source. In this step, any program on the local machine can access the database through the ODBC interface and data source name "RP97.
(3) Select the "FileView" property page in the "workspace" activity window of VC to open the standard framework header file "stdafx. H ", and add" afxdb. H "Reference: # include <afxdb. h>
(4) In the "workspace" activity window, select the "classview" property page, right-click "normal classes", and select "New Class... ", In the pop-up" New Class "dialog box, select" crecordset "as the base class of the newly added class, and enter the class name" codbcset "in the" name "column ", click "OK", select the "RP97" data source you just registered in the "ODBC" column in the pop-up dialog box, click "OK", and select a table for the database, click "OK". In "classview", a new class "codbcset" based on crecordset is added ". The following three functions bind elements at all levels of the database:
Cstring codbcset: getdefaconnect connect ()
{
Return _ T ("ODBC; DSN = RP97 ");
}
Cstring codbcset: getdefasql SQL ()
{
Return _ T ("[Document Table]");
}
Void codbcset: dofieldexchange (cfieldexchange * pfx)
{
// {Afx_field_map (codbcset)
Pfx-> setfieldtype (cfieldexchange: outputcolumn );
Rfx_text (pfx, _ T ("[Document ID]"), m_column1 );
Rfx_text (pfx, _ T ("[document name]"), m_column2 );
Rfx_text (pfx, _ T ("[Reporter]"), m_column3 );
//} Afx_field_map
}
(5) Add a new class "cconnectdb" based on "generic cwnd" in the same way ". Add a reference to "odbcset. H" to the source file of this class: # include "odbcset. H ". Add class codbcset before class cconnectdb in the header file of this class, and add public member variables and functions in this class:
Cdatabase m_dbdata;
Codbcset * m_pset;
Void cconnectdb: initial ()
{
// Open the data source RP97
Cstring OS = _ T ("ODBC; DSN = RP97 ");
M_dbdata.open (null, false, false, 0 );
M_pset = new codbcset (& m_dbdata );
// Open the document table in RP97 using the SQL Structured Query Language
Cstring SQL = "select * from document table ";
M_pset-> open (afx_db_use_default_type, SQL );
}
(6) Add a "test" button on the form and Its Response Function ontest ():
Void cnormalview: ontest ()
{
Cconnectdb connectdb;
// After initial () is executed, the m_pset pointer is not empty for safe use.
Connectdb. Initial ();
If (connectdb. m_pset = NULL)
Return;
Connectdb. m_pset-> movefirst ();
Cstring STR = connectdb. m_pset-> m_column3;
Afxmessagebox (STR );
}
Add two references at the beginning of the file:
# Include "connectdb. H"
# Include "odbcset. H"
Iv. Running and Testing
Compile and run the program. Click "test". The "Reporter" Field of the first record in "Document Table" of "RP97" database is displayed in the dialog box.
Summary:
The key of this program is to obtain the database pointer m_pset. When the class cconnectdb member function initial () is executed, m_pset has been obtained, before that, the pointer is null and cannot be used. Therefore, you must ensure that the initial () function is called before m_pset (). After m_pset is obtained, you can use various functions in the crecordset class to perform various operations and management on the database like other ODBC applications.