Click here to Skip to main content
15,878,748 members
Articles / Web Development / ASP.NET

Returning Multiple Ref Cursors from Oracle using DAAB

Rate me:
Please Sign up or sign in to vote.
3.87/5 (10 votes)
9 Jun 2009CPOL2 min read 63.6K   503   15   19
How to return multiple ref cursors from Oracle using DAAB

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:
    C#
    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:
    C#
    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:

C#
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)


Written By
Web Developer
India India
g

Comments and Discussions

 
AnswerReturning Multiple Ref Cursors from Oracle only not using DAAB Pin
Girish Lohiya13-Oct-11 4:23
Girish Lohiya13-Oct-11 4:23 
QuestionProblem with results array size when calling getStoredProcCommand [modified] Pin
Andrei D.O14-Jul-11 6:56
Andrei D.O14-Jul-11 6:56 
SuggestionRe: Problem with results array size when calling getStoredProcCommand Pin
Andrei D.O14-Jul-11 7:10
Andrei D.O14-Jul-11 7:10 
GeneralRe: Problem with results array size when calling getStoredProcCommand Pin
Andrei D.O25-Jul-11 4:35
Andrei D.O25-Jul-11 4:35 
GeneralThanks! Pin
Nate West30-Apr-10 5:40
Nate West30-Apr-10 5:40 
GeneralGood article Pin
reachpranish29-Mar-10 12:59
reachpranish29-Mar-10 12:59 
GeneralMy vote of 1 Pin
Pete O'Hanlon10-Jun-09 11:32
mvePete O'Hanlon10-Jun-09 11:32 
GeneralRe: My vote of 1 Pin
himanshu256110-Jun-09 18:55
himanshu256110-Jun-09 18:55 
GeneralRe: My vote of 1 Pin
Pete O'Hanlon10-Jun-09 22:00
mvePete O'Hanlon10-Jun-09 22:00 
GeneralRe: My vote of 1 Pin
himanshu256110-Jun-09 23:02
himanshu256110-Jun-09 23:02 
GeneralRe: My vote of 1 Pin
Pete O'Hanlon10-Jun-09 23:37
mvePete O'Hanlon10-Jun-09 23:37 
AnswerRe: My vote of 1 Pin
himanshu256111-Jun-09 1:02
himanshu256111-Jun-09 1:02 
GeneralRe: My vote of 1 Pin
LikeABanshee17-Jul-09 3:54
LikeABanshee17-Jul-09 3:54 
GeneralRe: My vote of 1 Pin
himanshu256117-Jul-09 19:38
himanshu256117-Jul-09 19:38 
GeneralRe: My vote of 1 Pin
bitmulticoder20-Jul-12 10:43
bitmulticoder20-Jul-12 10:43 
Yeah,
And while you are at it writing code for me because I cannot interpret the code here and then apply it to my situation, can you please provide a fully detailed and commented version of this code using Ruby, Java and SQL Lite, and perhaps I might need to use Python and Cocoa also, so please provide these also before I have to start berating people.
You tell em Petey!!

(seething with sarcasm for those who cannot figure this out)
GeneralRe: My vote of 1 Pin
ChrisOnNet20-Jul-09 8:44
ChrisOnNet20-Jul-09 8:44 
GeneralRe: My vote of 1 Pin
himanshu256120-Jul-09 19:21
himanshu256120-Jul-09 19:21 
GeneralRe: My vote of 1 Pin
MoeInsairat9-Aug-09 7:21
MoeInsairat9-Aug-09 7:21 
GeneralRe: My vote of 1 Pin
himanshu25619-Aug-09 19:36
himanshu25619-Aug-09 19:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.