/// <summary> /// Executes the rowmapper and get multiple output parameter results. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="commandText">Store procedure name. E.g. "Posts_GetPostsByUser</param> /// <param name="commandType">StoredProcedure</param> /// <param name="rowMapper">The mapper that maps a record to an object.</param> /// <param name="dbParameters">Array of parameters for the query</param> /// <param name="outputParamNames">Array of output parameter names.</param> /// <returns></returns> public static Tuple2 <IList <T>, IDictionary <string, object> > Query <T>(this IDBHelper dbHelper, string commandText, CommandType commandType, DbParameter[] dbParameters, IRowMapper <IDataReader, T> rowMapper, string[] outputParamNames) { DbCommand command = dbHelper.BuildCommand(commandText, CommandType.StoredProcedure, dbParameters); IList <T> items = null; IDictionary <string, object> paramResults = new Dictionary <string, object>(); using (command.Connection) { command.Connection.Open(); IDataReader reader = command.ExecuteReader(); // Map the workshops. items = rowMapper.MapRows(reader); // NOTE: Must close reader to get output params. reader.Close(); // Now get all the output parameters. if (outputParamNames != null && outputParamNames.Length > 0) { foreach (string outputParamName in outputParamNames) { object paramResult = command.Parameters[outputParamName].Value; paramResults[outputParamName] = paramResult; } } command.Dispose(); } return(new Tuple2 <IList <T>, IDictionary <string, object> >(items, paramResults)); }
public static BoolResult <IList <TEntity> > SPToList <TEntity>(this DataBase db, string spName, IRowMapper <IDataReader, TEntity> rowMapper, params IDataParameter[] parameters) where TEntity : class, new() { Guard.IsNotNull(rowMapper, "IRowMapper is null"); ValidationResults validationResults = new ValidationResults(); IList <TEntity> item = new List <TEntity>(); IDataReader dataReader = null; try { dataReader = db.ExecuteSPReader(db.ConnectionString, spName, parameters); item = rowMapper.MapRows(dataReader); } catch (Exception ex) { validationResults.Add(ex.Message); } finally { if (dataReader != null) { dataReader.Close(); } } return(new BoolResult <IList <TEntity> >(item, validationResults.IsValid, string.Empty, validationResults)); }
/// <summary> /// Executes the rowmappers /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dbHelper">Database helper</param> /// <param name="commandText">Store procedure name. E.g. "Posts_GetPostsByUser</param> /// <param name="commandType">StoredProcedure</param> /// <param name="rowMapper">The mapper that maps a record to an object.</param> /// <param name="dbParameters">Array of parameters for the query.</param> /// <returns></returns> public static IList <T> Query <T>(IDBHelper dbHelper, string commandText, CommandType commandType, DbParameter[] dbParameters, IRowMapper <IDataReader, T> rowMapper) { DbCommand command = dbHelper.BuildCommand(commandText, commandType, dbParameters); IList <T> items = null; using (command.Connection) { command.Connection.Open(); IDataReader reader = command.ExecuteReader(); // Map the workshops. items = rowMapper.MapRows(reader); reader.Close(); command.Dispose(); } return(items); }