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

发布于 2024-11-03 15:08:09 字数 3044 浏览 0 评论 0原文

我最近移植了一些代码以使用 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;

对于子表

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”?

非常感谢任何帮助...

I recently ported some of my codes to use OracleTransaction of ODP.NET

I have the Following PL/SQL Codes:

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...

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"?

ANY HELP IS MUCH APPRECIATED...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-11-10 15:08:09

看起来您需要在 ExecuteTransaction 函数的“foreach”循环中添加一些代码,以检测何时调用“insertneworder”函数,捕获其返回值,然后检测何时调用“insertneworderdetails”函数调用并将先前捕获的返回值绑定到“insertneworderdetails”调用中适当的参数标记。抱歉,我不能说得更具体,但我认为这是一个总体的前进方向。

分享并享受。

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. Sorry I can't be more specific, but I think that's a general way forward.

Share and enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文