Visual c ++ ODBC

Source: Internet
Author: User

ODBC is a call-level interface that enables applicationsProgramAccess data in any database with ODBC drivers. You can use ODBC to create a database application that has the permission to access any database (the end user has the ODBC driver of the database. ODBC provides APIs that make your applications independent from the source database management system (DBMS.

ODBC is the database part in Microsoft Windows open service architecture (wosa. Wosa is an interface that allows Windows-based desktop applications to connect to multiple computing environments without rewriting applications for each platform.

The following are ODBC components:
    • ODBC API

      Function call library and errorCodeSet and the (SQL) syntax of the standard structured query language used to access data on the DBMS.

    • ODBC driver Manager

      Represents the dynamic link library (odbc32.dll) where the application loads the ODBC database driver ). This dll is transparent to your application.

    • ODBC database driver

      One or more DLL that processes the ODBC function calls of a specific DBMS.

    • ODBC cursor library

      Dynamic Link Library (odbccr32.dll) that resides between the ODBC driver manager and the driver and processes data scrolling ).

    • ODBC manager odbcad32.exe

      A tool used to configure DBMS to make it available as a data source for applications.

Applications are independent of DBMS by writing ODBC drivers for DBMS instead of directly using DBMS. The driver converts these calls to the commands that can be used by DBMS, which simplifies the work of developers and makes it available to a wide range of data sources.

The database class supports any data source with an ODBC driver. For example, this may include relational databases, index sequential access methods (isam) databases, Microsoft Excel workbooks, or text files. The ODBC driver manages the connection to the data source. SQL is used to select records from the database.

Open Database Connection (ODBC) provides a common interface for accessing different types of SQL databases. ODBC is based on the Structure Query Language (SQL) and serves as the standard for data access. This interface provides maximum interoperability: An application can access different SQL database management systems (DBMS) through a set of public code ). This allows developers to build and distribute a client/server application without targeting a specific DBMS. Then, the database driver is added to link the application to the user-selected DBMS.

The following features demonstrate ODBC flexibility:

• The application is not bound to a proprietary API.

• SQL statements can be explicitly included inSource codeIt can also be built in real time at runtime.

• Applications can ignore basic data communication protocols.

• Data can be conveniently sent and received in the application format.

• ODBC is designed along with the emerging international ISO call-level interface standards.

• ODBC database drivers available for 55 of the most popular Databases.

Most ODBC access to Visual C ++ is completed through MFC.

The Visual C ++ MFC class library defines several database classes, which are often used in the use of MFC programming. They are cdatabase (Database Class), crecordset (record set class), And crecordview (visual record set class ).
For the mfc odbc database class, the cdatabase class object represents a connection to the same data source, through which you can operate on the data source. The crecordset object represents the set of records selected from the data source, which is also known as the record set object.
The crecordset object is usually used in two forms: dynamic set (dynasets) and snapshot set (snapshots ). The dynamic set can be synchronized with changes made by other users, while the snapshot set is a static view of data. Each form provides a set of records when a record is opened. The difference is that when you scroll to a record in a dynamic set, changes made by other users or other record sets in the application are displayed accordingly. The crecordview class object can display database records in a controlled manner. This view is directly connected to the table view of a crecordset object.

You can use the MFC template class to add database support, such:

