Database programming in Visual C
Author: Jiang Dongyu Transfer: www.vckbase.com ActiveX Data Objects (ADO) is the OLE DB upper level database API. 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 to create a standard MFC AppWizard (EXE) application, then initialize the OLE / COM library in the application class's initInstance function (because the ADO library is a COM DLL library). Bool CadotestApp :: InitInstance () {// Initialization OLE / COM library environment AFXoleinit ();} 2. Introducing the ADO library file Using ADO to introduce the STDAFX.H file to introduce the ADO library file in the project's stdafx.h file, To make the compiler to compile correctly. The code is as follows: #include #import "c: / program files / common files / system / ado / msado15.dll" no_namespace rename ("eof", "adoeof") header file comdef.h makes our application The program can 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 is performed in the CaboutDLG header file in the CaboutDLG header and add a listCtrl in the dialog. _ConnectionPtr M_PCONNECTION; _RecordSetPtr m_precordset; clistctrl m_list; ADO library contains three smart 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.