SqlConnection - ExecuteEntities
Enumerates execute entities in this collection.
public static void Main() { string sql = "SELECT 1 AS IntColumn, 'FizzBuzz' AS StringColumn UNION SELECT 2, 'BuzzBuzz' WHERE @Fizz = 1"; var dict = new Dictionary<string, object> {{"@Fizz", 1}}; using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())) { conn.Open(); //C# Extension Method: SqlConnection - ExecuteEntities List<TestObject> list = conn.ExecuteEntities<TestObject>(sql, dict.ToSqlParameters()).ToList(); FiddleHelper.WriteTable(list); } } public class TestObject { public int IntColumn { get; set; } public string StringColumn { get; set; } }
View Source
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; public static partial class Extensions { /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="parameters">Options for controlling the operation.</param> /// <param name="commandType">Type of the command.</param> /// <param name="transaction">The transaction.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters, CommandType commandType, SqlTransaction transaction) where T : new() { using (SqlCommand command = @this.CreateCommand()) { command.CommandText = cmdText; command.CommandType = commandType; command.Transaction = transaction; if (parameters != null) { command.Parameters.AddRange(parameters); } using (IDataReader reader = command.ExecuteReader()) { return reader.ToEntities<T>(); } } } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="commandFactory">The command factory.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, Action<SqlCommand> commandFactory) where T : new() { using (SqlCommand command = @this.CreateCommand()) { commandFactory(command); using (IDataReader reader = command.ExecuteReader()) { return reader.ToEntities<T>(); } } } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText) where T : new() { return @this.ExecuteEntities<T>(cmdText, null, CommandType.Text, null); } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="transaction">The transaction.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, SqlTransaction transaction) where T : new() { return @this.ExecuteEntities<T>(cmdText, null, CommandType.Text, transaction); } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="commandType">Type of the command.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, CommandType commandType) where T : new() { return @this.ExecuteEntities<T>(cmdText, null, commandType, null); } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="commandType">Type of the command.</param> /// <param name="transaction">The transaction.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, CommandType commandType, SqlTransaction transaction) where T : new() { return @this.ExecuteEntities<T>(cmdText, null, commandType, transaction); } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="parameters">Options for controlling the operation.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters) where T : new() { return @this.ExecuteEntities<T>(cmdText, parameters, CommandType.Text, null); } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="parameters">Options for controlling the operation.</param> /// <param name="transaction">The transaction.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters, SqlTransaction transaction) where T : new() { return @this.ExecuteEntities<T>(cmdText, parameters, CommandType.Text, transaction); } /// <summary> /// Enumerates execute entities in this collection. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="parameters">Options for controlling the operation.</param> /// <param name="commandType">Type of the command.</param> /// <returns>An enumerator that allows foreach to be used to process execute entities in this collection.</returns> public static IEnumerable<T> ExecuteEntities<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters, CommandType commandType) where T : new() { return @this.ExecuteEntities<T>(cmdText, parameters, commandType, null); } }