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

來源:互聯網
上載者:User
Introduction

In my earlier article Multiple Result Sets in ADO.net using SqlClient ,we have seen how to retrieve multiple results using Sqlclient against SQL Server. This was a fairly simple and straight forward. We can achieve the same against Oracle database with a slight difference. We need to iron out couple of things before we get to nuts and bolts of it.

  • We need System.Data.OracleClient.Dll assembly that is very similar to System.Data.SqlClient.Dll,.NET Managed Provider forOracle is not a part of .NET Version 1.0, a separate download click here to download . For .NET version 1.1, it’s going to be part of the bundle. If you are new to .Net framework provider for Oracle read my articles on code101 Boost performance with .net data provider for oracle
  • We need a stored procedure to return multiple results set using REF CURSOR. For novice, A ref cursor is a PL/SQL data type that you can use in a query to fetch data. Each ref cursor query is associated with a PL/SQL function that returns a strongly typed ref cursor.
Lets get feet dirty

Create a oracle package, I assume you know what a package by the way it has two parts Specification and body

Package Specification
CREATE OR REPLACE PACKAGE PKG_MUltiResultset as TYPE MyRefCur is REF CURSOR; procedure GetReadOnlyData(EmpCur OUT MyRefCur,  DeptCur OUT MyRefCur,  SalCur OUT MyRefCur);END;
Package body
CREATE OR REPLACE PACKAGE BODY PKG_MUltiResultset as  PROCEDURE GetReadOnlyData(EmpCur OUT MyRefCur,      DeptCur OUT MyRefCur,   SalCur OUT MyRefCur)  IS  BEGIN     open EmpCur for select * from emp;    open DeptCur for select * from dept;    open SalCur for select * from salgrade;  END; END;

Lets us get to the client part to access this stored procedure. We will establish connection to Oracle database retrieve data from three tables in one shot and fill up the dataset and bind the data to DataGrids.I am using Dataset you can try it with OracleDataReader as well.
NOTE: The Prefix is always Oracle for all the classes in Syatem.Data.OracleClient name space. Reference name spaces

using System.Data.OracleClient;using System.Data;
 Private void GetData_Click(object sender, System.EventArgs e)  {    try      {//Connect to DatabaseOracleConnection oCon=new OracleConnection        ("Data source=Firstdb;user id=scott;password=tiger"); oCon.Open();        //Command ObjectOracleCommand oCmd =new OracleCommand        ("PKG_MUltiResultset.GetReadOnlyData",oCon);//Stored ProcedureoCmd.CommandType=CommandType.StoredProcedure;//Create Parameter Object        oCmd.Parameters.Add(new OracleParameter        ("empcur",OracleType.Cursor)).Direction=ParameterDirection.Output;             oCmd.Parameters.Add(new OracleParameter        ("DeptCur",OracleType.Cursor)).Direction=ParameterDirection.Output;             oCmd.Parameters.Add(new OracleParameter       ("SalCur",OracleType.Cursor)).Direction=ParameterDirection.Output;//Instatiate DatasetDataSet Ds=new DataSet();//Instatiate Data AdopterOracleDataAdapter oAdp=new OracleDataAdapter(oCmd);//Fill Data SetoAdp.Fill(Ds);//Bind Data to GridsdataGrid1.SetDataBinding(Ds,"Table");dataGrid2.SetDataBinding(Ds,"Table1");dataGrid3.SetDataBinding(Ds,"Table2");}   catch (Exception x){ MessageBox.Show(x.Message);}      }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.