大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
一.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为 Oracle
bool 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;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/170075.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...