Dapper - Execute Scalar

Description

The ExecuteScalar is an extension method that can be called from any object of type IDbConnection. It executes the query, and returns the first column of the first row in the result set returned by the query. The additional columns or rows are ignored.

Parameters

The following table shows the different parameters of an ExecuteScalar method.

NameDescription
sqlThe command text to execute.
paramThe command parameters (default = null).
transactionThe transaction to use (default = null).
commandTimeoutThe command timeout (default = null)
commandTypeThe command type (default = null)
  1. using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
  2. {
  3. var name = connection.ExecuteScalar<string>("SELECT Name FROM Customers WHERE CustomerID = 1;");
  4. Console.WriteLine(name);
  5. }

Try it: .NET Core | .NET Framework