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

后端 未结 3 1447
情歌与酒
情歌与酒 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
        }
    }
    
    0 讨论(0)
  • 2020-12-05 09:17

    This must be new with a more recent version of Oracle. I was previously able to do this with the return value parameter listed after all of the input parameters in my C# code, but after running this on 12c I had this exact issue, which now works with this suggestion of putting the return val param first.

    0 讨论(0)
  • 2020-12-05 09:19

    I have created a generic method that helps you get your function result

    var result = await 
     dbManager.ExecuteFunctionResultAsync<Oracle.ManagedDataAccess.Types.OracleDecimal>(
                         functionName, parameters.List);
    
    
    public async Task<T> ExecuteFunctionResultAsync<T>(string spName, IEnumerable<OracleParameter> paramaters)
            {
                using (OracleConnection connection = new OracleConnection(this.connectionString))
                {
                    connection.Open();
                    using (OracleCommand comm = new OracleCommand(spName, connection))
                    {
                        comm.CommandType = CommandType.StoredProcedure;
                        comm.BindByName = true;
                        string returnParam = "return_value";
                        comm.Parameters.Add(new OracleParameter() {
                            ParameterName = returnParam,
                            Direction = ParameterDirection.ReturnValue,
                            OracleDbType = OracleDbType.Int16,
                        });
    
                        this.SetCommandParameters(comm, paramaters);
                        await comm.ExecuteNonQueryAsync();
                        var result = (T)comm.Parameters[returnParam].Value;
    
                        return result;
                    }
                }
            }
    
    private void SetCommandParameters(OracleCommand command, IEnumerable<OracleParameter> paramaters)
            {
                if (paramaters != null)
                {
                    foreach (OracleParameter p in paramaters)
                    {
                        command.Parameters.Add(p);
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题