How does vc operate Access databases through ADO?

Source: Internet
Author: User
Database applications play an indispensable role in applications. Today we will explain how to use ADO to Access databases in vc. First, introduce the definition file of the ADO Library and Reference msado15.dll. This file is in the programfilescommonfilessystemado folder of the system installation disk. You only need to run the following code: # importc: prog

Database applications play an indispensable role in applications. Today we will explain how to use ADO to Access databases in vc. First, introduce the definition file of the ADO Library and Reference msado15.dll. This file is in the program files \ common files \ system \ ado folder of the system installation disk. You only need to run the following code: # import c: \ prog

DatabaseThe application has an indispensable role in the application.HowPassADOOperationAccessDatabase.

First, introduce the definition file of the ADO Library and Reference msado15.dll. This file is in the program files \ common files \ system \ ado folder of the system installation disk. You only need to run the following code:

# Import "c: \ program files \ common files \ system \ ado \ msado15.dll "\

No_namespace \

Rename ("EOF", "adoEOF ")

You can add it to StdAfx. h. Www.2cto.com

Again, the COM library is initialized. This step is usually implemented during the initialization window. The Code is as follows:

AfxOleInit ();

Step 3 is to use the established connectionPassThe Connection and Command objects Execute SQL commands, or use the Recordset object to obtain the result record set for query and processing.

// Define ADO connection, command, record set variable pointer

_ ConnectionPtr m_pConnection;

_ CommandPtr m_pCommand;

_ RecordsetPtr m_pRecordset;

ToDatabaseDemo. mdb is used as an example. The following code initializes each pointer:

M_pConnection.CreateInstance (_ uuidof (Connection); // initialize COM and create ADO connections.Operation

Try

{

// Open the local Access Library Demo. mdb

M_pConnection-> Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = Demo. mdb", "", adModeUnknown );

}

Catch (_ com_error e)

{

AfxMessageBox ("DatabaseConnection Failed. OKDatabaseDemo. Whether mdb is in the current path !");

Return FALSE;

}

M_pRecordset.CreateInstance (_ uuidof (Recordset); // create with ADODatabaseRecord set

// In ADOOperationWe recommend that you use try... Catch () to capture error information,

// Because it often produces unexpected errors.

Try

{

M_pRecordset-> Open ("SELECT * FROM DemoTable", // query all fields in the DemoTable table

M_pConnection.GetInterfacePtr (), // gets the IDispatch pointer of the database connected to the database

AdOpenDynamic,

AdLockOptimistic,

AdCmdText );

}

Catch (_ com_error * e)

{

AfxMessageBox (e-> ErrorMessage ());

}

Second, you can query, modify, delete, and so on.Operation.

Please follow Li Mu space www.limou.net to learn more about MFC.

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.