ADO (ActiveX dataobjects) is a new generation of database access specifications released by Microsoft. Its powerful usage profile has basically replaced ODBC and Dao.
ADO object:
1. connection object is used to connect the data source and process some commands and things. Before using ADO to access the database, you must create a connection object to open the connection with the database.
2. The command object is used to execute commands passed to the data source.
3. the recordset object can operate data from the data source, through which almost all data can be operated. All recordsets can be constructed by using records (rows) and fields (columns ).
4. The parameter object parameter represents the parameter information of the parameter and the argument parameterized command.
Encapsulated ADO operation header file:
# Pragma once
# Import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll "\
No_namespacerename ("EOF", "adoeof") Rename ("Bof", "adobof ")
Class adoconn
{
Public:
Adoconn (void );
~ Adoconn (void );
_ Connectionptr m_pconnection; // Add a pointer to the connection
_ Recordsetptr m_precordset; // Add a pointer to recordset
Void onnitadoconn (void); // initialize the database connection
Void exitconnect (void); // exit the database connection
_ Recordsetptr & getrecordset (_ bstr_tbstrsql); // obtain the record set of the Data Source
Bool executesql (_ bstr_t bstrsql); // execute the passed Database SQL command
};
Encapsulated ADO operation source file:
# Include "stdafx. H"
# Include "adoconn. H"
Adoconn: adoconn (void)
{
}
Adoconn ::~ Adoconn (void)
{
}
Voidadoconn: onnitadoconn (void)
{
: Coinitialize (null); // initialize the OLE/COM Environment
Try
{
M_pconnection.createinstance ("ADODB. Connection ");
Cstring connectionstr = "driver = {sqlserver}; server = (local); database = personalinfomanagesystem; uid = sa; Pwd = 123456"; // set the connection string
M_pconnection-> open (_ bstr_t) connectionstr, "", "", admodeunknown );
}
Catch (_ com_error e) // catch exceptions
{
Afxmessagebox (E. Description (); // displays the error message.
}
}
Voidadoconn: exitconnect (void)
{
M_pconnection-> close ();
: Couninitialize ();
}
Booladoconn: executesql (_ bstr_t bstrsql)
{
_ Variant_t recordsaffected;
Try
{
If (m_pconnection = NULL)
{
Onnitadoconn ();
}
M_pconnection-> execute (bstrsql, null, ad1_text );
Return true;
}
Catch (_ com_error E)
{
E. Description ();
Return false;
}
}
_ Recordsetptr & adoconn: getrecordset (_ bstr_t bstrsql)
{
Try
{
If (m_pconnection = NULL)
{
Onnitadoconn ();
}
M_precordset.createinstance (_ uuidof (recordset ));
M_precordset-> open (_ bstr_t) bstrsql, m_pconnection.getinterfaceptr (), adopendynamic, adlockoptimistic, adshorttext );
}
Catch (_ com_error E)
{
E. Description ();
}
Return m_precordset;
}
Use the header file and source file to retrieve data from the data source and display the data in the listview:
Adoconn m_ado; // defines the ADO connection object
Int I = 400;
M_ado.onnitadoconn (); // Initialization
Cstring SQL = "select * From dataorder by frec DESC"; // sets the SQL command
_ Recordsetptr m_precord;
M_precord = m_ado.getrecordset (_ bstr_t (SQL); // obtain the record set queried by the data source
While (m_ado.m_precordset-> adoeof = 0) // = 0 indicates that there is still data
{
Cstring S;
S. Format ("% d", I --);
M_data.insertitem (0 ,"");
M_data.setitemtext (0, 0, S );
M_data.setitemtext (0, 1, (char *) (_ bstr_t) m_precord-> getcollect ("frec "));
M_data.setitemtext (0, 2, (char *) (_ bstr_t) m_precord-> getcollect ("result "));
M_precord-> movenext (); // obtain the next record
}
M_ado.exitconnect (); // exit the connection