<转>vc 用ado访问Oracle数据库的代码示例

简介: #pragma once class CDBOp { public: bool ReConnect(); bool CloseConnect(); bool OpenConnect(CString hostName, CString dBName, CString us...
#pragma once

class CDBOp  
{
public:
	bool ReConnect();
	bool CloseConnect();
	bool OpenConnect(CString hostName, CString dBName, CString userName, CString password);
	bool GetItemData(CString itemID, float &price, CString &descript);  //取存储过程数据,这里只是举例说明
	CString GetErrorMsg();
	CDBOp();
	virtual ~CDBOp();
private:
	_ConnectionPtr m_pConnection;  //连接对象
	_RecordsetPtr m_pRecordset;     //记录集对象
	bool   m_bConnectSuccess;     //连接是否成功
	CString   m_strConnString;      //数据库连接字符串 
	CString   m_strErrMsg;           //保存错误信息
};




#include "StdAfx.h"
#include "DBOp.h"

//类实现


CDBOp::CDBOp():m_bConnectSuccess(false)
{
	::CoInitialize(NULL);
	m_pConnection.CreateInstance("ADODB.Connection");
	m_pConnection->ConnectionTimeout=30; 
	m_pRecordset.CreateInstance("ADODB.Recordset");
}
CDBOp::~CDBOp()
{
	//::CoUninitialize();
	CloseConnect();
}
//打开连接(数据库类型,主机名,数据库名,登陆名,密码)
//数据库类型: 0 为Sql server, 1为 Oracle
bool CDBOp::OpenConnect(
						CString hostName, 
						CString dBName, 
						CString userName, 
						CString password)        
{
	CString strConn;

	//MSDAORA or OraOLEDB.Oracle.1
	strConn = "Provider=OraOLEDB.Oracle.1";
	strConn+= ";Persist Security Info=true";
	strConn+= ";User ID=";
	strConn+= userName;
	strConn+= ";Password=";
	strConn+= password;
	strConn+= ";Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)";
	strConn+= "(HOST=";
	strConn+= hostName;
	strConn+= ")(PORT=1521))(CONNECT_DATA=";
	strConn+= "(SERVICE_NAME=";
	strConn+= dBName;
	strConn+= ")))";


	if (strConn.IsEmpty()) 
	{
		m_strErrMsg="The connect string is null.";
		return false;
	}
	CloseConnect();
	m_strConnString =strConn; 

	return ReConnect();
}
//再次连接
bool CDBOp::ReConnect()
{
	m_strErrMsg=_T("");
	m_bConnectSuccess= false;
	HRESULT hr;
	try
	{
		hr = m_pConnection->Open(_bstr_t(m_strConnString), "", "", adModeUnknown);
		if (SUCCEEDED(hr))
			m_bConnectSuccess=true;  
	}
	catch(_com_error e)
	{  
		m_strErrMsg.Format(_T("Connect database failure!\r\n\r\n message error:%s\r\n\r\n The connect string:%s"),e.ErrorMessage(),m_strConnString); 
	}
	return m_bConnectSuccess;
}
//关闭链接
bool CDBOp::CloseConnect()
{
	if (m_bConnectSuccess)
	{
		if (m_pConnection->State==1) 
			m_pConnection->Close(); 
		m_bConnectSuccess =false;
	}
	return true;
}
//取得错误信息
CString CDBOp::GetErrorMsg()
{
	return m_strErrMsg;
}

