如何将参数从c#传递给oracle函数

时间:2016-07-30 13:15:02

标签: c# oracle oracle11g

我在c#program

中执行下面的函数
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;

但是我收到的错误是我无法在CUSTOMER_ID列中插入null 错误是通过将参数从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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

使用此语法传递参数值

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

考虑一下这个事实,你应该传递指定基础列所期望的类型的参数。如果CustomerID是一个整数,那么不要传递字符串,但要适当地将值转换为整数并使用正确的OracleDbType枚举。如果你没有采取这种习惯,你会发现自己遇到麻烦,无论出于何种原因,数据库引擎使用不同的方法来转换值(特别有问题的是浮点值和日期)