[ Team LiB ]Recipe 2.20 Retrieving Data from an Oracle Package Problem Given an Oracle package that returns multiple result sets for related tables as REF CURSOR data types, you want t
Trang 1[ Team LiB ]
Recipe 2.20 Retrieving Data from an Oracle Package
Problem
Given an Oracle package that returns multiple result sets for related tables as REF
CURSOR data types, you want to access this data using a DataReader and load the data into a DataSet
Solution
Use the data type OracleType.Cursor
The sample code creates a Command for an Oracle package CURSPKG that takes a
Customer ID input parameter The package calls a stored procedure that returns two result sets—Orders and Order Details data from Northwind for the specified customer—
as Oracle REF CURSOR output parameters
A DataAdapter is created from the Command, retrieves the Orders and Order Details result sets, and loads them into a DataSet A relation is created between the tables and the default view for the Orders table is bound to the data grid on the form
Next, a DataReader is created from the Command The Orders and Order Details result sets are displayed in a text box
The Oracle package is shown in Example 2-27, and the package body is shown in
Example 2-28
Example 2-27 File: Packages\CURSPKG
CREATE OR REPLACE PACKAGE CURSPKG
AS
TYPE T_CURSOR IS REF CURSOR;
PROCEDURE GetCustomerOrdersWithDetails (
pCustomerID IN CHAR,
curOrders OUT T_CURSOR,
curOrderDetails OUT T_CURSOR);
END CURSPKG;
Example 2-28 File: Package Bodies\CURSPKG
CREATE OR REPLACE PACKAGE BODY CURSPKG
AS
Trang 2PROCEDURE GetCustomerOrdersWithDetails
(
pCustomerID IN CHAR,
curOrders OUT T_CURSOR,
curOrderDetails OUT T_CURSOR
)
IS
V_CURSOR1 T_CURSOR;
V_CURSOR2 T_CURSOR;
BEGIN
OPEN V_CURSOR1 FOR
SELECT * FROM ORDERS
WHERE CustomerID = pCustomerID;
OPEN V_CURSOR2 FOR
SELECT * FROM ORDERDETAILS
WHERE OrderID IN
(SELECT OrderID FROM ORDERS WHERE
CustomerID = pCustomerID);
curOrders := V_CURSOR1;
curOrderDetails := V_CURSOR2;
END GetCustomerOrdersWithDetails;
END CURSPKG;
The C# code is shown in Example 2-29
Example 2-29 File: OracleRefCursorsForm.cs
// Namespaces, variables, and constants
using System;
using System.Text;
using System.Data;
using System.Data.OracleClient;
//
// Create the connection
OracleConnection conn = new OracleConnection(
ConfigurationSettings.AppSettings["Oracle_ConnectString"]); // Create the command for the Oracle package
OracleCommand cmd = new OracleCommand( );
Trang 3cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "CURSPKG.GetCustomerOrdersWithDetails";
// Add the parameters
cmd.Parameters.Add("pCustomerID", OracleType.Char, 5);
cmd.Parameters.Add("curOrders", OracleType.Cursor).Direction =
ParameterDirection.Output;
cmd.Parameters.Add("curOrderDetails", OracleType.Cursor).Direction =
ParameterDirection.Output;
// Set the Customer ID parameter value to user entry
customerIdTextBox.Text = customerIdTextBox.Text.ToUpper( );
cmd.Parameters["pCustomerID"].Value = customerIdTextBox.Text;
// Create the DataAdapter and table mappings
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.TableMappings.Add("Table", "ORDERS");
da.TableMappings.Add("Table1", "ORDERDETAILS");
// Fill the DataSet from the Oracle package
DataSet ds = new DataSet( );
da.Fill(ds);
// Create a relation
ds.Relations.Add("ORDERS_ORDERDETAILS_RELATION",
ds.Tables["ORDERS"].Columns["ORDERID"],
ds.Tables["ORDERDETAILS"].Columns["ORDERID"]);
// Bind the default view for the Orders table to the grid
dataGrid.DataSource = ds.Tables["ORDERS"].DefaultView;
// Create the DataReader from the Oracle package
conn.Open( );
OracleDataReader dr = cmd.ExecuteReader( );
// Output the Orders table for the customer
StringBuilder result = new StringBuilder("ORDERS" + Environment.NewLine); while(dr.Read( ))
{
for(int i = 0; i < dr.FieldCount; i++)
result.Append(dr[i].ToString( ) + "; ");
Trang 4result.Append(Environment.NewLine);
}
// Move to the REF CURSOR for the next result set
dr.NextResult( );
// Output the Order Details for the customer
result.Append(Environment.NewLine + "ORDER DETAILS" + Environment.NewLine); while(dr.Read( ))
{
for(int i = 0; i < dr.FieldCount; i++)
result.Append(dr[i].ToString( ) + "; ");
result.Append(Environment.NewLine);
}
conn.Close( );
// Output the result
resultTextBox.Text = result.ToString( );
Discussion
You cannot use a collection of SQL statements as a batch query within an Oracle stored procedure Instead, you must use an Oracle package, that is, a container that groups stored procedures and functions An Oracle package consists of a header and a body The package header defines the name of the package and provides method signatures for each procedure or function in the package The package body contains the code for the stored procedures and functions defined in the package header
A REF CURSOR is an Oracle data type that points into a result set returned by a query A REF CURSOR differs from a normal cursor in that while a cursor points to a specific result set, a REF CURSOR is a variable that can point to different result sets—a reference to a
cursor—and can be assigned at execution time
A REF CURSOR is typically used to pass result sets from a stored procedure to a client
You can access a result using an output parameter that references an Oracle REF
CURSOR The parameter name must match the name of the REF CURSOR and it must have the data type OracleType.Cursor
Trang 5If the package returns more than one REF CURSOR parameter, an OracleDataReader accesses them in the order they were added to the parameters collection The NextResult( ) method of the DataReader advances to the next REF CURSOR
[ Team LiB ]