Data transmission (rfx) between data sources and record sets is implemented in the crecordset class)
Void cdatatestset: dofieldexchange (cfieldexchange * pfx) {pfx-> setfieldtype (cfieldexchange: outputcolumn); // macros such as rfx_text () and rfx_int () are dependent on the // type of the member variable, not the type of the field in the database. // ODBC will try to automatically convert the column value to the requested typerfx_long (pfx, _ T ("[student ID]"), m_stunum); rfx_text (pfx, _ T ("[name]"), m_name); rfx_text (pfx, _ T ("[gender]"), m_gender); rfx_long (pfx, _ T ("[age]"), m_age); rfx_long (pfx, _ T ("[class number]"), m_classnum );}
In the View class, dodataexchange implements the connection between controls and database fields:
Void cdatatestview: dodataexchange (cdataexchange * PDX) {crecordview: dodataexchange (PDX); // you can insert the ddx_field * function here to connect the control to the database field, for example, // configure (PDX, idc_myeditbox, m_pset-> m_szcolumn1, m_pset); // ddx_fieldcheck (PDX, idc_mycheckbox, m_pset-> m_bcolumn2, m_pset); // For more information, see the msdn and ODBC examples (PDX, idc_edit1, m_pset-> m_stunum, m_pset); values (PDX, idc_edit2, m_pset-> m_name, m_pset); ddx_fieldtext (PDX, idc_edit3, m_pset-> m_gender, m_pset); ddx_fieldtext (PDX, idc_edit4, m_pset-> m_age, m_pset); values (PDX, idc_edit5, m_pset-> m_classnum, m_pset );}
Implementation of adding records
Void cdatatestview: onadddata () {updatedata (true); // check whether the IF (! M_pset-> isopen () |! M_pset-> canappend () {return;} m_pset-> setfieldnull (null); // clear all fields m_pset-> addnew (); // set the record to the Add mode // set the new record value here a new dialog box should be displayed to set the new record, but the given record m_pset-> m_stunum = 20060003 is directly added here; m_pset-> m_name = l "zhangsan"; m_pset-> m_gender = l "male"; m_pset-> m_classnum = 3; m_pset-> m_age = 10; // submit the update if (! M_pset-> Update () {afxmessagebox (L "error: Record addition failed");} // re-create the record set m_pset-> requery (); m_pset-> movelast (); updatedata (false );}
Simple implementation of modifying records
 
Void cdatatestview: oneditdata () {// check whether the IF (m_pset-> canupdate () {m_pset-> edit () can be updated (); // set record to edit status if (! Updatedata () return; m_pset-> m_stunum = 20070001; m_pset-> Update (); updatedata (false );}}
Simple implementation of deleting records
 
Void cdatatestview: ondelete () {// check whether the IF (m_pset-> canupdate () {m_pset-> Delete () can be updated (); // mark it as deleting m_pset-> requery (); // re-set the record set m_pset-> movelast (); // move it to the last record updatedata (false ); // update display }}
Data Query

use the crecordset: open () and crecordset: requery () member functions to query records. Before using the crecordset class object, you must use the crecordset: open () function to obtain a valid record set. Once you have used the crecordset: open () function, you can apply the crecordset: requery () function when querying again. When calling the crecordset: open () function, if you have passed an opened cdatabase object pointer to the m_pdatabase member variable of the crecordset class object, use the database object to establish an ODBC connection; otherwise, if m_pdatabase is empty, create a cdatabase Class Object and connect it to the default data source, then initialize the crecordset class object. The default data source is obtained by the getdefaconnect connect () function. You can also provide the required SQL statement and use it to call the crecordset: open () function, for example:
m_set.open (afx_database_use_default, strsql );
if no parameter is specified, the program uses the default SQL statement to operate the SQL statements specified in the getdefasql SQL () function:
cstring ctestrecordset :: getdefasql SQL ()
{return _ T ("[basicdata], [mainsize]") ;}< br> for the name of the table returned by the getdefasql SQL () function, the corresponding default operation is the SELECT statement, namely:
select * From basicdata, mainsize

You can also use the member variables m_strfilter and m_strsort of the crecordset during the query to execute conditional query and result sorting. M_strfilter is a filter string that stores the where condition strings in the SQL statement; m_strsort is a Sort string that stores the orderby character strings in the SQL statement. For example:
M_set.m_strfilter = "type = 'motor '";
M_set.m_strsort = "voltage ";
M_set.requery ();
The corresponding SQL statement is:
Select * From basicdata, mainsize
Where type = 'motor'
Order by voltage

Parameterized record set

Using parameterized record sets can speed up program execution and achieve dynamic functions.

Parameterization makes conditional query tasks more intuitive and convenient. To use parameterization, follow these steps:
(1). Declare the parameters:
Cstring P1;
Float P2;
(2) initialize the parameters in the constructor.
P1 = _ T ("");
P2 = 0.0f;
M_nparams = 2;
(3) bind the variable to the corresponding column
Pfx-> setfieldtype (cfieldexchange: Param)
Rfx_text (pfx, _ T ("p1"), P1 );
Rfx_single (pfx, _ T ("p2"), P2 );
After completing the preceding steps, you can use the parameters for conditional query:
M_pset-> m_strfilter = "type =? Andvoltage =? ";
M_pset-> P1 = "Motor ";
M_pset-> P2 = 60.0;
M_pset-> requery ();
The value of the Parameter Variable replaces "?" In the query string in the order of binding. Adapter.
If the query results contain multiple records, you can use the crecordset functions to move (), movenext (), moveprev (), movefirst (), and movelast () to move the cursor.

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.