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


    #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

    本人新博客网址为:http://www.hizds.com
    本博客注有“转”字样的为转载文章,其余为本人原创文章,转载请务必注明出处或保存此段。c++/lua/windows逆向交流群:69148232
  • 相关阅读:
    usb驱动之打印usb设备信息(一)
    驱动的分离分层设计——按键点灯
    三极管(7)之开关电路详解
    电阻(4)之上拉电阻与下拉电阻详解
    电感的能量储存在哪里-史上最深度的解析(6)
    电感的能量储存在哪里-深度解析(4)
    电容(2)旁路电容工作原理深度解析
    555定时器(1)单稳态触发器电路及Multisim实例仿真
    N沟通场效应管深度图解(1)工作原理及Multisim实例仿真
    开关电源(1)之BUCK降压变换器工作原理及Multisim实例仿真
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/2366136.html
Copyright © 2020-2023  润新知