VC accesses the DB2 database through ODBC/CLI | creates a data source

Source: Internet
Author: User
Tags db2 express db2 odbc driver dsn ibm db2 ibm db2 database ibm db2 odbc driver

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://

2): The driver is automatically installed after the IBM DB2 database is directly installed. Database ajzr_0m973fvbnzx63ecwwrnyaoisx2bvox3ehc/ /XF. LPR. d1vk/xg.4854742/XI. swg-db2expresscviper2/XY. regsrvs/xz. c7tz8b_fn0_ruc3stdvpbwy2qpi/

The following shows how to create a DSN and connect to the DB2 database.

The DB2 database information is as follows:


Port 50000

Username: Admin

Password: Admin

Database Name: db2test

Set DSN to outbound


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, 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,

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.

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: 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.