我的访问oracle存储过程的例子
新建一个dialog工程,工程名为oracle上面放一个button;
在stdafx.h最后加上:
#include <comdef.h>
#import "c:\program files\common files\system\ado\msado20.tlb"\
no_namespace rename ("EOF", "adoEOF")
在oracle.cpp最后加上:
long COracleApp::InitOracle()
{
try
{
m_connectionptr.CreateInstance (__uuidof(Connection));
_bstr_t strConnect = _T("Provider=MSDAORA.1;Password=manager;User ID=system;Data Source=cdwz;Persist Security Info=True");
m_connectionptr->Open (strConnect,"system","manager",-1);
}
catch(...)
{
DisplayAdoError();
}
return 0;
}
long COracleApp::DisplayAdoError()
{
long errorcount=m_connectionptr->GetErrors ()->GetCount ();
_bstr_t add;
CString errormsg;
for (short i=0;i<errorcount;i++)
{
add=m_connectionptr->GetErrors ()->GetItem (COleVariant((short)i))->GetDescription ();
errormsg.Insert (0,(char*)add);
errormsg+="\r";
}
AfxMessageBox(errormsg);
return 0;
}
在oracle.h中加上
public:
long DisplayAdoError();
long InitOracle();
COracleApp();
修改BOOL COracleApp::InitInstance()
加上:
AfxOleInit();
InitOracle();
COracleDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
在oracledlg.cpp中修改onbutton1:
COracleApp *papp=(COracleApp*)AfxGetApp();
try
{
_bstr_t sqltext="{call for_result.pro_test(?,?,{resultset 0, p_cur})}";
_CommandPtr m_commandptr;
_RecordsetPtr m_pUserSet;
m_commandptr.CreateInstance (__uuidof(Command));
m_pUserSet.CreateInstance (__uuidof(Recordset));
m_commandptr->ActiveConnection =papp->m_connectionptr ;
m_commandptr->CommandText =sqltext;
m_commandptr->CommandType =adCmdText;
_ParameterPtr ParameterPtr1=NULL,ParameterPtr2=NULL;
ParameterPtr1=m_commandptr->CreateParameter ("id",adInteger,adParamInput,4,COleVariant((short)1));
m_commandptr->Parameters ->Append (ParameterPtr1);
ParameterPtr2=m_commandptr->CreateParameter ("tablename",adVarChar,adParamInputOutput,255,COleVariant("ff" ));
m_commandptr->Parameters ->Append (ParameterPtr2);
m_pUserSet=m_commandptr->Execute (NULL,NULL,adCmdUnknown);
//如果说没有结果集,则换成
//m_commandptr->Execute (NULL,NULL,adCmdUnknown);
// 至于你说的返回成功标志,可以放在输出参数里
_bstr_t result=m_commandptr->Parameters->GetItem("tablename")->GetValue();
if (!m_pUserSet->adoEOF )
{
long count=m_pUserSet->Fields ->GetCount ();
long iii=m_pUserSet->GetCollect (COleVariant((short)0)).intVal ; }
}
catch(...)
{
papp->DisplayAdoError();
}
oralce的存储过程源程序:
create table a (id integer,addr varchar2);
create or replace package for_result is
type TypCur is ref cursor;
procedure pro_test(p_id number,result in out varchar2, p_cur out TypCur);
end for_result;
create or replace package body for_result is
procedure pro_test(p_id number,result in out varchar2, p_cur out TypCur)
is
begin
open p_cur for select * from a where id>p_id;
result :='pretty pretty gril';
end;
end for_result;