Some time ago, I made a small tool for information synchronization, and used VC database programming. Writing this log should be a simple note. It can also be used as a reference for beginners.
This article describes how VC connects to the DB2 database through the ODBC/CLI driver and lists someCodeAnd has the following functions:
1. Establish a connection;
2. Execute SQL statements;
3. Search for the list of tables in the database and the structure of the tables in the database. The table structure includes the name, data type, length, and whether the fields can be empty.
1. Configure the development environment
Before starting, we must set up the development environment. The development environment I used is VC 6.0 + DB2 express-C 9.7. Download and install DB2 express-C 9.7 free of charge from the IBM website, and start creating a table for testing in the DB2 database. After the installation is complete, create a data source driver for DB2, go to "Control Panel" = "Administrative Tools" = "Data Source", and add the data source for DB2 in data source management.
If you want to implementProgramTo dynamically Add a data source, you can call the sqlconfigdatasource function for configuration. For example, sqlconfigdatasource (null, odbc_add_dsn, "IBM DB2 ODBC driver", "DSN = DB2 \ 0" "uid = db2admin \ 0" "dbalias = mydbname ");
2. Create and configure a project
Create a project in VC and configure the directory that contains files and reference libraries.
Click "tool" = "option" = "directory" of vc6.0 to add the include directory and lib directory of DB2 to "include files" and "librery Files" respectively, and move them to the top;
Next, we have to configure our project to support ODBC by entering the link tab of the project configuration and adding odbc32.lib in the modules bar of Object/library.
====================================
First install the IBM DB2 ODBC driver
1): You can download the DB2 run-time client (about 86.6 MB) separately. After installation, the ODBC driver is successfully installed. : Ftp://ftp.software.ibm.com/ps/products/DB2/fixes2/English-US/db2winia32v8/Fixpak/fp17a_wr21440/fp17a_wr21440_rtcl.exe
2): The driver is automatically installed after the IBM DB2 database is directly installed. Database http://www6.software.ibm.com/sdfdl/v2/regs2/db2pmopn/Express-C/db2expressc9/xa.2/XB. ajzr_0m973fvbnzx63ecwwrnyaoisx2bvox3ehc/xc.db2exc_952_win_x86.zip/XD. /XF. LPR. d1vk/xg.4854742/XI. swg-db2expresscviper2/XY. regsrvs/xz. c7tz8b_fn0_ruc3stdvpbwy2qpi/db2exc_952_win_x86.zip
The following shows how to create a DSN and connect to the DB2 database.
The DB2 database information is as follows:
IP: 192.168.1.46,
Port 50000
Username: Admin
Password: Admin
Database Name: db2test
Set DSN to outbound
Steps:
1.1 open the ODBC data source Manager
1.2 create a data source
1.3 Add a data source name
1.4 ODBC settings
1.4.1 user name and password
1.4.2 enter host name and port
1.1 open the ODBC data source Manager
Click Add To Go To The create Data Source Page. :
1.2 select "IDB DB2 ODBC driver" and click "finish" to go to the data source name setting page.
1.3 Add a data source name
Add a data source name, such as Outbound. Select a "database alias" from the drop-down list ". If not, click "add" and enter the text in 1.3odbc settings.-To skip. If yes, click OK to return to the ODBC data source Manager page. Double-click "Configure", enter the user name and password, and check "Save Password". The connection is successfully tested.
1.4 ODBC settings
For example, to connect to the database db2test on 192.168.1.46, the username and password of the database are admin, and the following configuration is required,
1.4.1 user name and password
Enter the username and password of the database to connect to, for example, admin and Admin.
1.4.2 enter host name and port
Enter the Host IP address, that is, 192.168.1.46.
Port 50000
Database Name, that is, db2test
Alias: You can create a name that is not repeated in the drop-down list.
Click OK to return to the following page.
Select the outbound data source name,
Open the "configuration" button,
Click "test" to bring up the "connection successful" dialog box. The configuration is complete.