UseADO
1 Overview
ADO is an ActiveX Data Object, which is a Microsoft development database.
The new object-oriented interface of the application.ADO accesses the database by accessing the ole db data provider
Provides a pairThe simple high-level access interface of the ole db data provider.
ADO technology simplifies ole db operations. ole db programs use a large number of COM interfaces, while ADO
These interfaces are installed. So,ADO is a high-level access technology.
The ADO technology is based on the universal Object Model (COM). It provides access technology in multiple languages. At the same time,
BecauseADO provides an automatic access interface, so ADO can use the script language described to access
VBScript, VCScript, etc.
2. Use ADO in VC
AvailableActiveX controls provided by VC6 can be used to develop applications and ADO objects.
UseADO object development applications allow program developers to easily control access to the database, from
And generate database access programs that meet user needs.
UseSimilar to other technologies, the ADO object development application must be connected to the data source.
But unlike other access technologies,The hierarchical and sequential relationship between objects based on ADO
The requirements are not too strict. In the process of program development, it is not required to establish a connection before a record can be generated.
Objects. You can directly use the record object where the record is used. When creating the record object,
The program automatically establishes a connection with the data source. This model effectively simplifies the program design and enhances the process
Ordering flexibility. The following describes how to useThe method of Program Design for ADO objects.
21. Introduce the ADO Library File
UseBefore ADO, you must use the direct introduction symbol in the stdafx. h file of the Project # import the ADO Library File
To enable the compiler to compile correctly. The Code is as follows:
# Define INITGUID
# Import "c: program filescommon filessystemadomsado15.dll" no_namespace rename ("EOF", "EndOfFile ")
# Include "icrsint. h"
This line of statement declaration is used in the projectADO, but do not use the namespace of ADO, and to avoid
Conflict, SetThe name of EOF is EndOfFile.
22. initialize the ADO Environment
In useBefore the ADO object, you must initialize the COM environment. You can use the following code to initialize the COM environment:
: CoInitialize (NULL );
During initializationAfter the COM environment, you can use the ADO object. If this generation is not added before the program
Will generateCOM error.
After useAfter the ADO object, you need to use the following code to release the initialized object:
: CoUninitialize ();
This function is clearedThe COM Environment prepared by the ADO object.
23 interface Introduction
The ADO library contains three basic interfaces:
_ ConnectionPtr interface,
_ CommandPtr interface,
_ RecordsetPtr interface,
The _ ConnectionPtr interface returns a record set or a null pointer. It is usually used to create
Connect data or execute a data connection that does not return any resultsAn SQL statement, such as a stored procedure.
Use_ ConnectionPtr is not a good method to return a record set. Usually
SameLike CDatabase, use it to create a data connection and then use the number of executions of other objects
Data input and output operations.
The _ CommandPtr interface returns a record set. It provides a simple method to execute returned records.
Set stored procedures andSQL statement. When using the _ CommandPtr interface, you can use
BureauThe _ ConnectionPtr interface can also be used directly in the _ CommandPtr interface. For example
If only one or several data access operations are performed, the latter is a good choice. However, if frequent access is required
Database, and to return many Record Sets, you should use global_ ConnectionPtr Interface
Create a data connection and then useThe _ CommandPtr interface executes stored procedures and SQL statements.
_ RecordsetPtr is a record set object. Compared with the above two types of objects, it provides
More control functions, such as record lock and cursor control. SameThe _ CommandPtr interface is the same, it does not
You must use a created data connection. You can use a connection string to replace the connection pointer.
ToThe connection Member variable of _ RecordsetPtr allows it to create its own data connection. If you want
The best way to use multiple record sets is the sameThe Command object uses all the data connections that have been created
Bureau-ConnectionPtr interface, and then use _ Recordse7tPtr to execute stored procedures and SQL statements.
24. use ADO to access the database
_ ConnectionPtr is a connection interface. First, create a _ ConnectionPtr interface instance,
Next, point to and openODBC data source or ole db data Provider (Provider ). The following code points:
Do not createData Connection Between DSN and non-DSN.
// Use _ ConnectionPtr (based on DSN)
_ ConnectionPtr MyDb;
MyDb. CreateInstance (_ uuidof (Connection ));
MyDb-> Open ("DSN = samp; UID = admin; PWD = admin", "", ",-1 );
// Use-ConnectionPtr (based on non-DSN)
_ ConnectionPtr MyDb;
MyDb. CreateInstance (_ uuidof (Connection ));
MyDb. Open ("Provider = SQLOLEDB; SERVER = server; DATABASE = samp; UID = admin; PWD = admin", "", "",-1 );
// Use _ RecordsetPtr to execute an SQL statement
_ RecordsetPtr MySet;