Create a custom database class in VC ++

Source: Internet
Author: User
Tags odbc connection
Welcome to the CC ++ programming Community Forum and interact with 3 million technical staff. This article details the establishment of a CdataBaseOperate class that uses ODBC to operate databases, the specific implementation of several major functions and the usage of this class in actual applications are given. We all know that the VC ++ MFC class library has been compiled for programmers.

Welcome to the C/C ++ programming community forum, and interact with 3 million technical staff> This article details the establishment of a CdataBaseOperate class that uses ODBC to operate databases, the specific implementation of several major functions and the usage of this class in actual applications are given. We all know that the VC ++ MFC class library has been compiled for programmers.

Welcome to the C/C ++ programming Community Forum and interact with 3 million technical staff> enter

This article describes in detail the establishment of a CdataBaseOperate class that uses ODBC to operate databases, the implementation of several major functions, and the use of this class in actual applications.

As we all know, the VC ++ MFC class library has compiled database operation classes for programmers. programmers use the Wizard to create an application that connects to the database and operates on the database, no code is required, which provides a shortcut for programmers. However, when using the wizard, you can select a data source only for a Single-document or multi-document-based project and connect it to the specified database, the dialog box-based applications generated by the Wizard do not support databases. Even for single-document or multi-document-based applications, when special operations, such as opening a table, are required to return a set of records meeting certain conditions, MFC does not provide ready-made functions that fully meet the requirements. If you can use the database operations provided by MFC and add the functions you designed, that is to say, design a class for database operations and manually add this class to the program, you can perform database operations in a dialog box-based application. You can also design class functions based on the specific needs of your application, it provides great convenience for the implementation of specific functions.

---- In an application involving database operations, the commonly used MFC classes include CdaoDatabase, CdaoTableDef, CdaoRecordset, and CdaoQueryDef. When operating the database, you must first open the database, then open the tables in the database, and then obtain the query set and record set. Combine the operations of these four classes in your own defined classes and design a function to open the table to obtain the query set and record set. Later, when using this class in an application, you only need to include the header files of this class, the designed function can be called directly.

---- The database creation process can be divided into the following four steps:

---- 1. Define a CdataBaseOperate class without a base class

---- 1. Select the ClassView tab in the Workspace window, right-click the root of the tree structure chart, and select New Class ..., The create new class dialog box is displayed;

---- 2. Select Generic Class in Class type;

---- 3. enter the Name of the new class to be created in Name. The system will automatically create the header file and implementation file of the new class to start with the uppercase letter C, the file name is the class name. Remove the first uppercase letter C. To change the file name, click the change button.

---- 4. After Entering the items, press the OK button to confirm that a new class without a base class is successfully created, but it is still an empty class. In the next step, add content to the class.

---- 2. Add related definitions to custom classes

---- 1. In this application, ODBC is used to connect to the database of SQL SERVER. Therefore, before implementing the file constructor of the class, add the following definitions: # 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 used, you only need to change the connection with the required database, or reconfigure SQL-database to connect it to the new database. UID = sa; PWD = pass indicates that the user name used to log on to the database is sa, and the password is pass. If the password is empty, it indicates PWD = "".

---- 2. This class combines several types of databases provided by the MFC class library: CdaoDatabase, CdaoTableDef, CdaoRecordset, and CdaoQueryDef, the definition and implementation of these four classes are included in the header file afxdao. h. Therefore, you must add the statement in the header file of the newly defined class:

# Include <afxdao. h>;

---- 3. Declare an object for each of the four classes to be used as follows:

CDaoDatabase * loc_pDataBase;

CDaoTableDef * loc_pTable;

CDaoRecordset loc_pRecordset;

CDaoQueryDef * loc_pQueryDef;

---- The CdaoDatabase, CdaoTableDef, and CdaoQueryDef Classes define object pointers,

You must first use new and then delete. Take the CdaoDatabase class as an example. In the CdataBaseOperate class constructor, initialize the Object Pointer loc_pDataBase = new CDaoDatabase; In the destructor, release the pointer delete loc_pDataBase;

---- 3. Add the required functions and variables to the custom class

---- Adding a function manually involves two tasks. First, add the function declaration to the header file, and then add the specific implementation of the function to the implementation file. The declaration and implementation must be consistent;

---- Use the Wizard to add functions and variables:

---- 1. Select the ClassView tab in the Workspace window;

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: info-contact@alibabacloud.com 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.