An easy to use ODBC database class CMYODBC

Source: Internet
Author: User
Tags bind odbc knowledge base

First, the introduction

The CRecordset class of MFC is not very useful, because if we want to use it, we must derive a new class from the CRecordset class for each query, or make a dynamic data exchange. In the VC knowledge Base in the sixth period, there is an introduction to "use CRecordset alone" article, but the above CRecordset open mode can only use crecordset::forwardonly, cursors can only roll forward, and in this way, You can't get the number of columns from the open recordset to get this query. Once in the application, I had to catch the exception of CRecordset::GetFieldValue () to get the results of the query how many columns. For ease of use, I wrote a database class Cmyodbc, which is written with an ODBC API that supports various SQL statements and supports transaction processing. The best part of it is the dynamic binding of the query's Recordset, which is implemented through class Codbcset. To illustrate, these two classes can be said to be relatively simple, two classes of small amount of code, so suggest interested friends to look at its code, the following figure is the code to run the effect of the diagram:

Second, the principle

In fact, whether using an ODBC API or using Oracle's OCI (a friend interested in Oracle's OCI, welcome to explore, OCI is powerful, supports dynamic binding, supports Pl/sql, and its class encapsulation and CMYODBC are very similar, using it instead of ODBC programming To solve the problem of ODBC efficiency, the process is similar, generally divided into the following procedures:

1 initialization of the work environment

2 Connection Data source

3 Manipulating Data sources

4 Retrieving result sets

5 Update the result set

6 Transaction Processing

7 Disconnect, releasing various handles

We all know that when using CDatabase, if you want to execute a SELECT statement, then you have to CRecordset to retrieve the result set, and the CRecordset class wants us to first select the table and so on to bind first, so we use the time is very inconvenient, We don't need to do this at all, and we don't need to know that this is an SQL statement about the table on that database, because after SQLExecDirect (), you can call Sqlnumresultcols (), SQLColAttribute ( Functions such as how many columns the result set is executed, the field name of each column, the type of the column, and so on, and then dynamically allocate memory based on the type, then bind with the memory, and finally SQLFetch () to get the result set. In CMYODBC this class is to use the Codbcset class to implement the principle of automatic binding.

Here's how to use the class Cmyodbc:

1 joins the database by calling the Connectdb (const char *cpservername, const char *cpusername, const char *cppassword) function. The parameter meanings are as follows:

Cpservername-----ODBC Data Source Name

Cpusername-------User Name

Cppassword-----Password

2 by calling the Exesqldirect (const char *cpsqlstmt) function, you can execute statements that manipulate the data source, such as modifying, deleting statements, and so on. The parameters are as follows: CPSQLSTMT------The SQL statement you want to execute, such as delete from EMP where Deptno < 20

3 If you want to perform a transaction, call the Exectrans (CStringList &strsqllist) function, where the parameters strsqllist represent a series of SQL statements you want to execute.

4 If you want to execute the SELECT statement, use the following procedure:

I declare a Codbcset object, such as Codbcset RSet;

II Then call function preparesql (const char *cpsql, Codbcset &rset), where the meaning of the parameters is as follows:

Cpsql----Represents the SELECT statement to execute

RSet-----Represents a codbcset reference that you want to pass on the object declared above.

III Call the Fetchdata () function to get the result set.

5 Disconnects the data source by calling the function disconnect ().

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.