ADO database programming with Visual C author: Jiang Dongyu
The ActiveX Data Object (ADO) is the high-level database API on the OLE DB. We can also call ADO in the C program. This article will make a small example in the VC 6.0 environment explains how to use ADO.
1. Generate an application framework and initialize the OLE / COM library environment
Create a standard MFC AppWizard (EXE) application, then initialize the OLE / COM library in the initInstance function of the application class (because the ADO library is a COM DLL library).
Bool CadotestApp :: InitInstance ()
{// Initializing the OLE / COM library environment
Afxoleinit ();
2. Introduce the ADO library file
Before using ADO, you must introduce the STDAFX.H file in the project to introduce the ADO library file to enable the compiler to compile correctly. code show as below:
#Include
#Import "c: / program files / compon files / system / ado / msado15.dll"
NO_NAMESPACE
Rename ("EOF", "Adoeof")
Header files Comdef.h allows our applications to use some of the special COM support classes in Visual C , which makes it easier to handle OLE autonomy, and OLE autonomy is the data type used by ADO. The latter line uses the #import instruction to enter the ADO class library definition in our application.
The definition of the ADO class is to be stored in ADO DLL (MSADO15.DLL) as a resource in its inside. The type library describes the autonomous interface, and the COM VTable interface used by C . When using the #import command, Visual C needs to be read from the ADO DLL when running, and create a set of C header files. These header files have .TLI and .TLH extensions, readers can find these two files in the project's directory. The ADO class called in the C program code is defined in these files.
The third line of the program indicates that the ADO object does not use the namespace. In some applications, naming conflicts may occur due to objects in the application in the application, there is a need to use the namespace. If you want to use a namespace, you can modify the third line program to: Rename_NameSpace ("Adons"). The fourth line of code is renamed the EOF in the ADO (end) to AdoEOF to avoid conflicts with other library defined for your EOF.
3. Database operation using intelligent pointers
Define two ADO intelligence pointer instances in the CaboutDLG header and add a listCtrl in the dialog.
_ConnectionPTR m_PConnection;
_RecordSetPtr m_precordset;
ClistCtrl M_List;
The ADO library contains three intelligent pointers: _ConnectionPtr, _Commandptr, and _recordsetptr.
_ConnectionPtr is usually used to create a data connection or perform a SQL statement that does not return any result, such as a stored procedure.
_Commandptr 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.
_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. Add the following code in OnInitDialog ():
Bool caboutdlg :: oninitdialog ()
{
CDIALOG :: OnInitdialog ();
_VARIANT_T Thevalue;
M_List.resetContent ();
M_PConnection.createInstance (_UUIDOF (Connection));
M_PRecordSet.createInstance (_UUIDOF (Recordset);
Try {
m_pConnection-> Open ("DSN = adotest", "," ", 0); // Connect the ODBC data source called Adotest
M_PRecordset-> Open ("Select * from blockdefine", (idispatch *) m_pconnection, adoPENDYNAMIC, ADLOCKOPTIMISTIC, ADCMDTEXT
/ / Execute SQL statement to get a recordset
While (! m_precordset-> adoEof)
// Traverse all records
{
Thevalue = m_precordset-> getCollect ("blockIndex");
// Get the value of the field blockIndex
IF (Thevalue.Vt! = VT_NULL)
m_list.addstring ((char *) _ bstr_t (thevalue)); // Add this value into the list control
M_PRecordset-> MoveNext ();
}
m_precordset-> close ();
m_pConnection-> close ();
}
Catch (_COM_ERROR E) // Abnormal Processing
{
AfxMessageBox (E-> ErrorMessage ());
}
m_precordset = NULL;
M_PConnection = NULL;
Return True; // Return True UnsS you set the focus to a control
}
The program is converted from _variant_t and _bstr_t to the COM object and the C type data, and _variant_t classes encapsulate the OLE Autonomous Variant data type. Using the _variant_t class in C is much easier than using the Variant data type.
Ok, after compiling the program runs, remember to create an ODBC data source called Adotest before running. The program will display the block-de-field value in the table blockDefine in the list control.