Code for calling a function in a package from C# and ODP.NET

后端 未结 3 1458
情歌与酒
情歌与酒 2020-12-05 08:44

I\'ve tried to write C# code with ODP.NET to call a function in a package. I\'m getting the two errors below:

ORA-06550: line 1, column 7:
PLS-00306: wrong n         


        
3条回答
  •  渐次进展
    2020-12-05 09:14

    This is my first question on this forum and I am happy to post to my own answer.

    We can call an oracle package function using ODP.NET by setting CommandType.StoredProcedure.

    ORA-06550: line 1, column 7:
    PLS-00221: 'INSERT_FUNC' is not a procedure or is undefined
    ORA-06550: line 1, column 7: PL/SQL: Statement ignored
    

    If you get this error, just add this line as the first parameter on the command object:

    cmd.Parameters.Add("Return_Value", OracleDbType.Int16,
        ParameterDirection.ReturnValue);
    

    Here is the working code:

    using (var conn = new OracleConnection(oradb))
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "PKG_NAME.INSERT_FUNC";
    
        cmd.BindByName = true;
    
        cmd.Parameters.Add("Return_Value", OracleDbType.Int16,
            ParameterDirection.ReturnValue);
        cmd.Parameters.Add("i_description", OracleDbType.Varchar2, 1000,
            promotionEventSetupDetails.PromotionDescription,
            ParameterDirection.Input);
        cmd.Parameters.Add("i_theme", OracleDbType.Varchar2, 80,
            promotionEventSetupDetails.PromotionTheme,
            ParameterDirection.Input);
        cmd.Parameters.Add("o_id", OracleDbType.Varchar2,
            ParameterDirection.Output);
        cmd.Parameters.Add("o_error_msg", OracleDbType.Varchar2,
            ParameterDirection.Output);
    
        conn.Open();
        using (var dr = cmd.ExecuteReader())
        {
            // do some work here
        }
    }
    

提交回复
热议问题