欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

在VS环境下以ADO方式操作Oracle数据库

程序员文章站 2024-04-05 18:05:48
...

利用ADO引擎方式访问Oracle数据库的实现方法: 定义数据库头文件为CDBOperation.h #pragma once #import C:\Program Files\Com

利用ADO引擎方式访问Oracle数据库的实现方法:

定义数据库头文件为CDBOperation.h

#pragma once
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF"),rename("LockTypeEnum","AdoLockTypeEnum"),rename("DataTypeEnum","AdoDataTypeEnum"),\
rename("FieldAttributeEnum","AdoFieldAttributeEnum"),rename("EditModeEnum","AdoEditModeEnum"),rename("RecordStatusEnum","AdoRecordStatusEnum"),rename("ParameterDirectionEnum","AdoParameterDirectionEnum")
class CDBOperation
{
public:
//初始化数据库操作需要的对象
CDBOperation(void);
~CDBOperation(void);

//连接至数据库
bool ConnToDB();

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

private:
void PrintErrorInfo(_com_error &);

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

private:
//数据库连接需要的连接、命令操作对象
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
};

定义程序源文件CDBOperation.cpp

#include "CDBOperation.h"

CDBOperation::CDBOperation(void)
{
CoInitialize(NULL);
m_pConnection = CreateConnPtr();
m_pCommand = CreateCommPtr();
}
CDBOperation::~CDBOperation(void)
{
m_pConnection->Close();
}
bool CDBOperation::ConnToDB()
{
if (NULL == m_pConnection)
{
printf("Failed to create connection\n");
return false;
}
try
{
_bstr_t strConnect="Provider=OraOLEDB.Oracle;User ID=scott;Password=19900624;Persist Security Info=True;Data Source=\"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.108.85.137)(PORT = 1521)) )(CONNECT_DATA = (SID = ORCL)(SERVER=DEDICATED)))\"";
HRESULT hr=m_pConnection->Open(strConnect,"","",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(Recordset));
if (FAILED(hr) ==TRUE)
{
return NULL;
}
return recsetPtr;
}

在VS环境下以ADO方式操作Oracle数据库