Search code examples
c#plsqloracle12c.net-4.6

PL/SQL How to get SP's OUT parameter


I have a DAL function that needs to call a stored procedure and get the dataset, along with a couple of OUT parameters. I can get the resulting dataset but not sure how to get the parameters.

Normally, the parameters are available through cmd.Parameters["ParamName"].Value but I am using another API to make the DB connection and return the resulting dataset, not sure how to get the OUT parameters in addition to dataset.

Here is what i have, so far:

    Public static int getSomething(string inParam, out DataTable dtOut, out string outParam1, out string outParam2)
    {
        OracleDbCntext dbContext = new OracleDbContext();
        DataSet dsOut = new DataSet()
        DataTable dtOut = new dataTable();
        ....
        try
        {
            List<OracleParameter> spParams = new List<OracleParameter>();
            spParams.Add(new OracleParameter("INPARAM", OracleDbType.Varchar2, receptacleID, ParameterDirection.Input));
            spParams.Add(new OracleParameter("OUTARAM1", OracleDbType.TimeStamp, null, ParameterDirection.Output));
            spParams.Add(new OracleParameter("OUTARAM2", OracleDbType.TimeStamp, null, ParameterDirection.Output));
            spParams.Add(new OracleParameter("CUR_OUT", OracleDbType.RefCursor, ParameterDirection.Output));

            try
            {
                dbContext.Open();
                dbContext.ExecuteStoredProcedure("SOME_PKG.USP_SOMESP", spParams, ref dsOutcome);
            }
            catch (Exception oConnException)
            {
            }
            if (dsOut != null)
            {
                if (dsOut.Tables[0].Rows.Count > 0)
                {
                    dtOut = dsOut.Tables[0];
                    //outParam1 = ????
                    //outParam2 = ????
                }
            }
        }
    }

namespace Something.Model.DataAccess
{
    public class OracleDbContext
    {
        public OracleConnection DbConnection { get; private set; }

        public OracleDbContext()
        {
            string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;

            DbConnection = new OracleConnection(ConnectionString);
        }
        public void Open()
        {
            if (DbConnection.State != ConnectionState.Open)
            {
                DbConnection.Close();
                DbConnection.Open();
            }
        }
        public void Close()
        {
            if (DbConnection.State == ConnectionState.Open || DbConnection.State == ConnectionState.Broken)
            {
                DbConnection.Close();
                DbConnection.Dispose();
            }
        }
        public void ExecuteStoredProcedure(string spName, List<OracleParameter> spParams, ref DataSet dataset)
        {
            OracleDataAdapter da = null;
            OracleTransaction oraTransaction = null;
            using (OracleCommand command = new OracleCommand())
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddRange(spParams.ToArray<OracleParameter>());
                command.Connection = DbConnection;
                command.CommandText = spName;

                try
                {
                    oraTransaction = DbConnection.BeginTransaction();
                    da = new OracleDataAdapter(command);
                    da.Fill(dataset);
                    oraTransaction.Commit();
                }
                catch (Exception e)
                {
                    oraTransaction.Rollback();
                }
                finally
                {
                    if (oraTransaction != null)
                        oraTransaction.Dispose();

                    if (DbConnection != null)
                    {
                        this.Close();
                    }
                }
            }
        }

Stored Procedure in SOME_PKG:

    PROCEDURE USP_SOMESP
    (
      INPARAM       VARCHAR2,
      OUTPARAM1     OUT TIMESTAMP,
      OUTPARAM2     OUT TIMESTAMP,
      CUR_OUT       OUT GETDATACURSOR 
    )
    ....
      LVSQUERY:='SELECT FIELD1, '''|| V_EVENTCODE ||''' AS EVENTCODE, ...
 WHERE SOMETHING= '''|| V_LOC1||''' ';


      OPEN CUR_OUT FOR LVSQUERY;
      EXCEPTION WHEN OTHERS THEN
        ...;
    END USP_SOMESP;

Solution

  • As you have a layer in the middle you might need to change your code a little bit.

    Change this

    spParams.Add(new OracleParameter("OUTARAM1", OracleDbType.TimeStamp, null, ParameterDirection.Output));
    

    with

    var outParam1 = new OracleParameter("OUTARAM1", OracleDbType.TimeStamp, null, ParameterDirection.Output);
    spParams.Add(outParam1); 
    

    And then use the Value property:

    outParam1.Value; 
    

    According to the documentation:

    For output parameters the value is:
    Set on completion of the OracleCommand (true for return value parameters also).
    Set to the data from the database, to the data type specified in OracleDbType or DbType.