Critically defined database classes in VC ++

zhaozj2021-02-11  251

Critically defined database classes in VC

Li Jianping, Harbin University of Engineering, Li Chunyan, Zhang Jidong

Summary

---- This paper introduces a CDATABASEOPERATE class using ODBC to operate using ODBC, and gives several main functions, and the use of such classes in actual applications.

---- As we all know that VC MFC class library has prepared a class for database operations. Programperspers can use the wizard to build a application that is bound to the database and operates the database, which does not require any code, which is undoubtedly Programmaker provides a shortcut. However, when using the wizard, only a single document or multi-document-based project can select the data source, connect to the specified database, and the dialog-based application-based dialog-based application does not provide support. Even if some special operations are required, for example, open a table, the MFC does not provide an existing function that satisfies the required requirements when returning a table. If you can use the database operation provided by the MFC, add your own functions, that is, design a class for database operations, manually add this class in the program, then you can be in the dialog-based application Implement the operation of the database, and it is also possible to design a function of the specific functionality for specific functions.

---- In an application involving database operations, commonly used MFC classes include CDAODatabase class, CDaOTableDef class, CDAORECORDSET class, and CDAOQueryDef classes. When operating the database, you need to open the database first, then open the table in the database. , Then get the query set and record set. In your own defined class, you can design a function of opening a table to get the query set and record set. After using this class in the application, you only need to include the header file of the class, the function designed. You can call directly.

---- The process of establishing a database class can be divided into the following four steps:

---- First, define a class-free CDATABASEOPERATE class

---- 1, select the ClassView tab at the Workspace window, right-click the root of the tree type structure diagram, select New Class ..., the system will pop up the dialog box for the establishment of a new class;

---- 2, select Generic Class in Class Type;

---- 3, fill in the name of the new class to build in Name, to start with uppercase letters C, the system will automatically establish a new class file and implement file, the name of the file is the first uppercase letter C, if you want to change the name of the file, click the Change button.

---- 4, after filling in various items, press the OK button to determine, a new category of a base class is successful, but he is still an empty class, next, you will add content to the class.

---- II, join the relevant definition in the custom class

---- 1, in this application, use ODBC to connect to SQL Server database, thus adding the following definition before the implementation file constructor of the class: #define sql_database _t ("ODBC; DSN = SQL-DATABASE; UID = SA; PWD = pass; ") DSN = SQL-Database indicates that the name of the established ODBC connection is SQL-Database, if other databases are selected, only the links established with the required database, or reconfigure SQL -Database gives it a new database. UID = SA; PWD = pass Represents the user name of the login database is SA, the password is Pass, if the password is expressed as PWD = "". ---- 2, in this class, in this class, several class CDAODATABASE classes, CDAOTableDef classes, CDaorecordSet classes, CDaOQueryDef classes provided by the MFC class library, and the four classes are included in header files. AFXDAO.H, therefore, in the header file of the newly defined class must be added:

#include ;

---- 3, the four classes to be used in the following statements are as follows:

CDAODATABASE * LOC_PDATABASE;

CDaotabledef * Loc_ptable;

CDAORECORDSET LOC_PRECORDSET;

CDAOQUERYDEF * LOC_PQUERYDEF;

---- Where the CDAODATABASE class, the CDaOTabledef class, and the CDAOQueryDef class defines the object pointer. When you are using, you should first go to DELETE. Take the CDAODATABASE class as an example, initialize the object pointer LOC_PDATABASE = New CDAODATABASE in the constructor of the CDATABASEOPERATE class; release the pointer delete loc_pdatabase in the destructor;

---- Third, join the required functions and variables in custom classes

---- Manual join function includes two work, first join the declaration of functions in the header file, then, declare and implementation must be unified in implementing the specific implementation of the functions to implement the file;

---- Use the wizard to join the function and variables:

---- 1, select the ClassView tab in the Workspace window;

---- 2, click the mouse button on the class of the tree structure diagram to add a function and variable, click Add MEMBER FUNCTION to add a virtual function, add the member variable order, add a virtual function. ADD MEMBER VARIABLE;

---- 3, after the dialog box, fill in the name, type, type, and system of the system will automatically add the declaration and implementation of the function;

