1、新建一个基于对话框的工程zjy,在stdafx.h中所有的#include后加:
#imp
2、在zjydlg.h中声明 :
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
3、
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
//没有配置oracle客户端情况下ado连接字符串
CString m_sConn="Provider=OraOLEDB.Oracle.1;Password=platform1234;Persist Security Info=True;User ID=platform;Da
//配置好了oracle客户端 情况下ado连接字符串
// CString m_sConn="Provider=OraOLEDB.Oracle.1;Password=pwd;Persist Security Info=True;User ID=userid;server=serveraddress;Da
m_pConnection->Open((_bstr_t)m_sConn,"","",adConnectUnspecified);
//下面是查询数据的例子
CString strSql="select * from admin";
BSTR bstrSQL = strSql.AllocSysString();
m_pRecordset->Open(bstrSQL,(IDispatch*) m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t TheValue; //VARIANT数据类型
while(!m_pRecordset->adoEOF)
{
i=i+1;
TheValue= m_pRecordset->GetCollect("name");//得到字段BIG_NAME的值
if(TheValue.vt!=VT_NULL)
m_edit+=(char*)_bstr_t(TheValue);
UpdateData (false);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pConnection->Close();
}
catch (_com_error e)//异常处理
{
AfxMessageBox(e.Description());
long errorCode=e.WCode();
if(3127==errorCode) AfxMessageBox("表不存在");
if(3092==errorCode) AfxMessageBox("表已经存在");
return ;
}
红色部分为ado的连接字符串。