【问题标题】:How to use Oracle Complex Type out parameter in .NET如何在 .NET 中使用 Oracle 复杂类型输出参数
【发布时间】:2014-08-13 15:38:17
【问题描述】:

我有一个 Oracle 过程,它将复杂类型作为输出参数传递,我需要能够在 .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 复杂类型?

感谢您的帮助

【问题讨论】:

    标签: asp.net database oracle


    【解决方案1】:

    为什么不简单地返回一个 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);
    

    【讨论】:

    • 有趣的是,你提到了这一点,因为我也有同样的想法,并且确实按照你的建议让它工作。但是,我们的数据库认为它加倍了时间,所以我只是想看看它是否可能,如果可能,怎么做?这是你平时的做法吗?
    • 是的,在我的应用程序中,each 数据作为 RefCursor 提供(只要它不是单个值)。有些被传递给 OracleDataAdapter,有些被传递给 OracleDataReader,这取决于数据在客户端代码中的使用位置。
    猜你喜欢
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2016-07-21
    • 1970-01-01
    相关资源
    最近更新 更多