bool CDBOp::GetItemData(CString itemID, float &price, CString &descript)
{
	_CommandPtr   pCommand = NULL;
	pCommand.CreateInstance("ADODB.Command");
#ifdef   _DEBUG   
	if   (pCommand   ==   NULL)   
	{   
		AfxMessageBox(_T("Command Created fail! Please confirm whether initialize COM."));   
	}   
#endif   
	ASSERT(pCommand   !=   NULL);  
	try   
	{   
		if (m_bConnectSuccess==false)
		{
			if (ReConnect()==false) 
				return false;
		}
		//输入参数   itemID    
		_ParameterPtr   pParamItemID;   
		pParamItemID.CreateInstance("ADODB.Parameter");   
		pParamItemID->Name="ItemID";   //所用存储过程参数名称   
		pParamItemID->Type=adChar;    //参数类型   
		pParamItemID->Size=10;     //参数大小   
		pParamItemID->Direction=adParamInput;  //表明是输入参数   
		pParamItemID->Value=_variant_t(itemID);   
		pCommand->Parameters->Append(pParamItemID);    


		//输出参数   price    
		_ParameterPtr   pParamPrice;       
		pParamPrice.CreateInstance("ADODB.Parameter");   
		pParamPrice->Name="Price";    //参数名称   
		pParamPrice->Type=adNumeric;    //参数类型   
		pParamPrice->Size=9;      //参数大小
		pParamPrice->Precision =9;
		pParamPrice->NumericScale =2;  
		pParamPrice->Direction=adParamOutput;  //声明是输出参数   
		pCommand->Parameters->Append(pParamPrice);  

		//输出参数   Descript    
		_ParameterPtr   pParamDescript;       
		pParamDescript.CreateInstance("ADODB.Parameter");   
		pParamDescript->Name="Descript";   //参数名称   
		pParamDescript->Type=adVarChar;    //参数类型   
		pParamDescript->Size=160;     //参数大小
		pParamDescript->Direction=adParamOutput; //声明是输出参数   
		pCommand->Parameters->Append(pParamDescript);  
		//执行存储过程   
		pCommand->ActiveConnection=m_pConnection;   
		pCommand->CommandText="spItemInfo";   //存储过程名称   
		pCommand->CommandType=adCmdStoredProc;  //表示为存储过程adCmdStoredProc   
		pCommand->Execute(NULL,   NULL,   adCmdStoredProc);   


		price=(float)(pParamPrice->Value);
		descript = (char*)_bstr_t(pParamDescript->Value);  
		return true;
	}
	catch(_com_error   e)   
	{         
		m_strErrMsg.Format(_T("Error:GetItemData. Reason:%s\n file: %s; line: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);       
		return false;
	}   

}

  转自http://www.cnblogs.com/finema/archive/2008/08/22/1273478.html

相关文章
|
2天前
|
存储 Oracle 关系型数据库
Oracle数据库的应用场景有哪些?
【10月更文挑战第15天】Oracle数据库的应用场景有哪些?
94 64
|
1天前
|
监控 Oracle 关系型数据库
Oracle数据库性能优化
【10月更文挑战第16天】Oracle数据库性能优化是
5 1
|
8天前
|
存储 Oracle 关系型数据库
【数据库-Oracle】《Oracle 数据库探秘:基础知识点全攻略》
《Oracle 数据库探秘:基础知识点全攻略》深入介绍 Oracle 数据库的基础知识点,包括数据类型、表结构、查询语句等。通过详细讲解、代码示例和流程图,帮助读者快速掌握 Oracle 数据库的基本操作,为数据库开发和管理打下坚实基础。
19 0
|
10天前
|
Oracle 关系型数据库 数据库
oracle数据恢复—Oracle数据库文件损坏导致数据库打不开的数据恢复案例
打开oracle数据库时报错,报错信息:“system01.dbf需要更多的恢复来保持一致性,数据库无法打开”。急需恢复zxfg用户下的数据。 出现上述报错的原因有:控制文件损坏、数据文件损坏、数据文件与控制文件的SCN不一致等。数据恢复工程师对数据库文件做进一步检测分析后发现sysaux01.dbf文件有坏块。修复sysaux01.dbf文件,启动数据库依然有许多查询报错。export和data pump工具无法使用,查询告警日志并分析报错,确认发生上述错误的原因就是sysaux01.dbf文件损坏。由于该文件损坏,从数据库层面无法修复数据库。由于system和用户表空间的数据文件是正常的,
|
9天前
|
存储 SQL 关系型数据库
Mysql学习笔记(二):数据库命令行代码总结
这篇文章是关于MySQL数据库命令行操作的总结,包括登录、退出、查看时间与版本、数据库和数据表的基本操作(如创建、删除、查看)、数据的增删改查等。它还涉及了如何通过SQL语句进行条件查询、模糊查询、范围查询和限制查询,以及如何进行表结构的修改。这些内容对于初学者来说非常实用,是学习MySQL数据库管理的基础。
43 6
|
7天前
|
存储 关系型数据库 MySQL
Mysql(4)—数据库索引
数据库索引是用于提高数据检索效率的数据结构,类似于书籍中的索引。它允许用户快速找到数据,而无需扫描整个表。MySQL中的索引可以显著提升查询速度,使数据库操作更加高效。索引的发展经历了从无索引、简单索引到B-树、哈希索引、位图索引、全文索引等多个阶段。
38 3
Mysql(4)—数据库索引
|
9天前
|
SQL Ubuntu 关系型数据库
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
本文为MySQL学习笔记,介绍了数据库的基本概念,包括行、列、主键等,并解释了C/S和B/S架构以及SQL语言的分类。接着,指导如何在Windows和Ubuntu系统上安装MySQL,并提供了启动、停止和重启服务的命令。文章还涵盖了Navicat的使用,包括安装、登录和新建表格等步骤。最后,介绍了MySQL中的数据类型和字段约束,如主键、外键、非空和唯一等。
27 3
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
|
14天前
|
缓存 算法 关系型数据库
Mysql(3)—数据库相关概念及工作原理
数据库是一个以某种有组织的方式存储的数据集合。它通常包括一个或多个不同的主题领域或用途的数据表。
38 5
Mysql(3)—数据库相关概念及工作原理
|
1天前
|
存储 关系型数据库 MySQL
如何在MySQL中创建数据库?
【10月更文挑战第16天】如何在MySQL中创建数据库?
|
5天前
|
SQL Oracle 关系型数据库
安装最新 MySQL 8.0 数据库(教学用)
安装最新 MySQL 8.0 数据库(教学用)
34 4

推荐镜像

更多