Check Email
C++ Tutorial
Tutorials collection
Ask the Expert
All articles
Sourcode Library
Win programing
C++ Builder
Unix programming
Unix downloads
CGI programming
CGI Scripts
Mailing Lists
Message board
C++ Groups
C++ Newsgroups
Contact us
submit a resource
Link to us
Home Articles
Connecting to an Ms Access Database via ODBC with C/C++

Connecting to an MS Access Database via ODBC with C/C++ and connecting an MS Access Database to the Internet
By Mark Lee-Dell


This paper is a quick guide to connecting C/C++ to an ODBC database and an ODBC database to the Internet. It makes the assumption that the reader has a reasonable grasp of programming with the C/C++ language and of how to use basic SQL commands.


This paper was produced because I have just completed my final year project that involved interfacing between C/C++ and ODBC and ODBC and the Internet and finding documentation on how it could be done was very difficult. The aim of this paper is to provide people with a single location that has all the information that is required to carry out this task.


Before we start there are several things, which we need to ensure. Firstly I was trying to achieve this communication between MS Access 97 and MS Visual C++ 5.0; they were both running under the Windows 98 operating system on a Pentium Pro PC. I was also using MS Personal Web server.

Make sure that all of the above mentioned software has been installed and is configured correctly.

Now we have to create an ODBC data-source, which points to your MS Access database. Double click the "32bit ODBC" icon in Control Panel to start the ODBC administrator. Next click the "Add" button to create a new data-source, select the driver you wish to use (if your database is an MS Access database select the "Microsoft Access Driver" and so on) and when you have done this click the "finish" button. In the next box you need to enter a name for the data source in the "Data Source Name" field, a description can also be entered in the "Description" field but this is optional. Next you need to click the "Select" button from the "Database" field. A browser box appears where you select the location of your MS Access database file. When done click the "OK" button to return back to the previous screen and "OK" again to complete the creation of the data source. When this has been completed click the "OK" button to quit the ODBC administrator.

Connecting C/C++ to the ODBC data source

We are now ready to produce a C program that will open a connection to the database and perform an SQL query. The SQL query can be any SQL statement so it can be used to search, insert, update or delete data in the database. This program will not do any validation of whether or not a record with the same value already exists in the database table and it will not do any validation on whether the SQL string is a legal SQL statement.

The following is a copy of the necessary code required to connect to the ODBC data source and must be run on the same machine that the ODBC database is running on. A set of C functions are supplied with ODBC and are found in the two header files sql.h and sqlext.h both of which must be included in the program using the "#include" operation.

Click here to view the code

This code should connect to the ODBC data source "db97" and perform an SQL query. I have not included any of the error handling that can be used when updating the table or opening and closing the connection to the database, this can be done at your discretion. Remember that when you use this code the name of the data source will need to be changed and the database may require a user ID and password.

Connecting ODBC to the Internet

The important thing to remember when doing this is that the Web server has to be configured and set up on the same machine as the MS Access database and ODBC.

Simply compile the program above and copy the executable produced into the cgi-bin directory of the web server. Using MS Personal Web Server the absolute pathname on my installation was c:\inetpub\wwwroot\cgi-bin.

Books I Recommend

Windows 95 Multimedia and ODBC API bible by R.J Simon 1996

This is a really useful book as it contains full descriptions of all of the ODBC functions along with parameter lists, return codes, descriptions and even has code examples.

Special Edition - Using CGI by J.Dwight and M.Erwin 1996. Published by Que Corporation. ISBN 0-7897-0740-3. Price 29-95.

If you intend to dynamically build an SQL string by getting details from a HTML form, then this book is a useful resource for you. It also contains chapters on specific programming languages and database connectivity.

Links that you may find useful

Link to the Pervasive Software site who have many useful manuals and references including an ODBC developers guide.

Microsoft's Data Access Components web page, which includes information on ODBC, OLE and others.

Email Login
sign up!
Free Online tutorial & courses
Join Our Message board Discussions on C/C++
Unix Network
 Join Our Mailing list