Code for calling a function in a package from C# and ODP.NET
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
}
}
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.