中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 数据库开发 > 数据库综合
Oracle 10g Release2新功能之Ref Cursor
作者:小溪明净 时间:2006-08-23 01:30 出处:天极网 责编:月夜寒箫
              摘要:Oracle 10g Release2新功能之Ref Cursor
  Ref Cursor就是我们定义在服务器端的结果集的reference。 当我们打开一个Ref Cursor的时候,没有任何的数据返回到客户端,相反,数据在服务器上的地址将会被返回到客户端。这样用户就可以自己决定什么时间和以那种方式通过Ref Cursor去取数据。 

  在以前版本的ODP.NET中,我们可以通过Ref Cursor取数据,但是我们不能把Ref Cursor作为一个Input参数传递给PL/SQL的存储过程和存储函数。但是在Oracle Database 10g Release2,我们能够很简单的把Ref Cursor作为Input参数传递给PL/SQL的存储过程和存储函数。这是Oracle Database 10g Release2的新功能。

  我们接下来就以例程的方式来向你介绍这个新功能。

  准备数据库

  我们要在数据库中生成一个表和一个包,我们接下来的例子会用到。 

  请用HR用户登录数据库,然后运行下面的脚本。

create table processing_result
(
 status varchar2(64)
);

create or replace package cursor_in_out as
type emp_cur_type is ref cursor return employees%rowtype;

procedure process_cursor(p_cursor in emp_cur_type);

end;

/

create or replace package body cursor_in_out as

procedure process_cursor(p_cursor in emp_cur_type) is
employee employees%rowtype;

begin
 loop
  fetch p_cursor into employee;
  exit when p_cursor%notfound;
  insert into processing_result
  values('Processed employee #' ||
  employee.employee_id || ': ' ||
  employee.first_name || ' ' ||
  employee.last_name);
 end loop;
end;
end;

/

  创建.NET代码

  数据库已经准备好了,接下来我们就准备创建.NET代码。

using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace CursorInCursorOut
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 class Class1
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   // create connection to database
   // change for your environment
   string constr = "User Id=hr; Password=hr; Data Source=oramag; Pooling=false";
   OracleConnection con = new OracleConnection(constr);
   con.Open();

   // command and parameter objects to get ref cursor
   OracleCommand cmd = con.CreateCommand();
   cmd.CommandText = "begin open :1 for select * from employees where manager_id=101; end;";
   OracleParameter p_rc = cmd.Parameters.Add("p_rc", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);

  // get the ref cursor
  cmd.ExecuteNonQuery();

  // clear parameters to reuse
  cmd.Parameters.Clear();

  // command and parameter objects to pass ref cursor
  // as an input parameter
  cmd.CommandText = "cursor_in_out.process_cursor";
  cmd.CommandType = CommandType.StoredProcedure;
  OracleParameter p_input = cmd.Parameters.Add("p_input", OracleDbType.RefCursor, p_rc.Value,  ParameterDirection.Input);

  // process the input cursor
  cmd.ExecuteNonQuery();

  // clean up objects
  p_input.Dispose();
  p_rc.Dispose();
  cmd.Dispose();
  con.Dispose();
 }
}
}

  运行上面的代码,这个程序本身没有输出,但是我们可以通过SQL*PLUS很容易可以看到下面的输出。

SQL> select * from processing_result;

STATUS

----------------------------------------

Processed employee #108: Nancy Greenberg
Processed employee #200: Jennifer Whalen
Processed employee #203: Susan Mavris
Processed employee #204: Hermann Baer
Processed employee #205: Shelley Higgins

5 rows selected.

  我这里只是给大家一个很简单的例子,希望大家充分应用Oracle Database的新特性,使你的项目更加的稳定,效率更高。
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有