Execute a pl/sql function with OracleCommand
The following code works for me.
NB: Your pl/sql code called the function KRIST.f_Login, but your c# called it krist.p_login
NB2: Your pl/sql code used Varchar2, but your c# used varchar
NB3: I am using Oracle.DataAccess.dll
NB4: I assume your return value buffer size could be 1, but try different sizes.
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
int RETURN_VALUE_BUFFER_SIZE = 32767;
OracleCommand cmd = new OracleCommand();
try {
cmd.Connection = conn;
cmd.CommandText = "KRIST.f_Login";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("returnVal", OracleDbType.Varchar2, RETURN_VALUE_BUFFER_SIZE);
cmd.Parameters["returnVal"].Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add("userName", OracleDbType.Varchar2);
cmd.Parameters["userName"].Value = "kristian";
cmd.Parameters.Add("password", OracleDbType.Varchar2);
cmd.Parameters["password"].Value = "kristian";
cmd.ExecuteNonQuery();
string bval = cmd.Parameters["returnVal"].Value.ToString();
return bval;
} catch (Exception e) {
// deal with exception
} finally {
command.Dispose();
connection.Close();
connection.Dispose();
}
As far as I remember If you are using ODP.NET you need to provide retVal parameter as first.
Something is wrong with ODP.NET and it dosn't bind parameters with provided parameter names but with order of parameters.
So simply change order to:
cmd.Parameters.Add(returnVal);
cmd.Parameters.Add(p_one);
cmd.Parameters.Add(p_two);
And in my sources I found that return parameter i called "RETURN" (not sure if it counts):
OracleParameter returnVal = new OracleParameter("RETURN",null);
A ha and one more thing. It will never reach last line - cuase return would terminate execute. Close it as soon as you don't need it anymore.
RETURN returnVal;
CLOSE USERFINDER; --<<-- won't close this cursor
ODP.net binds by order by default. This behavior can be modified with: cmd.BindByName = true