如何在.NET中使用Oracle Complex Type out参数

时间:2014-08-13 15:38:17

标签: asp.net database oracle

我有一个Oracle过程,它将复杂类型作为out参数传递,我需要能够在.NET中使用。我没有太多运气找到一个如何做这样的工作的好例子所以我希望这里的一些人可能有答案或已经做到了这一点。

Oracle.manageddataAccess.dll版本#4.121.1.0

代码:

    CREATE OR REPLACE TYPE APPS.gary_pol_tbl_type AS TABLE OF gary_pol_rec_type;


    CREATE OR REPLACE PROCEDURE APPS.gary_pol_test3 ( x_gary_pol IN OUT apps.gary_pol_tbl_type ) AS
   CURSOR c1 IS
        SELECT org_id,
               po_type,
               po_header_id,
               po_num,
               po_release_id,
               po_release_num,
               vendor_id,
               vendor_name,
               vendor_site_num,
               vendor_site_name,
               pay_site_id,
               currency_code,
               po_amount,
               po_date,
               buyer_name,
               shipment_num,
               po_line_id,
               line_num,
               po_creation_date,
               release_date,
               item_num,
               item_description,
               uom,
               po_qty,
               received_qty,
               unit_price,
               taxable_flag,
               closed_date,
               entity,
               branch,
               planner_name,
               attribute1,
               attribute2,
               batch_number,
               processed_flag,
               wm_uuid,
               creation_date,
               created_by,
               last_update_date,
               last_updated_by
          FROM xxit.xxfin_ap_imagenow_pol_out
         WHERE ROWNUM < 2001
      ORDER BY org_id ASC NULLS LAST,
               po_header_id ASC NULLS LAST,
               po_release_id ASC NULLS LAST,
               shipment_num ASC NULLS LAST,
               po_line_id ASC NULLS LAST,
               line_num ASC NULLS LAST;
BEGIN
   FOR c1_rec IN c1 LOOP
      x_gary_pol.EXTEND;
      x_gary_pol ( x_gary_pol.LAST ) :=
         apps.gary_pol_rec_type ( c1_rec.org_id,
                                  c1_rec.po_type,
                                  c1_rec.po_header_id,
                                  c1_rec.po_num,
                                  c1_rec.po_release_id,
                                  c1_rec.po_release_num,
                                  c1_rec.vendor_id,
                                  c1_rec.vendor_name,
                                  c1_rec.vendor_site_num,
                                  c1_rec.vendor_site_name,
                                  c1_rec.pay_site_id,
                                  c1_rec.currency_code,
                                  c1_rec.po_amount,
                                  c1_rec.po_date,
                                  c1_rec.buyer_name,
                                  c1_rec.shipment_num,
                                  c1_rec.po_line_id,
                                  c1_rec.line_num,
                                  c1_rec.po_creation_date,
                                  c1_rec.release_date,
                                  c1_rec.item_num,
                                  c1_rec.item_description,
                                  c1_rec.uom,
                                  c1_rec.po_qty,
                                  c1_rec.received_qty,
                                  c1_rec.unit_price,
                                  c1_rec.taxable_flag,
                                  c1_rec.closed_date,
                                  c1_rec.entity,
                                  c1_rec.branch,
                                  c1_rec.planner_name,
                                  c1_rec.attribute1,
                                  c1_rec.attribute2,
                                  c1_rec.batch_number,
                                  c1_rec.processed_flag,
                                  c1_rec.wm_uuid,
                                  c1_rec.creation_date,
                                  c1_rec.created_by,
                                  c1_rec.last_update_date,
                                  c1_rec.last_updated_by );
   END LOOP;
 apps.gary_pol_tbl_type ) );
END gary_pol_test3;

通过将oracleDBType ....设置为对象或数组,我在搜索中看到了一些参考,但我没有这些选项。我可以看到对象可能在哪里找我想要的东西。

这是我在c#中尝试阅读此内容的代码。

public static DataTable test2()
        {
            DataTable dt = new DataTable();

            try
            {
                using (OracleConnection cn = BSIC_DAL.OracleHelpers.createOracleConnection())
                {
                    OracleDataAdapter da = new OracleDataAdapter();
                    OracleCommand cmd = new OracleCommand();
                    cmd.Connection = cn;

                    cmd.CommandText = "apps.gary_pol_test3";
                    cmd.CommandType = CommandType.StoredProcedure;

                    //cmd.Parameters.Add("p_batch_number", OracleDbType.Int16).Value = 133;
                    cmd.Parameters.Add("x_gary_pol", OracleDbType.RefCursor).Direction = ParameterDirection.Output;

                    da.SelectCommand = cmd;

                    da.Fill(dt);
                    return dt;
                }
            }
            catch (Exception ex)
            {
                return null;
                throw ex;
            }
        }

如何在.Net中使用如上所述的oracle复杂类型?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

为什么不简单地退回RefCursor?

应该是这样的:

CREATE OR REPLACE PROCEDURE APPS.gary_pol_test3 ( x_gary_pol OUT SYS_REFCORSOR) AS

begin
   open x_gary_pol for
   SELECT org_id, po_type, po_header_id, ...
   FROM xxit.xxfin_ap_imagenow_pol_out
   WHERE ROWNUM < 2001
   ORDER BY org_id ASC NULLS LAST, ...
end;

在C#中应该是这样的(未经验证):

cmd.Parameters.Add("x_gary_pol", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
da.SelectCommand = cmd;
cmd.ExecuteNonQuery();
da.Fill(dt);