自学内容网 自学内容网

oracle连接封装

#pragma once
#pragma once
#import “c:\program files\common files\system\ado\msado15.dll” no_namespace rename(“EOF”, “adoEOF”)
//,rename(“LockTypeEnum”,“AdoLockTypeEnum”),rename(“DataTypeEnum”,“AdoDataTypeEnum”),\ EndOfFile" ,adoEOF ,
//rename(“FieldAttributeEnum”,“AdoFieldAttributeEnum”),rename(“EditModeEnum”,“AdoEditModeEnum”),rename(“RecordStatusEnum”,“AdoRecordStatusEnum”),rename(“ParameterDirectionEnum”,“AdoParameterDirectionEnum”)

class DBOperation
{
public:
DBOperation(void);
~DBOperation(void);

//连接至数据库
bool ConnToDB(char *ConnectionString, char *UserID, char *Password);
//数据库操作函数
//查询操作 删除以及添加
_RecordsetPtr ExecuteWithResSQL(const char *);
//bool ExecuteNoResSQL(const char *);//delete and add
void CloseDb();

private:
void PrintErrorInfo(_com_error &);
private:
//初始化数据库连接、命令、记录集
_ConnectionPtr CreateConnPtr();
_CommandPtr CreateCommPtr();
_RecordsetPtr CreateRecsetPtr();
private:
//数据库连接需要的连接、命令操作对象
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;

};
//连接字符串:
CString strConn;
HRESULT hr;
_ConnectionPtr m_Conn;

CString strSQL,strSQL2,orcal,m_sConn;
orcal.Format(_T("Provider=OraOLEDB.Oracle;Password=%s;Persist Security Info=True;UserID=%s;Data Source=%s"),m_strPassword,m_strUserID,m_strCatalog);

//cpp
#include “StdAfx.h”
#include “DBOperation.h”

DBOperation::DBOperation(void)
{
CoInitialize(NULL);
m_pConnection = CreateConnPtr();
m_pCommand = CreateCommPtr();
}
DBOperation::~DBOperation(void)
{
//m_pCommand->Close();
if(m_pConnection->State)
{
m_pConnection->Close();
CoUninitialize();
}
}
void DBOperation::CloseDb()
{
if(m_pConnection->State)
{
m_pConnection->Close();
CoUninitialize();
}
}
bool DBOperation::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);

CString strerror;
strerror.Format(_T("ErrorNo: %d\nError Message:%s\nError Source:%s\nError Description:%s\n",),e.Error(), e.ErrorMessage(), (LPCTSTR)e.Source(), (LPCTSTR)e.Description());
PRINTF(strerror);
DWORD ret=GetLastError();
return false;
}

}
_RecordsetPtr DBOperation::ExecuteWithResSQL(const char *sql)
{
//已经在连接至数据库的时候进行判断了
if (NULL == m_pCommand || 0 == m_pConnection->State)
{
printf(“Failed to create command OR the state of connection is zero\n”);
PRINTF(_T(“Failed to create command OR the state of connection is zero \n”));
return NULL;
}
//char *query = new char;
//strcpy(query, sql);
try
{

PRINTF((LPCTSTR)(_bstr_t)(sql));
m_pCommand->CommandText = _bstr_t(sql);
_variant_t ra;
_RecordsetPtr pRst = m_pConnection->Execute((_bstr_t)sql, &ra, adCmdText);
return pRst;
}
catch(_com_error &e)
{
PrintErrorInfo(e);
CString strerror;
strerror.Format(_T("ErrorNo: %d\nError Message:%s\nError Source:%s\nError Description:%s\n",),e.Error(), e.ErrorMessage(), (LPCTSTR)e.Source(), (LPCTSTR)e.Description());
PRINTF(strerror);
return NULL;
}

}
void DBOperation::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 DBOperation::CreateConnPtr()
{
HRESULT hr;
_ConnectionPtr connPtr;
hr = connPtr.CreateInstance(__uuidof(Connection));
if (FAILED(hr) == TRUE)
{
return NULL;
}
return connPtr;
}
_CommandPtr DBOperation::CreateCommPtr()
{
HRESULT hr;
_CommandPtr commPtr;
hr = commPtr.CreateInstance(__uuidof(Command));
if (FAILED(hr) == TRUE)
{
return NULL;
}
return commPtr;
}
_RecordsetPtr DBOperation::CreateRecsetPtr()
{
HRESULT hr;
_RecordsetPtr recsetPtr;
hr = recsetPtr.CreateInstance(__uuidof(Command));
if (FAILED(hr) ==TRUE)
{
return NULL;
}
return recsetPtr;
}

封装的Oracle数据库连接类,可以自行调用.数据库连接无非三个指针,
_ConnectionPtr CreateConnPtr();创建连接
_CommandPtr CreateCommPtr();执行命令指令
_RecordsetPtr CreateRecsetPtr();查询记录
分别实例化指针就可以使用了.
_ConnectionPtr DBOperation::CreateConnPtr()
{
HRESULT hr;
_ConnectionPtr connPtr;
hr = connPtr.CreateInstance(__uuidof(Connection));
if (FAILED(hr) == TRUE)
{
return NULL;
}
return connPtr;
}
_CommandPtr DBOperation::CreateCommPtr()
{
HRESULT hr;
_CommandPtr commPtr;
hr = commPtr.CreateInstance(__uuidof(Command));
if (FAILED(hr) == TRUE)
{
return NULL;
}
return commPtr;
}
_RecordsetPtr DBOperation::CreateRecsetPtr()
{
HRESULT hr;
_RecordsetPtr recsetPtr;
hr = recsetPtr.CreateInstance(__uuidof(Command));
if (FAILED(hr) ==TRUE)
{
return NULL;
}
return recsetPtr;
}
总结下,创建你需要在指针,实例化(相当于初始化),写入字符串执行相关命令,数据库连接就完成了.
复杂问题简单化,简单问题明了化,一步一个脚印向前走!


原文地址:https://blog.csdn.net/u011637657/article/details/135599595

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!