如何将参数从c传递到oracle函数

本文关键字:oracle 函数 参数 | 更新日期: 2023-09-27 17:59:22

我正在c#程序中执行以下函数

CREATE OR REPLACE FUNCTION F_INSERT_ORDER_DATA (  P_CUSTOMER_ID     IN NUMBER,
                                                  P_NOTES                  IN VARCHAR2,
                                                  P_CREATED_BY         IN VARCHAR2)
   RETURN NUMBER
IS
   vCREATED_ON   DATE := SYSDATE;
   vORDER_ID     NUMBER;
BEGIN
   INSERT INTO orders ( ORDER_ID,
                                        CUSTOMER_ID,
                                        NOTES,
                                        CREATED_BY,
                                        CREATED_ON)
                        VALUES ( NULL,                             --ORDER_ID Filled by trigger
                                        P_CUSTOMER_ID,         --CUSTOMER_ID
                                        P_NOTES,                    --NOTES
                                        P_CREATED_BY,             --CREATED_BY
                                        vCREATED_ON)              --CREATED_ON
RETURNING ORDER_ID INTO vORDER_ID;
   RETURN (vORDER_ID);
END;

但我遇到了一个错误,我无法将null插入CUSTOMER_ID列,因此错误是将参数从c传递到oracle函数我需要有人通过传递参数来帮助我?

    private void B_Insert_Click(object sender, EventArgs e)
    {
        if (PaidCash < TotalInv) 
        {
            MessageBox.Show("paid amount less than cost", "be careful", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            TB_PAID_CASH.BackColor = Color.Tomato;
            TB_PAID_CASH.Focus();
            return;
        }
        else if (Reminder < 0)
        {
            MessageBox.Show("zero value", "be careful", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            TB_Discount.BackColor = Color.Tomato;
            TB_Discount.Focus();
            return; 
        }
        else
        {
            Form_control("DataInserted");
            string connstr = @"Data Source=orcl; User Id=admin; password=123123;";
            string insertcmdtxt = @"F_INSERT_ORDER_DATA";   //~ F_INSERT_ORDER_DATA ~//
            using (OracleConnection conn = new OracleConnection(connstr))
            using (OracleCommand cmd = new OracleCommand(insertcmdtxt, conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = insertcmdtxt;
                    cmd.Parameters.Add(new OracleParameter("P_CUSTOMER_ID",TB_CUSTOMER_ID.Text ));
                    cmd.Parameters.Add(new OracleParameter("P_NOTES", null));
                    cmd.Parameters.Add(new OracleParameter("P_CREATED_BY", "System"));
                    cmd.Parameters.Add(":vORDER_ID", OracleDbType.Int64, ParameterDirection.ReturnValue);
                    cmd.ExecuteNonQuery();
                    TB_INVOICE_ID.Text = (cmd.Parameters[":vORDER_ID"].Value).ToString();
                }
                catch (Exception EX)
                {
                    MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
    }

如何将参数从c传递到oracle函数

使用此语法传递参数值

 int customerID = Convert.ToInt32(TB_CUSTOMER_ID.Text);
 cmd.Parameters.Add("P_CUSTOMER_ID",OracleDbType.Int32).Value = customerID;

考虑一下这样一个事实:您应该传递指定基础列所需类型的参数。如果CustomerID是整数,则不要传递字符串,而是适当地将值转换为整数,并使用正确的OracleDbType枚举。如果你不养成这个习惯,无论出于什么原因,数据库引擎都会使用不同的方法来转换值(特别有问题的是浮点值和日期)