PL/SQL 如何获取SP的OUT参数

PL/SQL How to get SP's OUT parameter

我有一个 DAL 函数需要调用存储过程并获取数据集以及几个 OUT 参数。我可以获得生成的数据集,但不确定如何获取参数。

通常,参数可通过 cmd.Parameters["ParamName"].Value 获得,但我使用另一个 API 建立数据库连接并 return 生成数据集, 不确定如何获取除数据集之外的 OUT 参数。

这是我目前所拥有的:

    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();
                    }
                }
            }
        }

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;

由于中间有一层,您可能需要稍微更改一下代码。

改变这个

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

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

然后使用Value 属性:

outParam1.Value; 

根据 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.