ADO (ActiveX Data Object) is a new interface for the development of Microsoft database applications, a high-level database access technology built on OLE DB, even if you don't understand OLE db,com, it's easy to deal with ADO because it's very easy to use, It's even easier to use than the ODBC APIs, DAO, and RDO you've been exposed to before, without losing the flexibility. This article describes in detail how to use ADO in the Visual C + + development environment for database application development and to produce sample code. To allow readers to test the code provided by this example, we use an Access database, which you can find directly in the sample code we provide. The effect of the program's compilation run as shown in figure
Figure I, ADO operation acess Database Interface effect diagram
One, the realization method
Everything is difficult at the beginning, any new technology for beginners is the most important or "getting started" to grasp the main points. Let's look at the basic process of ADO database development! Its basic steps are as follows:
(1) Initialization of the COM library, the introduction of the ADO library definition file
(2) connecting the database with the Connection object
(3) using the established connection, execute the SQL command through connection, command object, or use the Recordset object to obtain the result recordset for querying and processing.
(4) After use, close the connection to release the object.
Below we will explain the above steps in detail and give the relevant code.
1, the initialization of the COM library
We can initialize the COM library using AfxOleInit (), which is usually done in an overloaded function of cwinapp::initinstance (), see the following code:
BOOL CADOTest1App::InitInstance()
{
AfxOleInit();
......
}
2. Using #import instruction to introduce ADO type library
In order to introduce the ADO type library, you need to include the following statement in the project's StdAfx.h file:
#import "c:\program files\common files\system\ado\msado15.dll"
no_namespace rename("EOF","adoEOF")
What is the effect of this statement? The final effect is similar to the #include we already know very well, when compiling the system will generate Msado15.tlh,ado15.tli two C + + header files to define the ADO library.
Readers need to note that the Msado15.dll in your development environment is not necessarily in this directory, please change according to the actual situation, the following warning may appear at compile time, this Microsoft explained in MSDN, and advised us to ignore this warning: msado15.tlh (405): Warning C4146:unary minus operator applied to unsigned type, result still unsigned.