您的位置:首页 > 数据库 > Oracle

C++ 连接Oracle

2012-10-18 15:26 381 查看
刚刚学习了C++、感觉学东西还是动手比较学得快一点!

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

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

[plain]view plaincopyprint?

BOSS =

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = xx.xx.xx.xx)(PORT = 1521))

)

(CONNECT_DATA =

(SERVICE_NAME = boss)

)

)

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

[cpp]view plaincopyprint?

#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]view plaincopyprint?

#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 recsetPtr;

}

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

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

[cpp]view plaincopyprint?

CDBOperation dbOper;

bool bConn = dbOper.ConnToDB("Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss", "用户名", "密码");

if (false == bConn)

{

MessageBox((LPCTSTR)"连接数据库出现错误\0",0,0);

return;

}

//查询

_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("查询数据出现错误!\0"),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(); //记录集指针移动到查询结果集的前面

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("插入数据成功\n"));

}

//执行删除语句

sprintf(sql, "delete boss_test_table2 where u_number = '%s'", "009");

pRst = dbOper.ExecuteWithResSQL(sql);

if (NULL != pRst)

{

MessageBox(_T("删除数据成功\0"),0,0);

}

//执行更新语句

sprintf(sql, "update boss_test_table2 set users_name = '%s' ", "C++反人类、MFC反社会");

pRst = dbOper.ExecuteWithResSQL(sql);

if (NULL != pRst)

{

MessageBox(_T("更新数据成功\0"),0,0);

}

哎呀、又有工单了.......一个JS浏览器的兼容问题、有得搞了......o(︶︿︶)o 唉!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: