Key issues of connecting VC console programs to Oracle

Source: Internet
Author: User

Recently, I was using vc6 to develop a console program to connect to the Oracle database. The program was compiled, but the operation was. When I created _ connectionptr, it was always unsuccessful. Now I finally found the problem, the code for successful testing is as follows.

 

# Include <iostream>
# Include <afx. h>
# Include "Main. H"
// # Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "adoeof ")

Using namespace STD;

 

Void main ()
{
: Coinitialize (null );
_ Connectionptr myconn;
Myconn. createinstance ("ADODB. Connection ");

// Microsoft provides the connection string
Char strconn [] = {"provider = msdaora; Data Source = ORACLE; user id = system; Password = zjzjzh ;"};
 
Hresult hR = myconn-> open (_ bstr_t) strconn, "", "", adconnectunspecified );
If (failed (HR ))
{
// Afxmessagebox ("database connection error ");
Cout <"database connection error" <Endl;
Return;
}

// Open a connection
_ Recordsetptr myrecord;
Myrecord. createinstance ("ADODB. recordset ");

// Open the result set
Myrecord-> open ("select * from HR. Jobs", myconn. getinterfaceptr (), adopendynamic, adlockoptimistic, adshorttext );

// Operation result set
Variant var;
Variantinit (& var );

Cstring strjobid;
While (myrecord-> adoeof = false)
{
Var = myrecord-> getcollect ("job_id ");

If (var. VT! = Vt_null)
{
Strjobid = (lpcstr) (_ bstr_t) var;
// Afxmessagebox (strjobid );
Cout <strjobid. getbuffer (strjobid. getlength () <Endl;
Myrecord-> movenext ();
}
}
// Closes the result set after the structure is used.
If (myrecord-> state = true)
{
Myrecord-> close ();
}
Myrecord = NULL;

// Close the database connection
If (myconn-> state = true)
{
Myconn-> close ();
}
Myconn = NULL;
Couninitialize ();

}
The problem is that when the OLE library is initialized, The afxoleinit function cannot be used, and coinitialize is used. Otherwise, the connection and result set object created below will fail.

 

Another: 1. To add a non-UI class of MFC to the console program, you can include afx. H header file, and select the/MD option in the compilation options set by the project,

2. If the iostream header file is included, the introduced statement must be in front of using namespace STD; otherwise, an error occurs during compilation.

The above are some of my experiences when developing Oracle programs, and I hope to help you.

 

There is no such problem in the development of MFC programs.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zjwhcn/archive/2009/06/03/4239414.aspx

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.