C++ 连接Oracle (二)

2014-11-24 09:46:38 · 作者: · 浏览: 2
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;
}

#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]
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(); //记录集指针移