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.