Using ADO Development Database Application in VC 1. Introducing an ADO library file Using ADO to use ADO to introduce Symbol #import to introduce the ADO library file in the STDAFX.H file to enable the compiler to be correctly compiled. The code is as follows: #import "c: / program files / common files / system / ado / msado15.dll" No-namespaces rename "Declaration of ADO in the project, but not using ADO Namespace, and rename EOF to AdoEOF in order to avoid conflicts.
2. Initializing the OLE / COM library environment must be noted that the ADO library is a set of COM dynamic libraries, which means that the application must initialize the OLE / COM library environment before calling ADO. In the MFC application, a better way is to initialize the OLE / COM library environment in the initInstance member function of the application primary class.
// Initialize the OLE / COM library environment
Bool CadoApp :: InitInstance () {if (! Afxoleinit ()) {AFXMessageBox ("OLE initialization!"); Return false;} ...
Function Afxolein initializes the OLE / COM library environment at each application starts.
3. ado interface Introduction ADO library contains three basic interfaces: __ connectionPTR interface, __ commandptr interface, __ recordsetptr interface,
__ConnectionPTR interface returns a recordset or an empty pointer. It is usually used to create a data connection or perform a SQL statement that does not return any result, such as a stored procedure. Returning a recordset with the __connectionptr interface is not a good way of use. Usually, like CDATABASE, use it to create a data connection, then use other objects to perform data input output operations.
__Commandptr The interface returns a recordset. It provides a simple way to perform the stored procedures and SQL statements that returns the record set. When using the __commandptr interface, you can use the global __connectionptr interface, or you can use the connection string directly in the __commandptr interface. If only one or more data access is performed, the latter is a better choice. But if you want to access the database frequently and return a lot of record sets, you should use the global __connectionptr interface to create a data connection, then use the __commandptr interface to perform the stored procedure and SQL statement.
__Recordsetptr is a recordset object. Compared with the above two objects, it provides more control functions to records, such as record lock, cursor control, and the like. Like the __commandptr interface, it does not have to use a created data connection, you can use a connection string instead of the Connection member variable assigned to __recordsetptr, let it create data connections yourself. If you want to use multiple record sets, the best way is to use the global-ConnectionPTR interface that has created a data connection with the Command object, and then use the __recordsetptr to perform the stored procedure and SQL statements.
4. Use the __connectionptr interface
__Connectionptr is a connection interface that is similar to CDatabase and CDAODATABASE. First create a __connectionPTR interface instance, then point to and open an ODBC data source or OLE DB data provider (Provider). The following code creates a DSN and non-DSN-based data connection.
// 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);
5. Use the __recordsetptr interface __recordsetPTR interface to use the use of CDAODatabase, through the following code, you will find that the use -recordsetptr port is very simple (the following code uses the data that has been created above):
// Execute SQL statements using CDAODatabase
CDAORECORDSET MySet = New CDAORECORDSET (MYDB);
MySet-> Open (AFX__DAO__USE__DEFAULT__TYPE, "SELECT from t__samp");
Now Using ADO:
// Use __recordsetptr to execute SQL statements
__Recordsetptr myset;
MySet.createInstance (__ uuidof (recordset));
MySet-> Open ("SELECT from some__table",
Mydb.getInterfacePtr (), AdoPENDYNAMIC, AdLockOptimistic, AdcmdText);
Now we already have a data connection and a recordset, and you can use data. As can be seen from the following code, using the __recordsetptr interface, it is not necessary to frequently use the large and complex data structure Variant like Dao, and forced to convert various data types, this is also one of the advantages of ADO. Assume that the program has a listbox control named m__list. The following code we use the __recordsetPtr interface to get recordset data and populate this ListBox control:
// Access data using ADO
__variant__t Holder
Try {while (! myset-> adoEof)
{Holder = MySet-> getCollect ("Field__1");
IF (Holder.Vt! = vt__null)
m__list.addstring ((char ) __ bstr__t (Holder);
MySet-> MoveNext ();}}
Catch (__ com__error e)
{Cstring error = e-> errorMessage ();
AfxMessageBox (E-> ErrorMessage ());
} catch (...)
{MessageBox ("ADO error!");
You must always use TRY and CATCH to capture an ADO error in your code, otherwise the ADO error will make your application crash. When the ADO runs an error (such as the database does not exist), the OLE DB data provider will automatically create a __com_rm_rror object and populate the error message to the member variable of this object.
6. Use the __commandptr interface __commandptr interface to return a RecordSet object, and provide more recordset control functions, the following code example uses the __commandptr interface method: // Using the __commandptr interface to get data
__Commandptr pcommand;
__Recordsetptr myset;
PCOMMAND.CREATEINSTANCE (__ uuidof (command));
PCOMMAND-> ActiveConnection = MYDB;
PCOMMAND-> CommandText = "SELECT from Some-Table";
PCOMMAND-> CommandType = AdcmdText;
PCOMMAND-> Parameters-> refresh ();
MySet = pCommand-> Execute (NULL, NULL, AdcmDunknown);
__variant__t thevalue = MySet-> getCollect ("Field__1");
CString Svalue = (CHAR ) __ bstr__t (thevalue);
7. About data type conversion Since the COM object is a cross-platform, it uses a generic method to handle various types of data, so the CSTRING class and COM object are incompatible, we need a set of API to convert COM Objects and C types of data. __vatiant__t and __BSTR_T are the two objects. They provide a general method to convert COM objects and C types of data.