Using Dapper QueryMultiple in Oracle

前端 未结 3 1274
無奈伤痛
無奈伤痛 2021-02-01 22:23

I´m trying to use dapper with Oracle (ODP.NET) and I would like to use the \"QueryMultiple\" functionality.

Passing this string to the QueryMultiple method:



        
3条回答
  •  庸人自扰
    2021-02-01 22:50

    Building on greyseal96's helpful answer, I created this implementation of IDynamicParameters:

    public class OracleDynamicParameters : SqlMapper.IDynamicParameters
    {
        private readonly DynamicParameters dynamicParameters;
    
        private readonly List oracleParameters = new List();
    
        public OracleDynamicParameters(params string[] refCursorNames) {
            dynamicParameters = new DynamicParameters();
            AddRefCursorParameters(refCursorNames);
        }
    
        public OracleDynamicParameters(object template, params string[] refCursorNames) {
            dynamicParameters = new DynamicParameters(template);
            AddRefCursorParameters(refCursorNames);
        }
    
        private void AddRefCursorParameters(params string[] refCursorNames)
        {
            foreach (string refCursorName in refCursorNames)
            {
                var oracleParameter = new OracleParameter(refCursorName, OracleDbType.RefCursor, ParameterDirection.Output);
                oracleParameters.Add(oracleParameter);
            }
        }
    
        public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
        {
            ((SqlMapper.IDynamicParameters)dynamicParameters).AddParameters(command, identity);
            var oracleCommand = command as OracleCommand;
            if (oracleCommand != null)
            {
                oracleCommand.Parameters.AddRange(oracleParameters.ToArray());
            }
        }
    }
    

    Assuming the same query, it can be used so:

    var queryParams = new { id };
    string[] refCursorNames = { "rslt1", "rslt2", "rslt3" };
    var dynParams = new OracleDynamicParameters(queryParams, refCursorNames);
    ...
    var multi = dbConn.QueryMultiple(sql, param: dynParams);
    

提交回复
热议问题