您的位置:首页 > 数据库 > Oracle

Retrieve multiple Oracle Ref Cursor using .NET data Provider for Oracle

2008-06-19 21:25 411 查看
Introduction
InmyearlierarticleMultipleResultSetsinADO.netusingSqlClient,wehaveseenhowtoretrievemultipleresultsusingSqlclientagainstSQLServer.Thiswasafairlysimpleandstraightforward.WecanachievethesameagainstOracledatabasewithaslightdifference.Weneedtoironoutcoupleofthingsbeforewegettonutsandboltsofit.

WeneedSystem.Data.OracleClient.DllassemblythatisverysimilartoSystem.Data.SqlClient.Dll,.NETManagedProviderforOracleisnotapartof.NETVersion1.0,aseparatedownloadclickheretodownload.For.NETversion1.1,it’sgoingtobepartofthebundle.Ifyouarenewto.NetframeworkproviderforOraclereadmyarticlesoncode101Boostperformancewith.netdataproviderfororacle

WeneedastoredproceduretoreturnmultipleresultssetusingREFCURSOR.Fornovice,ArefcursorisaPL/SQLdatatypethatyoucanuseinaquerytofetchdata.EachrefcursorqueryisassociatedwithaPL/SQLfunctionthatreturnsastronglytypedrefcursor.

Letsgetfeetdirty
Createaoraclepackage,IassumeyouknowwhatapackagebythewayithastwopartsSpecificationandbody
PackageSpecification
CREATEORREPLACEPACKAGEPKG_MUltiResultsetas

TYPEMyRefCurisREFCURSOR;

procedureGetReadOnlyData(EmpCurOUTMyRefCur,

DeptCurOUTMyRefCur,

SalCurOUTMyRefCur);

END;

PackagebodyCREATEORREPLACEPACKAGEBODYPKG_MUltiResultsetas

PROCEDUREGetReadOnlyData(EmpCurOUTMyRefCur,

DeptCurOUTMyRefCur,

SalCurOUTMyRefCur)

IS

BEGIN

openEmpCurforselect*fromemp;

openDeptCurforselect*fromdept;

openSalCurforselect*fromsalgrade;

END;

END;
Letsusgettotheclientparttoaccessthisstoredprocedure.WewillestablishconnectiontoOracledatabaseretrievedatafromthreetablesinoneshotandfillupthedatasetandbindthedatatoDataGrids.IamusingDatasetyoucantryitwithOracleDataReaderaswell.
NOTE:ThePrefixisalwaysOracleforalltheclassesinSyatem.Data.OracleClientnamespace.Referencenamespaces


usingSystem.Data.OracleClient;

usingSystem.Data;
PrivatevoidGetData_Click(objectsender,System.EventArgse)

{

try

{

//ConnecttoDatabase

OracleConnectionoCon=newOracleConnection

("Datasource=Firstdb;userid=scott;password=tiger");

oCon.Open();

//CommandObject

OracleCommandoCmd=newOracleCommand

("PKG_MUltiResultset.GetReadOnlyData",oCon);

//StoredProcedure

oCmd.CommandType=CommandType.StoredProcedure;

//CreateParameterObject

oCmd.Parameters.Add(newOracleParameter

("empcur",OracleType.Cursor)).Direction=ParameterDirection.Output;

oCmd.Parameters.Add(newOracleParameter

("DeptCur",OracleType.Cursor)).Direction=ParameterDirection.Output;

oCmd.Parameters.Add(newOracleParameter

("SalCur",OracleType.Cursor)).Direction=ParameterDirection.Output;

//InstatiateDataset

DataSetDs=newDataSet();

//InstatiateDataAdopter

OracleDataAdapteroAdp=newOracleDataAdapter(oCmd);

//FillDataSet

oAdp.Fill(Ds);

//BindDatatoGrids

dataGrid1.SetDataBinding(Ds,"Table");

dataGrid2.SetDataBinding(Ds,"Table1");

dataGrid3.SetDataBinding(Ds,"Table2");

}

catch(Exceptionx)

{

MessageBox.Show(x.Message);

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: