A stored procedure could be stand alone or be a part of a package. It all depends on how the developers chose to implement it in the database. In either case the methods to call the stored procedure remains the same.
1) you establish a connection to the database.
2) make sure your account (the login that you use) has privileges to execute the stored procedure.
3) create a ODP .NEt command object
4) create parameters for the command object based on the parameters the stored proc takes.
5) execute the stored procedure
6) retrieve results.
Here's a sample code that works.
This code uses an insert query with parameters to insert a record in an oracle table.
c# Code:
public static void InsertProspect(OracleConnection oracleConnection, int partyId, string accountId)
{
//ReadCommitted is the default in oracle.
var transaction = oracleConnection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
const string sqlStr = "INSERT INTO party_Table \n" +
"(partyId, account_Id) \n" +
"VALUES(:partyId, :accountId)";
var oracleCommand = new OracleCommand
{
Connection = oracleConnection,
CommandText = sqlStr,
CommandType = CommandType.Text
};
var prmcontractId = new OracleParameter
{
ParameterName = "partyId",
Direction = ParameterDirection.Input,
OracleDbType = OracleDbType.Int32,
Size = 8,
Value = partyId
};
oracleCommand.Parameters.Add(prmcontractId);
var prmaccountId = new OracleParameter
{
ParameterName = "account_Id",
Direction = ParameterDirection.Input,
OracleDbType = OracleDbType.Varchar2,
Size = 100,
Value = accountId
};
oracleCommand.Parameters.Add(prmaccountId);
var retVal = oracleCommand.ExecuteNonQuery();
transaction.Commit();
}catch(Exception exception)
{
transaction.Rollback();
Console.WriteLine(string.Format("InsertParty:\t{0}",exception.Message));
throw;
}
}
This stored procedure doesn't have any input parameters and it returns a recordset from the database.
c# Code:
private static OracleDataReader FireMyFunc(OracleConnection oc)
{
try
{
var cmd = new OracleCommand
{
Connection = oc,
CommandText = "sendmearefcursor",
CommandType = CommandType.StoredProcedure
};
var prm = cmd.CreateParameter();
prm.OracleDbType = OracleDbType.RefCursor;
prm.ParameterName = "returncurse";
prm.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(prm);
cmd.ExecuteNonQuery();
var rc = (OracleRefCursor) prm.Value;
return rc.GetDataReader();
}
catch (Exception ex)
{
Console.WriteLine(String.Format("FireMyFunc: {0}",ex));
throw;
}
}