MFC connects to the Oracle database through ado

Source: Internet
Author: User

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 "))));

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.