ASP.NET调用oracle存储过程返回多个游标

利用oracle可以返回多个记录集。

首先创建程序包,再创建程序包主体。

利用存储过程返回多个记录集。

--创建包

create or replace package pack_siit is
 type t_cursor is ref cursor;--定义游标变量
 type t_scan is ref cursor;
 type t_send is ref cursor;
 type t_receive is ref cursor;
 type t_review is ref cursor;
 type t_locked is ref cursor;
 type t_confirm is ref cursor;
 procedure getFlowTrack
           (var_barcode in varchar2,
          cur_FirstTrial out t_cursor,
          cur_Scan out t_scan,
          cur_Send out t_send,
          cur_Receive out t_receive,
          cur_Review out t_review,
          cur_Locked out t_locked,
          cur_Confirm out t_confirm);
   end pack_siit;

--创建包主体

create or replace package body pack_siit is
  procedure getFlowTrack
    (var_barcode in varchar2,
    cur_FirstTrial out t_cursor,
    cur_Scan out t_scan,
    cur_Send out t_send,
    cur_Receive out t_receive,
    cur_Review out t_review,
    cur_Locked out t_locked,
    cur_Confirm out t_confirm)
    as
     begin
    open cur_FirstTrial for
     select  * from a where id=var_barcode;  
     -- return 1
     open cur_Scan for
     select  * from b where id=var_barcode;
     --2
     open cur_Send for
    select  * from c where id=var_barcode;
    --3
    open cur_Receive for
    select  * from d where id=var_barcode;
     --4
    open cur_Review for
    select  * from e where id=var_barcode;
     --5
    open cur_Confirm for
    select  * from f where id=var_barcode;
      --6
    open cur_Locked for
    select  * from g where id=var_barcode;

    end getFlowTrack;
   end pack_siit;

 

ASP.NET里调用

OracleConnection connection = new OracleConnection(connectionString);
                --连接
                connection.Open();
                OracleCommand command = new OracleCommand();
                command.Connection = connection;
                command.CommandText = "PACK_SIIT.getFlowTrack";--存储过程名
                command.CommandType = CommandType.StoredProcedure;--设置执行为存储过程
                command.Parameters.Add("var_barcode", OracleType.VarChar, 50);
                command.Parameters.Add("cur_FirstTrial", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters.Add("cur_Scan", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters.Add("cur_Send", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters.Add("cur_Receive", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters.Add("cur_Review", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters.Add("cur_Confirm", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters.Add("cur_Locked", OracleType.Cursor).Direction = ParameterDirection.Output;
                command.Parameters["var_barcode"].Value = stBarcode;// "DH448400001308120004CB1";
                OracleDataAdapter daReader = new OracleDataAdapter(command);
                DataSet ds = new DataSet();
                daReader.Fill(ds);

 

操作返回的DataSet

对DataSet里的7张表创建新列在赋值

DataSet setDs=null;

 if (ds.Tables[0].Rows.Count > 0)
                {
                    setDs.Tables.Add(ds.Tables[0].Copy());

                    DataColumn taColumn = new DataColumn("IMGTATUS", System.Type.GetType("System.String"));
                    setDs.Tables[0].Columns.Add(taColumn);
                    setDs.Tables[0].Rows[0]["IMGTATUS"] = "新列";
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        DataColumn taColumn1 = new DataColumn("IMGTATUS", System.Type.GetType("System.String"));
                        taColumn1.DefaultValue = "新列";
                        ds.Tables[1].Columns.Add(taColumn1);
                        setDs.Tables[0].ImportRow(ds.Tables[1].Rows[0]);
                    }
                    if (ds.Tables[2].Rows.Count > 0)
                    {
                        DataColumn taColumn2 = new DataColumn("IMGTATUS", System.Type.GetType("System.String"));
                        //taColumn2.DefaultValue = "新列";
                        ds.Tables[2].Columns.Add(taColumn2);
                        for (int i = 0, length = ds.Tables[2].Rows.Count; i < length; i++)
                        {
                           
                            if (i > 0)
                            {
                                ds.Tables[2].Rows[i]["IMGTATUS"] = "值1";
                            }
                            else
                            {
                                ds.Tables[2].Rows[i]["IMGTATUS"] = "值2";
                            }
                            setDs.Tables[0].ImportRow(ds.Tables[2].Rows[i]);
                        }
                    }

                    ..........等等

        return setDs;

}

得到DataSet后在排序,绑定在GridView上

                    DataView view = ds.Tables[0].DefaultView;
                        view.Sort = "CREATE_TIME ASC";//根据列排序
                        gridView.DataSource = view;
                        gridView.DataBind();

Mybatis调用Oracle存储过程返回多个游标的步骤如下: 1. 定义存储过程Oracle数据库中定义存储过程,该存储过程需要返回多个游标,例如: ``` CREATE OR REPLACE PROCEDURE get_multi_cursor( p_id IN NUMBER, p_cursor1 OUT SYS_REFCURSOR, p_cursor2 OUT SYS_REFCURSOR ) AS BEGIN OPEN p_cursor1 FOR SELECT * FROM table1 WHERE id = p_id; OPEN p_cursor2 FOR SELECT * FROM table2 WHERE id = p_id; END; ``` 2. 创建Java类 创建一个Java类,用于封装存储过程的参数和返回结果,例如: ```java public class MultiCursorResult { private List<Table1> table1List; private List<Table2> table2List; // getter and setter } ``` 3. 定义Mapper接口 在Mybatis的Mapper接口中定义调用存储过程的方法,例如: ```java public interface MultiCursorMapper { void getMultiCursor( @Param("id") Integer id, @Param("cursor1") ResultSet[] cursor1, @Param("cursor2") ResultSet[] cursor2 ); } ``` 4. 编写Mapper XML 在Mapper XML中编写调用存储过程的SQL语句,例如: ```xml <select id="getMultiCursor" statementType="CALLABLE"> {call get_multi_cursor(#{id},#{cursor1,mode=OUT,jdbcType=CURSOR,javaType=ResultSet},#{cursor2,mode=OUT,jdbcType=CURSOR,javaType=ResultSet})} </select> ``` 5. 调用Mapper方法 在Java程序中调用Mapper方法,例如: ```java MultiCursorMapper mapper = sqlSession.getMapper(MultiCursorMapper.class); ResultSet[] cursor1 = new ResultSet[1]; ResultSet[] cursor2 = new ResultSet[1]; mapper.getMultiCursor(1, cursor1, cursor2); List<Table1> table1List = new ArrayList<>(); while (cursor1[0].next()) { Table1 table1 = new Table1(); table1.setId(cursor1[0].getInt("id")); table1.setName(cursor1[0].getString("name")); table1List.add(table1); } List<Table2> table2List = new ArrayList<>(); while (cursor2[0].next()) { Table2 table2 = new Table2(); table2.setId(cursor2[0].getInt("id")); table2.setName(cursor2[0].getString("name")); table2List.add(table2); } MultiCursorResult result = new MultiCursorResult(); result.setTable1List(table1List); result.setTable2List(table2List); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值