当前位置:数据库 > Oracle >>

C++ 连接Oracle

刚刚学习了C++、感觉学东西还是动手比较学得快一点!

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

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


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

BOSS =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = xx.xx.xx.xx)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = boss)
    )
  )新建一个头文件、名为CDBOperation.h:


[cpp] 
#pragma once  
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")  
class CDBOperation 

public: 
    //初始化数据库操作需要的对象  
    CDBOperation(void); 
    ~CDBOperation(void); 
 
    //连接至数据库  
    bool ConnToDB(char *ConnectionString, char *UserID, char *Password); 
 
    //数据库操作函数  
    //查询操作 删除以及添加  
    _RecordsetPtr ExecuteWithResSQL(const char *); 
 
private: 
    void PrintErrorInfo(_com_error &); 
 
private: 
    //初始化数据库连接、命令、记录集  
    _ConnectionPtr CreateConnPtr(); 
    _CommandPtr CreateCommPtr(); 
    _RecordsetPtr CreateRecsetPtr(); 
 
private: 
    //数据库连接需要的连接、命令操作对象  
    _ConnectionPtr m_pConnection; 
    _CommandPtr m_pCommand; 
}; 

#pragma once
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
class CDBOperation
{
public:
 //初始化数据库操作需要的对象
 CDBOperation(void);
 ~CDBOperation(void);

 //连接至数据库
 bool ConnToDB(char *ConnectionString, char *UserID, char *Password);

 //数据库操作函数
 //查询操作 删除以及添加
 _RecordsetPtr ExecuteWithResSQL(const char *);

private:
 void PrintErrorInfo(_com_error &);

private:
 //初始化数据库连接、命令、记录集
 _ConnectionPtr CreateConnPtr();
 _CommandPtr CreateCommPtr();
 _RecordsetPtr CreateRecsetPtr();

private:
 //数据库连接需要的连接、命令操作对象
 _ConnectionPtr m_pConnection;
 _CommandPtr m_pCommand;
};新建一个c++源文件、名为CDBOperation.cpp:
[cpp] 
#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::PrintErrorInfo(_com_error &e) 

    printf("Error infomation are 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 rec

补充:软件开发 , C++ ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,