DbConnection - ExecuteScalar
A DbConnection extension method that executes the scalar operation.
public static void Main() { const string sql = @"SELECT 1 As IntColumn WHERE @Fizz = 1"; var dict = new Dictionary<string, object> {{"@Fizz", 1}}; using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())) { conn.Open(); //C# Extension Method: DbConnection - ExecuteScalar object result = conn.ExecuteScalar(sql, dict.ToDbParameters(conn)); Console.WriteLine(result); } }
View Source
using System; using System.Data; using System.Data.Common; public static partial class Extensions { /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, DbParameter[] parameters, CommandType commandType, DbTransaction transaction) { using (DbCommand command = @this.CreateCommand()) { command.CommandText = cmdText; command.CommandType = commandType; command.Transaction = transaction; if (parameters != null) { command.Parameters.AddRange(parameters); } return command.ExecuteScalar(); } } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, Action<DbCommand> commandFactory) { using (DbCommand command = @this.CreateCommand()) { commandFactory(command); return command.ExecuteScalar(); } } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText) { return @this.ExecuteScalar(cmdText, null, CommandType.Text, null); } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, DbTransaction transaction) { return @this.ExecuteScalar(cmdText, null, CommandType.Text, transaction); } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, CommandType commandType) { return @this.ExecuteScalar(cmdText, null, commandType, null); } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, CommandType commandType, DbTransaction transaction) { return @this.ExecuteScalar(cmdText, null, commandType, transaction); } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, DbParameter[] parameters) { return @this.ExecuteScalar(cmdText, parameters, CommandType.Text, null); } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, DbParameter[] parameters, DbTransaction transaction) { return @this.ExecuteScalar(cmdText, parameters, CommandType.Text, transaction); } /// <summary> /// A DbConnection 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 object ExecuteScalar(this DbConnection @this, string cmdText, DbParameter[] parameters, CommandType commandType) { return @this.ExecuteScalar(cmdText, parameters, commandType, null); } }