[Oracle] efficient PL/SQL programming (5)-call a stored procedure to return a result set

Source: Internet
Author: User
Tags oracleconnection

 

Oracle supports returning the result set after calling the stored procedure through the ref cursor. Using the cursor is much better than returning array variables in terms of memory consumption and time!

Example: Create a package for the database

Create or replace package ref_cur_demo is
Type RC is ref cursor;
Procedure ref_cursor (p_owner in varchar2, p_cursor in out RC );
End ref_cur_demo;

Create or replace package body ref_cur_demo is

Procedure ref_cursor (p_owner in varchar2, p_cursor in out RC)
Is
Begin
Open p_cursor for select object_name, object_type from all_objects where owner = p_owner and rownum <3;
End;

End ref_cur_demo;

C # is used to create a small application. The main code is as follows:

Oracle. dataaccess. Client. oracleconnection oracleconnection1 = new oracleconnection ("Data Source = precolm2; user id = colmtest; Password = colmtest ");
Oracleconnection1.open ();
String strsql = @ "ref_cur_demo.ref_cursor ";

Oracle. dataaccess. Client. oracledataadapter da = new Oracle. dataaccess. Client. oracledataadapter ();
Oracle. dataaccess. Client. oraclecommand cmd = new Oracle. dataaccess. Client. oraclecommand (strsql, oracleconnection1 );
Cmd. commandtype = commandtype. storedprocedure;

Oracle. dataaccess. Client. oracleparameter pram = new Oracle. dataaccess. Client. oracleparameter ("p_owner", Oracle. dataaccess. Client. oracledbtype. varchar2 );
Pram. value = "colmtest ";
Cmd. Parameters. Add (PRAM );

Oracle. dataaccess. Client. oracleparameter pram1 = new Oracle. dataaccess. Client. oracleparameter ("p_cursor", Oracle. dataaccess. Client. oracledbtype. refcursor );
Pram1.direction = parameterdirection. output;
Cmd. Parameters. Add (pram1 );

Da. selectcommand = cmd;

Dataset DS = new dataset ();

Da. Fill (DS );

This. datagrid1.datasource = Ds. Tables [0]. defaultview;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.