Click here to Skip to main content
6,629,885 members and growing! (23,077 online)
Email Password   helpLost your password?
Database » Database » Oracle     Intermediate License: The Code Project Open License (CPOL)

Returning Multiple Ref Cursors from Oracle using DAAB

By himanshu2561

How to return multiple ref cursors from Oracle using DAAB
C# (C# 1.0, C# 2.0, C# 3.0), .NET, ASP.NET, Oracle, DBA, Dev
Version:4 (See All)
Posted:9 Jun 2009
Views:4,767
Bookmarked:8 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.68 Rating: 3.86 out of 5
1 vote, 11.1%
1

2

3

4
8 votes, 88.9%
5

Introduction

This article explains how to work with Oracle stored procedure which returns multiple ref cursors using Data Access Application Block (DAAB) of Enterprise Library.

Background

DAAB is used in a variety of situations such as reading data for display, passing data through application layers, and submitting changed data back to the database system. It includes support for both stored procedures and in-line SQL. In other words, Data Access Application Block provides access to the most often used features of ADO.NET in simple-to-use classes.

Using the Code

(1) For Procedure Returning Single Ref Cursor

  • Add the reference to Microsoft.Practices.EnterpriseLibrary.Data.
  • Create the database object.
  • Create the DbCommand object and pass the name of the stored procedure.
  • If stored procedure takes any input parameter, pass that as shown in below code:
    Database db = DatabaseFactory.CreateDatabase("Connection String");
    DbCommand cmd = db.GetStoredProcCommand("PKG_TEST.EMAIL");
    db.AddInParameter(cmd, "varMODULEID", DbType.String);
    db.SetParameterValue(cmd, "varMODULEID", 1);
    DataSet ds = db.ExecuteDataSet(cmd);
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind(); 

Now, stored procedure "PKG_TEST.EMAIL" takes one input parameter varMODULEID and returns a single ref cursor. But as in the above code, I have not created any parameter for ref cursor.

So, the question arises, how it will come to know about the ref cursor ????

The answer is in the code of DAAB.

When we call the stored procedure which returns a single ref cursor, DAAB automatically creates a parameter of type cursor. The name of the automatically created parameter is cur_OUT. Here, one point should be kept in mind that the name of the ref cursor in your stored procedure must be cur_OUT. So, in case of stored procedure which returns single ref cursor, you need not pass ref cursor as parameter to DbCommand object.

(2) For Procedure Returning Multiple Ref Cursors

  • Add the reference to Microsoft.Practices.EnterpriseLibrary.Data.
  • Create the database object.
  • Create the DbCommand object and pass the name of the stored procedure.
  • If stored procedure takes any input parameter, set the value of that as shown in below code:
    Database db = DatabaseFactory.CreateDatabase("Connection String");
    object[] results = new object[3];
    DbCommand cmd = db.GetStoredProcCommand("PKG_TEST.DEPARTMENT", results);
    db.SetParameterValue(cmd, "varDEPTID", 1);
    DataSet ds = db.ExecuteDataSet(cmd);
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
    GridView2.DataSource = ds.Tables[1];
    GridView2.DataBind();

When you create a DbCommand object for a stored procedure that returns multiple cursors, you must pass an array of objects to the GetStoredProcCommand method. The size of the array must be equal to the number of parameters required by the stored procedure.

In this case, store procedure "PKG_TEST.DEPARTMENT" takes one input parameter and returns two ref cursors. Here:

object[] results = new object[3];

I create an array of size three which signifies that my stored procedure requires three parameters.

So, when your stored procedure returns more than one ref cursor, you have to create an array of object where size of array should be equal to the number of parameters taken by your stored procedure.

History

  • 9th June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

himanshu2561


Member
g
Occupation: Web Developer
Location: India India

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmvpPete O'Hanlon12:32 10 Jun '09  
GeneralRe: My vote of 1 Pinmemberhimanshu256119:55 10 Jun '09  
GeneralRe: My vote of 1 PinmvpPete O'Hanlon23:00 10 Jun '09  
GeneralRe: My vote of 1 Pinmemberhimanshu25610:02 11 Jun '09  
GeneralRe: My vote of 1 PinmvpPete O'Hanlon0:37 11 Jun '09  
AnswerRe: My vote of 1 Pinmemberhimanshu25612:02 11 Jun '09  
GeneralRe: My vote of 1 PinmemberLikeABanshee4:54 17 Jul '09  
GeneralRe: My vote of 1 Pinmemberhimanshu256120:38 17 Jul '09  
GeneralRe: My vote of 1 PinmemberChrisOnNet9:44 20 Jul '09  
GeneralRe: My vote of 1 Pinmemberhimanshu256120:21 20 Jul '09  
GeneralRe: My vote of 1 PinmemberEstarta8:21 9 Aug '09  
GeneralRe: My vote of 1 Pinmemberhimanshu256120:36 9 Aug '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by himanshu2561
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project