First, add # IMP to the stdafx. h header file.Ort "C: \ Program Files \ common files \ System \ ADO \ msado15.dll "\
No_namespace Rename ("EOF", "adoeof ")
In the header file of the app class
Public:_ Connectionptr m_pconnection; // connection object pointer
Add the following code to the initinstance () function of the app class:
Afxoleinit (); // initialize com
Try // connect to the database
{
M_pconnection.createinstance (_ uuidof (connection ));
_ Bstr_t strconnect = "provider = msdaora.1; user id = 'system'; Password = 'renzh '; daTa source = orcl; persist Security info = false ;";
M_pconnection-> open (strconnect, "system", "renzh", admodeunknown );
}
Catch (_ com_error E)
{
Afxmessagebox (text ("database connection error! "));
Afxmessagebox (E. Description ());
}
You can also add:
If (m_pconnection-> state)
M_pconnection-> close ();
M_pconnection-> release ();
I don't think this step is necessary either (I did not add it myself)
Then the database is opened and can be called in all classes as follows: theapp.M_pconnection-> execute (_ bstr_t) SQL, null, ad1_text );
Then a record set pointer is known at the place where the record set is to be accessed:
Cstring SQL = _ T ("this is an execution statement !! "):
_ Recordsetptr PRS ("ADODB. recordset ");
PRS-> open (_ bstr_t) SQL, _ variant_t (idispatch*) Theapp. m_pconnection, true), adopenstatic, adlockoptimistic, ad1_text );
If (! (PRS-> adobof) // if the record set is not empty
{
......
}
To retrieve the record set information, you can use PRS-> getcollect_r (_ T ("an attribute column name"). Its return value is _ variant_t, which can be converted as needed. For example, insert a clistctrl object:
M_grd.setitemtext (, (lptstr) (_ bstr_t) (PRS-> getcollect_r (_ T ("first column attribute name "))));