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

时间:2011-04-28 05:25:26

标签: c# transactions plsql odp.net

我最近移植了一些代码来使用ODP.NET的OracleTransaction

我有以下PL / SQL代码:

表示父表

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;

父表触发器

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;

子表触发器

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;

假设我为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;
            } 

我的问题是“如何通过OracleCommand将insertneworder函数返回的PARENT表的id传递给insertneworderdetails”?

任何帮助都得到了很多赞赏......

1 个答案:

答案 0 :(得分:0)

看起来你需要在ExecuteTransaction函数的'foreach'循环中添加一些代码来检测何时调用'insertneworder'函数,捕获它的返回值,然后检测'insertneworderdetails'函数是什么时候即将被调用并将先前捕获的返回值绑定到“insertneworderdetails”调用中的相应参数标记。对不起,我不能更具体,但我认为这是一个普遍的前进方式。

分享并享受。