|
Location: Desktop development - C/C++ License: The Intelliproject Open License (IPOL) Connecting to a database from C++ using ADOPosted by Silviu CarageaProvides an easy technique to access ADO objects from C++ |
Skill: AdvancedPosted: 12/11/2008Views: 1699Rating: 5.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
Microsoft's ActiveX Data Objects (ADO) is a set of Component Object Model (COM) objects for accessing data sources. It provides a layer between programming languages and OLE DB, an easy way to data access and manipulation that is independent of data stores, tools, and languages.

The best way to use ADO in C++ is to import ADO type libraries. The list of type libraries available can be found in most cases at the following path:
C:\Program Files\Common Files\System\ado
On my system the list includes the following type libraries:
I this tutorial I had decided to use the last version of ADO type library (msado27.tlb).
You can import this type library by using the #import directive:
#import "c:\program files\common files\system\ado\msado27.tlb"
no_namespace rename( "EOF", "A_EOF" )
If we don't specify the no_namespace option, the directive generates a type library header (msado27.tlh) that contains typedef declarations, smart pointers for interfaces, and enumerators under the ADODB namespace.
rename( "EOF", "A_EOF" ) tells the preprocessor to replace the EOF constants by A_EOF.
Before using any ADO objects we should initialize the COM library by calling CoInitialize or CoInitializeEx:
Let’s begin by creating a connection object.
And now let’s try to make the database connection using the ADO connection object created above.
Now let’s put the above source code in a single function which can be used to create a database connection.
For example let’s say we want to add a new record to our database. We can use the following function:
In the same mode we can execute different SQL queries which not return any results, like UPDATE, DELETE, etc.

First we have to create a function which will return the value for a specified column in the recordset.
And now let’s create a function which iterate over the recordset and save all the records in a vector container.
After we finish with the database we should close the connection and release the resources allocated for COM.
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| Silviu Caragea
| Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject. He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy. His programming experience includes: - C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL) - Open Source libraries :CURL & Boost - HTML, CSS - Java (SE,ME) - JavaScript, Ajax, Google Web Toolkit (GWT) - Php, MySQL -Oracle, PL SQL - C# .NET -Objective C, IPhone SDK, Cocoa Location: |
Sign up to post message on the article message board!