Database Access Technology-ODBC

Source: Internet
Author: User
Tags driver manager

Before specific applications, let's take a look at the working principles of ODBC database access technology:

To access a database, an application must first register a data source using the ODBC Manager. The manager registers a data source based on the database location, database type, ODBC driver, and other information provided by the data source, establish a connection between ODBC and a specific database. In this way, as long as the application provides the data source name to ODBC, ODBC can establish a connection with the corresponding database. However, you must note that in ODBC, ODBC APIs cannot directly access the database and must exchange information with the database through the driver manager. The driver manager is responsible
The API call is passed to the correct driver, and the driver returns the result to the application through the driver manager after performing the corresponding operation.

Next we will prepare for the demo:

1. Use Access to create an odbcdemo database. MDB and studentinfo tables (note that the ID is set as the primary key) include: Student ID, name, sex, age, and major) and the address field, add several records by the way, save and exit.
2. Use the control panel-management tools-Data Source (ODBC) to add data sources;

Procedure: click Add. In the displayed create data source dialog box, select Microsoft Access Driver (*. MDB), click Finish. In the displayed ODBC Microsoft Access installation dialog box, select database odbcdemo. MDB; and set the data source name DSN. Here we set it to dsn_odbcdemo. Of course, you can also click the Advanced button to set the user name and password for the database. Wait until all settings are complete

Click OK to add the data source. In this case, you will find that dsn_odbcdemo already exists in the user data source list.

3. Next we will start writing our demo.

Procedure:

1. Create a dialog box-based application. Note: You can select Winsock for server authentication;
2. Create a derived class crecordsetchild Based on the crecordset class, specify the data source in the database Options dialog box, select dsn_odbcdemo in ODBC, and select Dynamic dynaset in recordset type. Click OK. In the displayed dialog box, select the table studentinfo to exit;
3. Add the header file # include <afxdb. h> to stdafx. h.
4. Add member variables in * DLG class

Public: cdatabase m_db; // database object crecordsetchild * m_pset; // record set pointer

5. Add the header file # include "recordsetchild. H" to * DLG. h"
6. Add code to the oninitdialog () function

Cstring DSN = "DSN = dsn_odbcdemo; DBQ = odbcdemo. mdb "; // note: the database is located in the folder where the project is located. If your database is located elsewhere, modify the database path cstring DSN =" DSN = dsn_odbcdemo; DBQ = odbcdemo. mdb "; m_db.openex (DSN); m_pset = new crecordsetchild (& m_db); cstring SQL =" select * From studentinfo "; m_pset-> open (crecordset: dynaset, SQL ); // put the query results into the record set according to the SELECT statement. Dynamic.

7. to display the information on the interface, you need to add 6 controls (edit box) to the interface and associate them with six variables: m_edit_name m_edit_id m_edit_sex m_edit_age m_edit_profession.
M_edit_address, and add the code in the oninitdialog () function:

M_edit_name = m_pset-> m_name; m_edit_sex = m_pset-> m_sex; m_edit_age.format ("% d", m_pset-> m_age); m_edit_id = m_pset-> m_id; m_edit_profession = m_pset-> m_profession; m_edit_address = m_pset-> m_address; updatedata (false); // update to display it on the page


After compilation and running, you can see the information read from the database.
9. view the record information
Add 6 button controls and add the following code:

Void codbcdemodlg: onbuttonadd () // Add record {// todo: add your control notification handler code herem_pset-> addnew (); updatedata (true ); m_pset-> m_name = m_edit_name; m_pset-> m_age = atoi (lpctstr) m_edit_age); m_pset-> m_sex = m_edit_sex; m_pset-> m_id = m_edit_id; m_pset-> m_sion sion = m_edit_profession; m_pset-> m_address = m_edit_address; m_pset-> Update (); m_pset-> movelast ();} void codbcdemodlg: onbuttond Elete () // Delete record {// todo: add your control notification handler code herem_pset-> Delete (); m_edit_name = ""; m_edit_age = ""; m_edit_sex = ""; m_edit_id = ""; m_edit_sion sion = ""; m_edit_address = ""; updatedata (false);} void codbcdemodlg: onbuttonupdate () // modify record {// todo: add your control notification handler code herem_pset-> edit (); updatedata (true); m_pset-> m_name = m_edit_name; m_pset-> m_age = atoi (LP Ctstr) m_edit_age); m_pset-> m_sex = m_edit_sex; m_pset-> m_id = m_edit_id; m_pset-> m_sion sion = m_edit_profession; m_pset-> m_address = m_edit_address; m_pset-> Update ();} void codbcdemodlg: onbuttonfind () // view {// todo: add your control notification handler code hereupdatedata (true ); m_pset-> close (); // Note: Because the SQL statements used for query are different, you must first disable cstring SQL for the previously opened record set when querying again; SQL. format ("select * From studentinfo where name = '% S' ", m_edit_name); m_pset-> open (crecordset: dynaset, SQL); If (m_pset! = NULL) {m_edit_name = m_pset-> m_name; m_edit_sex = m_pset-> m_sex; m_edit_age.format ("% d", m_pset-> m_age); m_edit_id = m_pset-> m_id; m_edit_profession = m_pset-> m_profession; m_edit_address = m_pset-> m_address; updatedata (false) ;}} void codbcdemodlg: onbuttonfirst () // view the first record {// todo: add your control notification handler code herem_pset-> movefirst (); m_edit_name = m_pset-> m_name; m_edit_sex = m_pset-> m_sex; m_edit_age.format ("% d", m_pset-> m_age ); m_edit_id = m_pset-> m_id; m_edit_sion sion = m_pset-> m_profession; m_edit_address = m_pset-> m_address; updatedata (false);} void codbcdemodlg: onbuttonprevious () // view the previous record {// todo: add your control notification handler code herem_pset-> moveprev (); If (m_pset-> isbof ()) {m_pset-> movefirst ();} m_edit_name = m_pset-> m_name; m_edit_sex = m_pset-> m_sex; m_edit_age.format ("% d", m_pset-> m_age ); m_edit_id = m_pset-> m_id; m_edit_sion sion = m_pset-> m_profession; m_edit_address = m_pset-> m_address; updatedata (false);} void codbcdemodlg: onbuttonnext () // view the next small record {// todo: add your control notification handler code herem_pset-> movenext (); If (m_pset-> iseof ()) {m_pset-> movelast ();} m_edit_name = m_pset-> m_name; m_edit_sex = m_pset-> m_sex; m_edit_age.format ("% d", m_pset-> m_age ); // convert int to cstringm_edit_id = m_pset-> m_id; m_edit_sion sion = m_pset-> m_sion Sion; m_edit_address = m_pset-> m_address; updatedata (false);} void identifier: onbuttonlast () // view the last record {// todo: add your control notification handler code herem_pset-> movelast (); m_edit_name = m_pset-> m_name; m_edit_sex = m_pset-> m_sex; m_edit_age.format ("% d", m_pset-> m_age); m_edit_id = m_pset-> m_id; m_edit_sion sion = m_pset-> m_profession; m_edit_address = m_pset-> m_address; updatedata (false );}

Now, our demo is compiled. The specific instance code can be downloaded at the following address:

Http://download.csdn.net/detail/jhg1204/5252298

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.