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