国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

Oracle 10g Release2新功能之Ref Cursor

2024-08-29 13:42:02
字體:
供稿:網(wǎng)友

  Ref Cursor就是我們定義在服務器端的結(jié)果集的reference。 當我們打開一個Ref Cursor的時候,沒有任何的數(shù)據(jù)返回到客戶端,相反,數(shù)據(jù)在服務器上的地址將會被返回到客戶端。這樣用戶就可以自己決定什么時間和以那種方式通過Ref Cursor去取數(shù)據(jù)。 

  在以前版本的ODP.NET中,我們可以通過Ref Cursor取數(shù)據(jù),但是我們不能把Ref Cursor作為一個Input參數(shù)傳遞給PL/SQL的存儲過程和存儲函數(shù)。但是在Oracle Database 10g Release2,我們能夠很簡單的把Ref Cursor作為Input參數(shù)傳遞給PL/SQL的存儲過程和存儲函數(shù)。這是Oracle Database 10g Release2的新功能。

  我們接下來就以例程的方式來向你介紹這個新功能。

  預備數(shù)據(jù)庫

  我們要在數(shù)據(jù)庫中生成一個表和一個包,我們接下來的例子會用到。 

  請用HR用戶登錄數(shù)據(jù)庫,然后運行下面的腳本。

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;

/
  創(chuàng)建.NET代碼

  數(shù)據(jù)庫已經(jīng)預備好了,接下來我們就預備創(chuàng)建.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的新特性,使你的項目更加的穩(wěn)定,效率更高。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 上杭县| 叙永县| 盐山县| 普兰县| 福清市| 鄂伦春自治旗| 咸阳市| 台中县| 莱西市| 军事| 垦利县| 建宁县| 施甸县| 永寿县| 芮城县| 扎兰屯市| 泸定县| 浑源县| 甘肃省| 遂溪县| 中牟县| 靖安县| 大庆市| 治多县| 达尔| 辽源市| 南丹县| 朝阳县| 舟山市| 余江县| 方城县| 茶陵县| 格尔木市| 江孜县| 平武县| 松溪县| 建湖县| 汽车| 东辽县| 德令哈市| 湘乡市|