SqlConnection - ExecuteScalarTo
A SqlConnection extension method that executes the scalar operation.
public static void Main() { string sql = "SELECT 1 AS IntColumn, 'FizzBuzz' AS StringColumn WHERE @Fizz = 1"; var dict = new Dictionary<string, object> {{"@Fizz", 1}}; using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())) { conn.Open(); //C# Extension Method: SqlConnection - ExecuteScalarTo var result = conn.ExecuteScalarTo<int>(sql, dict.ToSqlParameters()); Console.WriteLine(result); } }
View Source
using System; using System.Data; using System.Data.SqlClient; public static partial class Extensions { /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <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 object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters, CommandType commandType, SqlTransaction transaction) { using (SqlCommand command = @this.CreateCommand()) { command.CommandText = cmdText; command.CommandType = commandType; command.Transaction = transaction; if (parameters != null) { command.Parameters.AddRange(parameters); } return command.ExecuteScalar().To<T>(); } } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="commandFactory">The command factory.</param> /// <returns>An object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, Action<SqlCommand> commandFactory) { using (SqlCommand command = @this.CreateCommand()) { commandFactory(command); return command.ExecuteScalar().To<T>(); } } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <returns>An object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText) { return @this.ExecuteScalarTo<T>(cmdText, null, CommandType.Text, null); } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="cmdText">The command text.</param> /// <param name="transaction">The transaction.</param> /// <returns>An object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, SqlTransaction transaction) { return @this.ExecuteScalarTo<T>(cmdText, null, CommandType.Text, transaction); } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <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 object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, CommandType commandType) { return @this.ExecuteScalarTo<T>(cmdText, null, commandType, null); } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <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 object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, CommandType commandType, SqlTransaction transaction) { return @this.ExecuteScalarTo<T>(cmdText, null, commandType, transaction); } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <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 object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters) { return @this.ExecuteScalarTo<T>(cmdText, parameters, CommandType.Text, null); } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <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 object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters, SqlTransaction transaction) { return @this.ExecuteScalarTo<T>(cmdText, parameters, CommandType.Text, transaction); } /// <summary> /// A SqlConnection extension method that executes the scalar operation. /// </summary> /// <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 object.</returns> public static T ExecuteScalarTo<T>(this SqlConnection @this, string cmdText, SqlParameter[] parameters, CommandType commandType) { return @this.ExecuteScalarTo<T>(cmdText, parameters, commandType, null); } }