---- 4, the specific operation of adding functions can be further filled in editing code;

---- These operations will be reflected immediately in the ClassView option of the Workspace window, and click the corresponding function in the classview to enter the implementation of the function, and further write code, if this is not done, The operation of adding member functions is incorrect.

---- The following is an example of this application, gives the specific table structure and several main functions, readers can design functions according to their own actual situation.

---- The structure of a typical table in this application is:

Sequence number topic content difficult coefficient score answer

Integer characteristic long integer double precision character character

---- Open the function of the database is implemented as follows: if (! LOC_PDATABASE-> isopen ())

LOC_PDATABASE-> Open (Null, False, False, SQL_DATABASE);

---- This function uses two functions of the CDAODatabase class. ISOpen () and Open (Null, False, False, SQL_DATABASE), because the pointer object LOC_PDATABASE of this class has been declared, so you can call the CDAODatabase class. Among them, the last parameter SQL_DATABASE in the open () function has been introduced in front, and the relevant database is opened.

---- Due to the opening table in the program, not only returns all record sets, but also to return a record set that meets a certain condition, so the function of opening the table has a parameter difficulty in the table name, lndxs = At 0, when all the data in the table, when LNDXS = 1 to n, the selection of the difficulty coefficient = 1 to n is selected.

Bool CDatabaseOperate :: OpenTable

(CSTRING STRTABLENAME, Long LNDXS)

{

CString strfieldnumber;

LOC_PTABLE = New CDaotabledef

(LOC_PDATABASE);

IF (! LOC_PTABLE-> isopen ())

LOC_PTABLE-> Open (strtablename);

/ / Open the specified table name

StrfieldNumber.Format ("% d", LOC_PTABLE->

Getfieldcount ()); // Get the number of fields

CSTRING SQLSTR, SQLSTR1, SQLSTR2;

LOC_PQUERYDEF = New CDAOQueryDef (LOC_PDATABASE);

// Get the query set and record set

IF (lnDxs == 0)

{

SQLSTR = _T ("Select * from" strtablename);

}

Else

{SQLSTR1 = "SELECT *" STRTABLENAME;

Sqlstr2.format ("WHERE Difficulties =% D", LNDXS);

SQLSTR = _T (SQLSTR1 SQLSTR2);

}

LOC_PQueryDef-> Create (NULL, SQLSTR);

LOC_PRECORDSET.OPEN (LOC_PQUERYDEF);

m_nrecordnumber = 0;

While (! loc_precordset.iseof ())

{

M_nRecordNumber ;

Loc_PrecordSet.movenext ();

}

Return True;

}

---- In order to maintain the security of the database, the table should be turned off after the mesh, close the table, and the object pointer initialized during the opening table is turned on, for example: delete LOC_PQueryDef. Also note that the object pointer initialized in the constructor is always released in the destructor. The initialization and release of the object pointer are pairing.

LOC_PDATABASE = New CDAODATABASE;

/ / Initialize the object pointer in the constructor.

DELETE LOC_PDATABASE;

// Release the object pointer in the destructor.

----

Fourth, the application of CDATABASEOPERATE class

---- 1, use the VC wizard to generate an application, you can choose the dialog-based or single, multi-document, select a single document or multiple documentation as needed, or multiple documents, or multiple documents, do not select database support. ---- 2, add #include "databaseoperate.h" in the main header file of the application, and still declare an object of a CDATABASEOPERATE class, public: cdatabaseOperate M_CDatabaseOperate;

---- 3, after the object of the CDATABASEOPERATE class, the functions that have just been prepared in the CDATABASEOPERATE class can be called via "M_CDatabaseOperate. Function Name".

summary:

---- This article is a preliminary discussion of the CDatabaseOperate class establishment and application under VC 6.0, and the functions implemented by the CDATABASEOPERATE class are very powerful. In addition to several basic and common functions of the CDatabaseOperate class, CDATABASEOPERATE has many functional functions for other aspects, which is not described here. The function of the CDATABASEOPERATE class implements the display, modification, addition, deletion of the database content, basically meet the needs of the database operator. In addition to these, programmers can also define their own unique functions according to the needs of the program.

---- The procedures provided herein are compiled in VC 6.0.

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

New Post(0)