VC连接Oracle数据库之ODBC及ADO
来源:互联网 发布:淘宝模板图片素材 编辑:程序博客网 时间:2024/04/29 14:18
一.ODBC连ORACLE:
str.Format( "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=Username;Pwd=asdasd; ")
二.ADO连接ORACLE:
1.客户端一定要安装 Oracle的oledb的驱动。可到以下网址下载(10.版本的大概190M)
http://otn.oracle.com/software/tech/windows/ole_db/content.html
2.要使用Net Configuration Assistant配置好侦听及相关环境
代码
首先,在 stdafx.h 中加入以下代码,导入ado库
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
其次,要确保有 ::CoInitialize(NULL)的调用,以初始化COM.
//类定义
class CDBOp {public: bool ReConnect(); bool CloseConnect(); bool OpenConnect(int dbType, 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; //保存错误信息};
//类实现
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为 Oraclebool CDBOp::OpenConnect(int dBType, CString hostName, CString dBName, CString userName, CString password) { CString strConn; if (dBType =0) //Sql server { strConn = "Provider=SQLOLEDB.1"; strConn+= ";User ID="; strConn+= userName; strConn+= ";Password="; strConn+= password; strConn+= ";Initial Catalog="; strConn+= dBName; strConn+= ";Data Source="; strConn+= hostName; } else if (dBType =1) //Oracle { //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("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("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; } }
- VC连接Oracle数据库之ODBC及ADO
- VC ADO连接Oracle数据库
- VC之ODBC连接ACCESS数据库
- ADO.NET 连接数据库字符串(Oracle、SqlServer、Access、ODBC)
- ADO.NET 连接数据库字符串(Oracle、SqlServer、Access、ODBC)
- ADO.NET 连接数据库(Oracle、SqlServer、Access、ODBC)
- ADO.NET 连接数据库字符串(Oracle、SqlServer、Access、ODBC)
- ADO.NET 连接数据库字符串(Oracle、SqlServer、Access、ODBC)
- ADO.NET 连接数据库字符串小结(Oracle、SqlServer、Access、ODBC)
- VC++6.0 通过ADO连接Oracle数据库
- VC++用ADO连接Oracle数据库
- 浅谈常用数据库方式连接之ADO和ODBC
- ODBC 连接 oracle 数据库
- VC++ADO连接数据库
- VC++ADO连接数据库
- VC++ADO连接数据库
- VC连接ADO数据库
- vc连接ADO数据库
- Firefox 快捷键
- N900词典解决方案
- 谷歌+摩托 大于苹果吗?
- C# 获取调用函数 参数名称和值
- POJ1797 dij的变形。
- VC连接Oracle数据库之ODBC及ADO
- 移动社交:妈妈都会用就是成功
- 科学家发现燃烧蜡烛中包含成千上万颗钻石微粒
- 华为笔试题,是java程序员的来做作吧--->含源代码哦!
- 【转载】【杂谈】女生嫁给电气工程师的10大好处!
- 如何做世界的领导者
- ubuntu下安装mentohost的deb包
- 内核Kconfig与Makefile文件分析
- 转:Javascript 控制 CheckBox 的全选与取消全选