简体   繁体   English

如何通过 OracleCommand 使用 OracleTransaction 将父表的 id 传递给子表

[英]How to pass id of Parent Table to Child using OracleTransaction through OracleCommand

I recently ported some of my codes to use OracleTransaction of ODP.NET我最近移植了一些代码以使用 ODP.NET 的 OracleTransaction

I have the Following PL/SQL Codes:我有以下 PL/SQL 代码:

for Parent table对于父表

CREATE OR REPLACE FUNCTION insertneworder (
   orderdate        IN   orders.order_date%TYPE,
   orderdescription IN   orders.order_description%TYPE
)
   RETURN orders.order_id%TYPE
IS
  orderid   orders.order_id%TYPE;

BEGIN
   INSERT INTO orders
               (order_date, order_description
               )
        VALUES (orderdate,orderdescription
               )
     RETURNING order_id
          INTO orderid;

   RETURN orderid;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      NULL;
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END insertneworder;

Parent Table Trigger父表触发器

CREATE OR REPLACE TRIGGER bi_orders_pk
   BEFORE INSERT
   ON orders
   REFERENCING NEW AS NEW OLD AS OLD
   FOR EACH ROW
BEGIN
   SELECT TO_CHAR (SYSTIMESTAMP, 'yyyymmddhh24missff3')
     INTO :NEW.order_id
     FROM DUAL;
END;

for Child Table子表

CREATE OR REPLACE PROCEDURE insertneworderdetails (
   orderid     IN   order_details.order_id%TYPE,
   quantity    IN   order_details.quantity%TYPE,
   productid   IN   order_details.prod_id%TYPE,
   itemcost    IN   order_details.itemcost%TYPE
)
IS
BEGIN
   INSERT INTO order_details
               (order_id, quantity, prod_id, itemcost
               )
        VALUES (orderid, quantity, productid, itemcost
               );
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      NULL;
   WHEN OTHERS
   THEN
      -- Consider logging the error and then re-raise
      RAISE;
END insertneworderdetails;

Child Table Trigger子表触发器

CREATE OR REPLACE TRIGGER bi_order_details_pk
   BEFORE INSERT
   ON order_details
   REFERENCING NEW AS NEW OLD AS OLD
   FOR EACH ROW
BEGIN
   SELECT TO_CHAR (SYSTIMESTAMP, 'yyyymmddhh24missff3')
     INTO :NEW.item_id
     FROM DUAL;
END;

Assuming that I created an OracleCommand for insertneworder and insertneworderdetails I will pass it to this code...假设我为 insertneworder 和 insertneworderdetails 创建了一个 OracleCommand,我将把它传递给这段代码......

public static bool ExecuteTransaction(List<OracleCommand> command) 
            {
                OracleConnection conn;
                conn = OraConnection.Instance.OracleConnection;
                if (conn.State == ConnectionState.Closed) {
                    conn.Open();
                    }
                OracleTransaction trans;
                trans = conn.BeginTransaction();

                try 
                    {
                    foreach (OracleCommand cmd in command) 
                        {
                        cmd.Connection = conn;
                        cmd.ExecuteNonQuery();
                        }
                    trans.Commit(); 
                    }

                catch (Exception ex)
                    {
                    trans.Rollback();
                    return false;
                    }
            return true;
            } 

MY QUESTION IS "How can I pass the id of PARENT table returned by insertneworder function to insertneworderdetails through OracleCommand"?我的问题是“如何将 insertneworder function 返回的 PARENT 表的 ID 通过 OracleCommand 传递给 insertneworderdetails”?

ANY HELP IS MUCH APPRECIATED...任何帮助深表感谢...

It looks like you'd need to add some code in the 'foreach' loop in your ExecuteTransaction function to detect when the 'insertneworder' function has been called, capture its return value, then detect when the 'insertneworderdetails' function is about to be called and bind the previously-captured return value to the appropriate parameter marker in the 'insertneworderdetails' call. It looks like you'd need to add some code in the 'foreach' loop in your ExecuteTransaction function to detect when the 'insertneworder' function has been called, capture its return value, then detect when the 'insertneworderdetails' function is about to be调用并将先前捕获的返回值绑定到“insertneworderdetails”调用中的适当参数标记。 Sorry I can't be more specific, but I think that's a general way forward.抱歉,我不能更具体,但我认为这是一个普遍的前进方向。

Share and enjoy.分享和享受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM