执行多个oracle函数会返回错误

时间:2017-02-17 15:28:59

标签: c# oracle oracle12c

你可以看到我试图用c#执行两个oracle函数 第一个函数F_INS_ORDER_DATA返回值,将作为参数使用第二个但是当第二个函数执行时F_INS_ORDER_DATA_DETAILS我得到的误差

我在蟾蜍中测试这两个功能,它们工作正常,所以

  1. 我错过了什么?
  2. 如果我的代码有任何改进或更好的方法,我会非常感谢任何建议
  3.   

    ora-06550:第1行,第7栏:

         

    PLS-00221:' F_INS_ORDER_DATA_DETAILS'不是程序或是   未定义

         

    ora-06550:第1行,第7栏:

         

    pl / sql:语句忽略

                string connstr = @"Data Source=orcl; User Id=user; password=pwd;";
                string cmdtxt01 = @"F_INS_ORDER_DATA";
                string cmdtxt02 = @"F_INS_ORDER_DATA_DETAILS";
                int INVOICE_ID;
    
                using (OracleConnection conn = new OracleConnection(connstr))
                using (OracleCommand cmd = new OracleCommand(cmdtxt01, conn))
                {
                    try
                    {
                        conn.Open();
    
                        cmd.CommandText = cmdtxt01;
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        cmd.Parameters.Clear();
    
                        cmd.Parameters.Add(":vORDER_ID", OracleDbType.Int32, ParameterDirection.ReturnValue);
    
                        cmd.Parameters.Add(new OracleParameter(":P_CUSTOMER_ID", OracleDbType.Int64)).Value = TB_CUSTOMER_ID.Text;
                        cmd.Parameters.Add(new OracleParameter(":P_ORDER_NOTE", OracleDbType.Varchar2)).Value = TB_ORDER_NOTE.Text;
                        cmd.Parameters.Add(new OracleParameter(":P_CREATED_BY", OracleDbType.Varchar2)).Value = "SYSTEM";
    
                        cmd.ExecuteNonQuery();
    
                        TB_INVOICE_ID.Text = cmd.Parameters[":vORDER_ID"].Value.ToString();
                        int.TryParse(TB_INVOICE_ID.Text, out INVOICE_ID);
    
                        cmd.CommandText = cmdtxt02;
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        foreach (DataGridViewRow Row in DGV_INVOICE.Rows)
                        {
                            cmd.Parameters.Clear();
    
                            cmd.Parameters.Add(new OracleParameter(":P_ORDER_ID", OracleDbType.Int32)).Value = INVOICE_ID;
                            cmd.Parameters.Add(new OracleParameter(":P_PRODUCT_ID", OracleDbType.Int64)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_PRODUCT_ID"].Index].Value;
                            cmd.Parameters.Add(new OracleParameter(":P_UNIT_PRICE", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_UNIT_PRICE"].Index].Value;
                            cmd.Parameters.Add(new OracleParameter(":P_QUANTITY", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_QUANTITY"].Index].Value;
                            cmd.Parameters.Add(new OracleParameter(":P_DISCOUNT", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_DISCOUNT"].Index].Value;
                            cmd.Parameters.Add(new OracleParameter(":P_ORDER_STATUS", OracleDbType.Varchar2)).Value = '1';
                            cmd.Parameters.Add(new OracleParameter(":P_ITEM_NOTE", OracleDbType.Varchar2)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_ITEM_NOTE"].Index].Value;
                            cmd.Parameters.Add(new OracleParameter(":P_CREATED_BY", OracleDbType.Varchar2)).Value = "SYSTEM";
    
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception EX)
                    {
                        MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
    

2 个答案:

答案 0 :(得分:2)

Oracle区分过程和函数。

程序存储的代码不返回值。函数是存储返回值的代码。你使用它们的方式(在Oracle中)有点不同。

由于数据库抱怨MyCollection::whereRaw('(updated_at - created_at) < ?', 300)->get(); 不是一个程序,它可能是一个函数(带有返回值)但是你没有像第一次调用那样捕获它:

F_INS_ORDER_DATA_DETAILS

因此,请确保您使用的是存储过程:

cmd.Parameters.Add(":vORDER_ID", OracleDbType.Int32, ParameterDirection.ReturnValue);

或从函数中捕获返回值。

答案 1 :(得分:0)

using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt01, conn))
{
    try
    {
        conn.Open();

        cmd.CommandText = cmdtxt01;
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(":vORDER_ID", OracleDbType.Int32, ParameterDirection.ReturnValue);

        cmd.Parameters.Add(new OracleParameter(":P_CUSTOMER_ID", OracleDbType.Int32)).Value = TB_CUSTOMER_ID.Text;
        cmd.Parameters.Add(new OracleParameter(":P_ORDER_NOTE", OracleDbType.Varchar2)).Value = TB_ORDER_NOTE.Text;
        cmd.Parameters.Add(new OracleParameter(":P_CREATED_BY", OracleDbType.Varchar2)).Value = "SYSTEM";
        //excute first SP
        cmd.ExecuteNonQuery();
        //get the value
        TB_INVOICE_ID.Text = cmd.Parameters[":vORDER_ID"].Value.ToString();
        int INVOICE_ID;
        // parse return value
        int.TryParse(TB_INVOICE_ID.Text, out INVOICE_ID);

        // now set 2nd SP 
        cmd.CommandText = cmdtxt02;
        cmd.CommandType = CommandType.StoredProcedure;

        foreach (DataGridViewRow Row in DGV_INVOICE.Rows)
        {
            cmd.Parameters.Clear();
            cmd.Parameters.Add(new OracleParameter(":P_ORDER_ID", OracleDbType.Int32)).Value = INVOICE_ID;
            cmd.Parameters.Add(new OracleParameter(":P_PRODUCT_ID", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_PRODUCT_ID"].Index].Value;
            cmd.Parameters.Add(new OracleParameter(":P_UNIT_PRICE", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_UNIT_PRICE"].Index].Value;
            cmd.Parameters.Add(new OracleParameter(":P_QUANTITY", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_QUANTITY"].Index].Value;
            cmd.Parameters.Add(new OracleParameter(":P_DISCOUNT", OracleDbType.Int32)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_DISCOUNT"].Index].Value;
            cmd.Parameters.Add(new OracleParameter(":P_ORDER_STATUS", OracleDbType.Varchar2)).Value = '1';
            cmd.Parameters.Add(new OracleParameter(":P_ITEM_NOTE", OracleDbType.Varchar2)).Value = Row.Cells[DGV_INVOICE.Columns["DGV_ITEM_NOTE"].Index].Value;
            cmd.Parameters.Add(new OracleParameter(":P_CREATED_BY", OracleDbType.Varchar2)).Value = "SYSTEM";

            cmd.ExecuteNonQuery();
        }


    }
    catch (Exception EX)
    {
        MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
}