Database programming with ADO in Visual C + +

Source: Internet
Author: User
Tags header ole resource

ActiveX Data Objects (ADO) are high-level database APIs above OLE DB. We can also call ADO in C + + programs. This article will be in VC 6.0 environment to do a small example to explain how to use ADO.

1. Build the application framework and initialize the Ole/com library environment

Create a standard MFC AppWizard (EXE) application, and 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()
   { //初始化OLE/COM库环境
AfxOleInit();}

The program will finally call:: CoUninitialize ();//Release the COM resource that the program occupies.

Other than that:

m_pRecordset->Close(); 注意!!!不要多次关闭!!!!!!!!!!!!
m_pConnection->Close();
m_pRecordset = NULL;
m_pConnection = NULL;

2. Introduction of ADO library files

Before using ADO, we must introduce the #import into the ADO library file in the engineering stdafx.h file, so that the compiler can compile correctly. The code is as follows:

   #include 〈comdef.h〉
   #import "c:\program files\common files\system\ado\msado15.dll"
   no_namespace
   rename ("EOF","adoEOF")

Header file Comdef.h enables our applications to use some of the special COM support classes in Visual C + +, which makes it easier to handle OLE autonomy, which is the type of data that ADO uses. The following three lines use the #import directive to enter the ADO class library definition in our application.

The ADO class is defined as a resource stored in an ADO DLL (Msado15.dll), within which it is called a type library. The type library describes the autonomous interface and the COM vtable interface used by C + +. When using the #import directive, Visual C + + needs to read this type library from the ADO DLL at run time, creating a set of C + + header files. These header files have the. Tli and. TLH extensions, and readers can find the two files in the project's directory. The ADO classes that are called in C + + program code are defined in these files.

The third line of the program indicates that the ADO object does not use namespaces. In some applications, namespaces are necessary because there may be naming conflicts between objects in your application and objects in ADO. If you want to use namespaces, you can modify the third line of programs to: Rename_namespace ("Adons"). The four-line code renames the EOF (end of file) in ADO to adoeof to avoid conflicts with other libraries that define their own EOF.

3. Using smart pointers for database operations

Define two instances of the ADO smart pointer class in the CABOUTDLG header file and add a Listctrl to the dialog box.

class CAdotestDlg : public CDialog
{
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
   ClistCtrl m_List;
......
}

The ADO library contains three smart pointers: _connectionptr, _commandptr, and _RecordsetPtr.

_connectionptr is often used to create a data connection or execute a SQL statement that does not return any results, such as a stored procedure.

_commandptr returns a recordset. It provides an easy way to execute stored procedures and SQL statements that return a recordset. When using the _commandptr interface, you can use the global _connectionptr interface, or you can use the connection string directly in the _commandptr interface.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.