Ado operation Database
A small project that was created some time ago involves connecting data with ado, which is now organized for backup.
Ado (ActiveX
Data object) is a database Access Technology released by MS, which is based on ole
The database technology, like odbc, accesses data through the middle layer. In terms of performance, it is not as fast as oci accessing oracle and other api methods, but in terms of ease of use, it does
Is very convenient. Of course, it is limited to applications on the window platform.
We generally use the encapsulated Ado library, because most applications use the existing encapsulated library. This article also uses the existing encapsulated library on the website. For more information about this class library, see the e Document of the codeproject Ado encapsulation library.
.
Basic Process
I borrowed some comments from netizens (one of VC ++ ADO development practices)
), Ado development mainly involves the following processes:
1. initialize the COM library and introduce the Ado definition library file.
In the VC environment, the initialization of the COM library is usually completed in the CWinApp: initInstance () function.
BOOL CWinApp: initInstance ()
{
AfxOleInit ();
}
We usually use the encapsulated Ado library, so the "Introduction of Ado library definition" is generally completed during encapsulation. When you view the encapsulated class library, you can see a line of code similar to this:
# Import "C:/Program Files/Common Files/System/ado/msado15.dll" rename ("EOF", "EndOfFile ")
# Import "C:/Program Files/Common Files/System/ado/MSJRO. DLL" no_namespace rename ("ReplicaTypeEnum", "_ ReplicaTypeEnum ")
This is defined in some cases. If the system cannot find the file during compilation, you need to change it to the above two lines. Of course, you need to write the correct path.
# Import <msado15.dll> rename ("EOF", "EndOfFile ")
# Import <MSJRO. DLL> no_namespace rename ("ReplicaTypeEnum", "_ ReplicaTypeEnum ")
2. Create a database connection object.
In an unencapsulated class library, the database connection object is _ ConnectionPtr. In the encapsulation class in this article, the database operation object is CADODatabase:
CADODatabase m_Db = new CADODatabase ();
CString conStr = "Provider = OraOLEDB. Oracle.1; Persist Security Info = True; Data Source = DAMIS ;";
M_pDb-> Open (conStr, "SCOTT", "rf0007 ");
ConStr is the connection string. According to different configurations of each database, the sample code is the string connecting to the Oracle database. The connection String of each database can be found in the ado connection string on codeproject.
.
The method for opening a database connection is Open (connectionString, user, pwd ). You can determine whether the open operation is successful Based on the returned value.
3. Execute the query and process the returned result set.
First, you must create a result set object and point it to the connection to the operation database. In this way, you can use the established database connection to operate the result set.
CADORecordset m_Rs = new CADORecordset (m_Db );
CString queryStr = "select * from analog ";
M_Rs.open (queryStr );
In this way, the result set for executing this SQL statement is saved in the m_Rs object. For details about how to access each record, refer to the article codeproject Ado encapsulation library e mentioned above.
The author gives a detailed description. Using this method to obtain the result set is relatively simple, but its functionality is not powerful enough. Another way is to create a CADOCommand class to operate SQL statements, for detailed operations, refer to the example file in this document.
4. Close the connection and release the object resource.
After resources are used up, release the database resources. There are two resources to be released: CADORecordset and CADODatabase. The release method is as follows:
M_Rs.delete ();
M_Db.close ();
Note:
Database connections are not the same. Before connecting, make sure that the local machine is connected to a remote database. For example, during the connection of Oracle data, the local machine needs to install the Oracle database client program and configure the connection information.
References
Codeproject Ado encapsulation library e
Ado connection string on codeproject
One of VC ++ ADO development practices