/// <summary> /// Executes a query, returning the data typed as <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">The type of results to return.</typeparam> /// <param name="db">The DB to query on.</param> /// <param name="sql">The SQL to execute for the query.</param> /// <param name="param">The parameters to pass, if any.</param> /// <param name="buffered">Whether to buffer results in memory.</param> /// <param name="commandTimeout">The command timeout (in seconds).</param> /// <param name="commandType">The type of command to execute.</param> /// <returns> /// A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive). /// </returns> public static IEnumerable <T> Query <T>(this Db db, string sql, object param = null, bool buffered = true, int?commandTimeout = null, CommandType?commandType = null) { var command = new CommandDefinition(sql, param, db.Transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None); var data = SqlMapper.QueryImpl <T>(db.Connection, command, typeof(T)); return(command.Buffered ? data.ToList() : data); }
/// <summary> /// Executes a single-row query, returning the data typed as <paramref name="type"/>. /// </summary> /// <param name="db">The DB to query on.</param> /// <param name="type">The type to return.</param> /// <param name="sql">The SQL to execute for the query.</param> /// <param name="param">The parameters to pass, if any.</param> /// <param name="buffered">Whether to buffer results in memory.</param> /// <param name="commandTimeout">The command timeout (in seconds).</param> /// <param name="commandType">The type of command to execute.</param> /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception> /// <returns> /// A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive). /// </returns> public static IEnumerable <object> Query(this Db db, Type type, string sql, object param = null, bool buffered = true, int?commandTimeout = null, CommandType?commandType = null) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var command = new CommandDefinition(sql, param, db.Transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None); var data = SqlMapper.QueryImpl <object>(db.Connection, command, type); return(command.Buffered ? data.ToList() : data); }
/// <summary> /// Executes a query, returning the data typed as <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">The type of results to return.</typeparam> /// <param name="db">The DB to query on.</param> /// <param name="command">The command used to query on this connection.</param> /// <returns> /// A sequence of data of <typeparamref name="T"/>; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is /// created per row, and a direct column-name===member-name mapping is assumed (case insensitive). /// </returns> public static IEnumerable <T> Query <T>(this Db db, CommandDefinition command) { var data = SqlMapper.QueryImpl <T>(db.Connection, command, typeof(T)); return(command.Buffered ? data.ToList() : data); }