C++ 连接Oracle

下面是一个ADO方式连接Oracle的小程序部分代码……

首先是Oracle的配置、在Oracle的安装路径下找到:Oracle
etworkADMIN nsnames.ora文件、配置一下连接配置

[plain] view plain copy

BOSS =  
  DESCRIPTION =  
    ADDRESS_LIST =  
      ADDRESS = PROTOCOL = TCP)HOST = xx.xx.xx.xx)PORT = 1521))  
    )  
    CONNECT_DATA =  
      SERVICE_NAME = boss)  
    )  
  )  

新建一个头文件、名为CDBOperation.h:

[cpp] view plain copy

#pragma once  
#import “c:program filescommon filessystemadomsado15.dll” no_namespace rename”EOF”, “adoEOF”)  
class CDBOperation  
{  
public:  
    //初始化数据库操作需要的对象  
    CDBOperationvoid);  
    ~CDBOperationvoid);  
  
    //连接至数据库  
    bool ConnToDBchar *ConnectionString, char *UserID, char *Password);  
  
    //数据库操作函数  
    //查询操作 删除以及添加  
    _RecordsetPtr ExecuteWithResSQLconst char *);  
  
private:  
    void PrintErrorInfo_com_error &);  
  
private:  
    //初始化数据库连接、命令、记录集  
    _ConnectionPtr CreateConnPtr);  
    _CommandPtr CreateCommPtr);  
    _RecordsetPtr CreateRecsetPtr);  
  
private:  
    //数据库连接需要的连接、命令操作对象  
    _ConnectionPtr m_pConnection;  
    _CommandPtr m_pCommand;  
};  

新建一个c++源文件、名为CDBOperation.cpp:

[cpp] view plain copy

#include “stdafx.h”  
#include “DBOperation.h”  
CDBOperation::CDBOperationvoid)  
{  
    CoInitializeNULL);  
    m_pConnection = CreateConnPtr);  
    m_pCommand = CreateCommPtr);  
}  
CDBOperation::~CDBOperationvoid)  
{  
    m_pConnection->Close);  
}  
bool CDBOperation::ConnToDBchar *ConnectionString, char *UserID, char *Password)  
{  
    if NULL == m_pConnection)  
    {  
        printf“Failed to create connection
“);  

        return false;  
    }  
    try  
    {  
        HRESULT hr = m_pConnection->OpenConnectionString, UserID, Password, NULL);  
        if TRUE == FAILEDhr))  
        {  
            return false;  
        }  
        m_pCommand->ActiveConnection = m_pConnection;  
        return true;  
    }  
    catch_com_error &e)  
    {  
        PrintErrorInfoe);  
        return false;  
    }  
}  
_RecordsetPtr CDBOperation::ExecuteWithResSQLconst char *sql)  
{  
    try  
    {  
        m_pCommand->CommandText = _bstr_tsql);  
        _RecordsetPtr pRst = m_pCommand->ExecuteNULL, NULL, adCmdText);  
        return pRst;  
    }  
    catch_com_error &e)  
    {  
        PrintErrorInfoe);  
        return NULL;  
    }  
}  
void CDBOperation::PrintErrorInfo_com_error &e)  
{  
    printf“Error infomation are as follows
“);  

    printf“ErrorNo: %d
Error Message:%s
Error Source:%s
Error Description:%s
“, e.Error), e.ErrorMessage), LPCTSTR)e.Source), LPCTSTR)e.Description));  

}  
  
_ConnectionPtr CDBOperation::CreateConnPtr)  
{  
    HRESULT hr;  
    _ConnectionPtr connPtr;  
    hr = connPtr.CreateInstance__uuidofConnection));  
    if FAILEDhr) == TRUE)  
    {  
        return NULL;  
    }  
    return connPtr;  
}  
  
_CommandPtr CDBOperation::CreateCommPtr)  
{  
    HRESULT hr;  
    _CommandPtr commPtr;  
    hr = commPtr.CreateInstance__uuidofCommand));  
    if FAILEDhr) == TRUE)  
    {  
        return NULL;  
    }  
    return commPtr;  
}  
  
_RecordsetPtr CDBOperation::CreateRecsetPtr)  
{  
    HRESULT hr;  
    _RecordsetPtr recsetPtr;  
    hr = recsetPtr.CreateInstance__uuidofCommand));  
    if FAILEDhr) ==TRUE)  
    {  
        return NULL;  
    }  
    return recsetPtr;  
}  

我的代码是放在MFC一个按钮Click事件里面的:

记住在处理事件的cpp文件中导入头文件:#include “DBOperation.h”

[cpp] view plain copy

CDBOperation dbOper;  
    bool bConn = dbOper.ConnToDB“Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss”, “用户名”, “密码”);  
    if false == bConn)  
    {  
        MessageBoxLPCTSTR)“连接数据库出现错误”,0,0);  
        return;  
    }  
  
    //查询  
    _RecordsetPtr pRst;  
    char sql[255] = {0};  
    strcpysql, ” select * from boss_test_table2 where rownum = 1 “);  
    pRst = dbOper.ExecuteWithResSQLsql);  
    if NULL == pRst)  
    {  
        MessageBox_T“查询数据出现错误!”),0,0);  
        return;  
    }  
    if pRst->adoEOF)  
    {  
        pRst->Close);  
        MessageBoxLPCTSTR)“There is no records in this table”,0,0);  
        return;  
    }  
    _variant_t vSno, vName;  
    while !pRst->adoEOF)  
    {  
        //pRst->MoveFirst); //记录集指针移动到查询结果集的前面  
        vSno = pRst->GetCollect_variant_t“U_NUMBER”));  
        vName = pRst->GetCollect_variant_t“USERS_NAME”));  
        MessageBoxLPCTSTR)_bstr_t)vSno,0,0);  
        pRst->MoveNext);  
    }   
  
    strcpysql, “insert into boss_test_table2 u_number, users_name, users_phone, status, customno_id) values ‘0001’, ‘C+TTT+’, ‘13999000000’, 2, ‘BPPPPPPPPPP’)”);  
    pRst = dbOper.ExecuteWithResSQLsql);  
    if NULL != pRst)  
    {  
        AfxMessageBox_T“插入数据成功
“));  

    }  
    //执行删除语句  
    sprintfsql, “delete boss_test_table2 where u_number = ‘%s'”, “009”);   
    pRst = dbOper.ExecuteWithResSQLsql);  
    if NULL != pRst)   
    {  
        MessageBox_T“删除数据成功”),0,0);  
    }  
    //执行更新语句  
    sprintfsql, “update boss_test_table2 set users_name = ‘%s’ “, “C++反人类、MFC反社会”);  
    pRst = dbOper.ExecuteWithResSQLsql);  
    if NULL != pRst)  
    {  
        MessageBox_T“更新数据成功”),0,0);   
    }  

Published by

风君子

独自遨游何稽首 揭天掀地慰生平