Get value of Oracle OUT parameter from a stored procedure call using Dapper.NET

拜拜、爱过 提交于 2019-12-01 03:35:24

I know this is extremely late and may be common knowledge to most everyone but me, but there is a comment in the original message from not too long ago, so I'll describe how I got around the issue of having a cursor out parameter as well as non-cursor out parameters.

Since my example only has one Oracle cursor, I can user the Query method. The results of the other out parameters are in the parameters themselves and can be retrieved with the Get<> method in the OracleDynamicParameters class.

The other important part for me was to add a size to my out parameters, otherwise they were coming back as null or empty strings.

Below is a sample of the code I'm using.

using (IDbConnection db = new OracleConnection(connectionString)) {
    var p = new OracleDynamicParameters();
    p.Add("p_first_parameter", someParameter, OracleDbType.Varchar2, ParameterDirection.Input);
    p.Add("o_cursr", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output);
    p.Add("o_sqlerrm", dbType: OracleDbType.Varchar2, direction: ParameterDirection.Output, size: 200);
    p.Add("o_sqlcode", dbType: OracleDbType.Varchar2, direction: ParameterDirection.Output, size: 200);

    dynamic csr = db.Query("myStoredProcedure", p, commandType: CommandType.StoredProcedure).ToList().First();
    string code = p.Get<OracleString>("o_sqlcode").ToString();
    if (code != "0") {
        string errm = p.Get<OracleString>("o_sqlerrm").ToString();
        throw new Exception($"{code} - {errm}");
    }
}

Close the connection, then read the output parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!