博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle利用游标返回结果集的的例子(C#)...(最爱)
阅读量:4473 次
发布时间:2019-06-08

本文共 4464 字,大约阅读时间需要 14 分钟。

引用地址:http://www.alixixi.com/program/a/2008050727634.shtml
 
本例在VS2005+Oracle 92010 + WindowsXp Sp2测试通过
1、创建一个游标变量,为返回值使用
create or replace package types as
  type cursorType is ref cursor;
end;
2、创建函数(或者存储过程)
create or replace function testpro return types.cursorType is
lc types.cursorType;
begin
  open lc for select * from test;
  return lc;
end testpro;
3、编写C#程序(注意:要先应用System.Data.OracleClient)
            OracleConnection conn = new OracleConnection("YourConnectString");
            OracleCommand cmd = new OracleCommand("testpro", conn);
            cmd.CommandType = CommandType.StoredProcedure;
 
            OracleParameter op = new OracleParameter("c", OracleType.Cursor);
            op.Direction = ParameterDirection.ReturnValue; 
            cmd.Parameters.Add(op);
 
            DataSet ds = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter(cmd);
 
            da.Fill(ds,"test");
 
            this.dataGridView1.DataSource = ds.Tables["test"];
PS:使用储过程方法类似。
 
1、创建一个游标变量,为返回值使用

CREATE OR REPLACE PACKAGE types_mei1

AS
TYPE myrctype1 IS REF CURSOR;

PROCEDURE get (p_id NUMBER, p_rc OUT myrctype1);

END types_mei1 ;

 

2、创建函数(或者存储过程)

create or replace function testpro_mei1(IV IN NUMBER) return types_mei1.myrctype1 is
lc types_mei1.myrctype1;
begin
open lc for select * from classes where ID=IV;
return lc;
end testpro_mei1;

 

=====================================下面是对通过单个ID查询的数据

 

create or replace package types_mei as

type cursorType is ref cursor;
end;

=============================================

CREATE OR REPLACE PACKAGE types_mei

AS
TYPE myrctype IS REF CURSOR;

PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);

END types_mei ;

 

create or replace function testpro_mei(IV IN NUMBER) return types_mei.myrctype is

lc types_mei.myrctype;
begin
open lc for select * from test where ID=IV;
return lc;
end testpro;

=================================================

CREATE OR REPLACE PACKAGE types_mei1

AS
TYPE myrctype1 IS REF CURSOR;

PROCEDURE get (p_id NUMBER, p_rc OUT myrctype1);

END types_mei1 ;

 

create or replace function testpro_mei1(IV IN NUMBER) return types_mei1.myrctype1 is
lc types_mei1.myrctype1;
begin
open lc for select * from classes where ID=IV;
return lc;
end testpro_mei1;

===================================================

 

 

===================================

存储过程:

插入数据:
CREATE OR REPLACE Procedure p_insert_t_cls --存储过程名称
(
p_stuid in CLASSES.ID%type,
p_stuname in varchar
)
as
BEGIN
insert into classes
values
(p_stuid,p_stuname);
commit;
end;
===============================================
删除 :(带返回参数)
create or replace procedure proc_delete
(
isid in number , P_ROWS OUT NUMBER
)
is
begin
delete classes where id=isid;
If SQL%Found Then
DBMS_OUTPUT.PUT_LINE('删除成功!');
P_ROWS := 1;
Else
DBMS_OUTPUT.PUT_LINE('删除失败!');
P_ROWS := 0;
End If;
commit;
end
;

删除 : (不带返回参数)

create or replace procedure p_delete_t_cls1(
cla_id in Number
)
is
begin
DELETE FROM classes WHERE id = cla_id;
commit;
end p_delete_t_cls1;

删除 : (不带返回参数)指定ID删除

create or replace procedure p_delete_t_cls is
begin
DELETE FROM classes WHERE id = 7;
commit;
end p_delete_t_cls;
====================================================

修改数据:(不带返回参数)

create or replace procedure p_update_t_cls1(
p_stuid in Number,
p_stuname in Nvarchar2
)
is
begin
update classes x set x.classname = p_stuname where x.id = p_stuid;
commit;
end p_update_t_cls1;

修改数据: :(带返回参数)

create or replace procedure proc_update(

p_stuid in Number,
p_stuname in Nvarchar2,
P_ROW out number
)
is
begin
update classes set classname = p_stuname where id = p_stuid;
If SQL%Found Then
DBMS_OUTPUT.PUT_LINE('更新成功!');
P_ROW := 1;
Else
DBMS_OUTPUT.PUT_LINE('更新失败!');
P_ROW := 0;
End If;
commit;
end proc_update;

修改数据: : (不带返回参数)指定ID修改

create or replace procedure p_update_t_cls
is
begin
update classes x set x.classname = '44' where x.id = 3;
commit;
end p_update_t_cls;

====================================================

查询所有数据:(带返回参数 游标)

create or replace package types1 as
type cursorType1 is ref cursor;
end;

create or replace function testpro1 return types1.cursorType1 is

lc1 types1.cursorType1;
begin
open lc1 for select id,classname from classes;
return lc1;
end testpro1;

 

传递ID查询数据:(带返回参数 游标)传递ID查询数据

CREATE OR REPLACE PACKAGE pkg_test1
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);
END pkg_test1 ;

create or replace function testpro(IV IN NUMBER) return types.cursorType is
lc types.cursorType;
begin
open lc for select * from classes where ID=IV;
return lc;
end testpro;
====================================================

转载于:https://www.cnblogs.com/meimao5211/p/3387921.html

你可能感兴趣的文章
SignalR主动通知订阅者示例
查看>>
用python实现矩阵转置
查看>>
linux 小技巧(磁盘空间搜索)
查看>>
iOS开发——捕获崩溃信息
查看>>
(for 循环)编程找出四位整数 abcd 中满足 (ab+cd)(ab+cd)=abcd 的数
查看>>
tomcat使用spring-loaded实现应用热部署
查看>>
boost1.53中的lock-free
查看>>
链表_leetcode203
查看>>
基于ajax 的 几个例子 session ,ajax 实现登录,验证码 ,实现ajax表单展示
查看>>
连接不上sql server服务器的解决方案
查看>>
c3po数据库连接池中取出连接
查看>>
bootstrap-table 分页
查看>>
使用本机IP调试web项目
查看>>
【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
查看>>
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>
Ubuntu各种软件的安装
查看>>
java开发环境搭建-慕课网
查看>>
NOIP2015-D2T3运输计划
查看>>
Z :彻底了解指针数组,数组指针以及函数指针 [复
查看>>