Here is an ADO way to connect to Oracle's applet part of the code ...
First is the configuration of Oracle, located under the Oracle installation path: Oracle\network\admin\tnsnames.ora file, configure the connection configuration
[Plain]View plain copy
- BOSS =
- (DESCRIPTION =
- (Address_list =
- (ADDRESS = (PROTOCOL = TCP) (HOST = xx.xx.xx.xx) (PORT = 1521))
- )
- (Connect_data =
- (service_name = boss)
- )
- )
Create a new header file, named CDBOperation.h:
[CPP]View plain copy
- #pragma once
- #import "C:\Program Files\Common Files\system\ado\msado15.dll" No_namespace rename ("EOF", "adoeof")
- Class Cdboperation
- {
- Public
- //Initialize objects required for database operations
- Cdboperation (void);
- ~cdboperation (void);
- //Connect to database
- bool Conntodb (char *connectionstring, char *userid, char *password);
- //Database operation function
- //Query operation delete and add
- _RecordsetPtr executewithressql (const char *);
- Private
- void Printerrorinfo (_com_error &);
- Private
- //Initialize database connections, commands, recordset
- _connectionptr createconnptr ();
- _commandptr createcommptr ();
- _RecordsetPtr createrecsetptr ();
- Private
- //connection required for database connection, command action object
- _connectionptr m_pconnection;
- _commandptr M_pcommand;
- };
Create a new C + + source file named CDBOperation.cpp:
[CPP]View plain copy
- #include "stdafx.h"
- #include "DBOperation.h"
- Cdboperation::cdboperation (void)
- {
- CoInitialize (NULL);
- M_pconnection = Createconnptr ();
- M_pcommand = Createcommptr ();
- }
- Cdboperation::~cdboperation (void)
- {
- M_pconnection->close ();
- }
- BOOL Cdboperation::conntodb (char *connectionstring, char *userid, char *password)
- {
- if (NULL = = m_pconnection)
- {
- printf ("Failed to create connection\n");
- return false;
- }
- Try
- {
- HRESULT hr = M_pconnection->open (ConnectionString, UserID, Password, NULL);
- if (TRUE = = FAILED (hr))
- {
- return false;
- }
- M_pcommand->activeconnection = m_pconnection;
- return true;
- }
- catch (_com_error &e)
- {
- Printerrorinfo (e);
- return false;
- }
- }
- _RecordsetPtr cdboperation::executewithressql (const char *sql)
- {
- Try
- {
- M_pcommand->commandtext = _bstr_t (sql);
- _RecordsetPtr pRst = M_pcommand->execute (null, NULL, adCmdText);
- return pRst;
- }
- catch (_com_error &e)
- {
- Printerrorinfo (e);
- return NULL;
- }
- }
- void Cdboperation::P rinterrorinfo (_com_error &e)
- {
- printf ("Error infomation is as follows\n");
- printf ("Errorno:%d\nerror message:%s\nerror source:%s\nerror description:%s\n", E.error (), E.errormessage (), ( LPCTSTR) E.source (), (LPCTSTR) e.description ());
- }
- _connectionptr cdboperation::createconnptr ()
- {
- HRESULT HR;
- _connectionptr connptr;
- hr = Connptr.createinstance (__uuidof (Connection));
- if (FAILED (hr) = = TRUE)
- {
- return NULL;
- }
- return connptr;
- }
- _commandptr cdboperation::createcommptr ()
- {
- HRESULT HR;
- _commandptr commptr;
- hr = Commptr.createinstance (__uuidof (Command));
- if (FAILED (hr) = = TRUE)
- {
- return NULL;
- }
- return commptr;
- }
- _RecordsetPtr Cdboperation::createrecsetptr ()
- {
- HRESULT HR;
- _RecordsetPtr recsetptr;
- hr = Recsetptr.createinstance (__uuidof (Command));
- if (FAILED (HR) ==true)
- {
- return NULL;
- }
- return recsetptr;
- }
My code is placed in the MFC one button click event:
Remember to import the header file in the CPP file that handles the event: #include "DBOperation.h"
[CPP]View plain copy
- Cdboperation Dboper;
- bool Bconn = Dboper.conntodb ("PROVIDER=ORAOLEDB.ORACLE.1; Persist Security info=true;data Source=boss ", " username "," password ");
- if (false = = Bconn)
- {
- MessageBox ((lpctstr)"Connection Database Error", 0,0);
- return;
- }
- //Enquiry
- _RecordsetPtr pRst;
- char sql[255] = {0};
- strcpy (SQL, "SELECT * from boss_test_table2 where rownum = 1");
- pRst = dboper.executewithressql (sql);
- if (NULL = = pRst)
- {
- MessageBox (_t ("Query data Error! "), 0,0);
- return;
- }
- if (prst->adoeof)
- {
- Prst->close ();
- MessageBox ((lpctstr)"There is no records in this table\0", 0,0);
- return;
- }
- _variant_t Vsno, VName;
- While (!prst->adoeof)
- {
- //prst->movefirst ();//record set pointer moved to the front of the query result set
- Vsno = Prst->getcollect (_variant_t ("U_number"));
- VName = Prst->getcollect (_variant_t ("Users_name"));
- MessageBox ((lpctstr) (_bstr_t) vsno,0,0);
- Prst->movenext ();
- }
- strcpy (SQL, "INSERT into Boss_test_table2 (U_number, Users_name, Users_phone, status, customno_id) VALUES (' 0001 ', ' C +ttt+ ', ' 13999000000 ', 2, ' bpppppppppp ');
- pRst = dboper.executewithressql (sql);
- if (NULL! = pRst)
- {
- AfxMessageBox (_t ("Insert data succeeded \ n"));
- }
- //Execute DELETE statement
- sprintf (SQL, "Delete boss_test_table2 where U_number = '%s '", "009");
- pRst = dboper.executewithressql (sql);
- if (NULL! = pRst)
- {
- MessageBox (_t ("delete data successfully"), 0,0);
- }
- //Execute UPDATE statement
- sprintf (SQL, "Update boss_test_table2 set users_name = '%s '", " C + + anti-Human, MFC anti-social");
- pRst = dboper.executewithressql (sql);
- if (NULL! = pRst)
- {
- MessageBox (_t ("Update data successfully"), 0,0);
- }
C + + Connect Oracle