添加类:
public class RefCursor { public static RefCursor Value = new RefCursor(); private RefCursor() { } } public class RefCursorHandler : SqlMapper.ITypeHandler { public void SetValue(IDbDataParameter parameter, object value) { OracleParameter oracleParameter = (OracleParameter)parameter; oracleParameter.OracleDbType = OracleDbType.RefCursor; oracleParameter.Direction = ParameterDirection.Output; parameter.Value = value; } public object Parse(Type destinationType, object value) { throw new NotImplementedException(); } }
使用之前:
SqlMapper.AddTypeHandler(typeof(RefCursor), new RefCursorHandler());
因为上面的代码只需要执行一次即可,建议在静态构造函数内运行
使用:
public List<SampleListDto> GetSampleListByDapper(IPagedResultRequest paged, ref int total) { var stored = "p_CReportRecognition"; var parameter = new DynamicParameters(); parameter.Add("p_pageSize", paged.PageSize); parameter.Add("p_pageIndex", paged.PageIndex); parameter.Add("p_RowsCount", total, DbType.Int32, ParameterDirection.Output); parameter.Add("cur_SysList", RefCursor.Value);//cur_SysList为游标名称 using (IDbConnection _db = new OracleConnection(_connString)) { var list = _db.Query<SampleListDto>(stored, parameter, null, false, null, CommandType.StoredProcedure).ToList(); return list; } }