Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a single value from the database using Dapper

I'm using Dapper and trying to retrieve a short from the database and would like to do this without grabbing it from a collection.

I've got the following code, which doesn't work because QueryAsync<short> returns IEnumerable<short>.

short status;
using (var sqlConnection = new SqlConnection(connectionString))
{
    var parameters = new DynamicParameters();
    parameters.Add("@ID", ID, DbType.Int32, ParameterDirection.Input);

    await sqlConnection.OpenAsync();
    status = await sqlConnection.QueryAsync<short>("SELECT [StatusID] FROM [MyTable] WHERE [ID] = @ID", parameters, commandTimeout: _sqlCommandTimeoutInSeconds);
}

Can I get Dapper to return a single value, instead of a collection?

like image 226
Nic Avatar asked Jul 18 '16 07:07

Nic


People also ask

Which returns a single value from a database query?

The Command object provides the capability to return single values using the ExecuteScalar method. The ExecuteScalar method returns, as a scalar value, the value of the first column of the first row of the result set. The following code example inserts a new value in the database using a SqlCommand.

Which dapper method should be used when updating a single row?

Fastest Dapper Plus Extensions You can easily update a single row by writing an UPDATE statement with parameters for each column you want to update. It a simple SQL UPDATE statement on the Books table. There are the columns and their values corresponding to parameters. The Execute extension method of Dapper.

How can you select a single row?

In SQLJ, a single-row query can be executed and its result set data can be retrieved with a single statement: SELECT ... INTO <select target list> . The INTO-clause contains a list of host variables or host expressions that receive the result set data.

What is Queryfirst in dapper?

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.


3 Answers

FYI, Dapper has now added both QuerySingle and QuerySingleAsync as well as their corresponding OrDefault variants... usage for QuerySingleOrDefaultAsync is:

await connection.QuerySingleOrDefaultAsync<short>(sql);
like image 185
David Mohundro Avatar answered Sep 20 '22 12:09

David Mohundro


Either you can use ExecuteScalarAsync or Single() with ExecuteScalarAsync you can retrieve a single value from database using Dapper.

short status;
using (var sqlConnection = new SqlConnection(connectionString))
{
  var parameters = new DynamicParameters();
  parameters.Add("@ID", ID, DbType.Int32, ParameterDirection.Input);

  await sqlConnection.OpenAsync();
  status = await sqlConnection.ExecuteScalarAsync<short>("SELECT [StatusID] FROM     [MyTable] WHERE [ID] = @ID", parameters, commandTimeout: _sqlCommandTimeoutInSeconds);
}

Single() can be used in this way

short status;
using (var sqlConnection = new SqlConnection(connectionString))
{
  var parameters = new DynamicParameters();
  parameters.Add("@ID", ID, DbType.Int32, ParameterDirection.Input);

  await sqlConnection.OpenAsync();
  status = await sqlConnection.QueryAsync<short>("SELECT [StatusID] FROM [MyTable] WHERE [ID] = @ID", parameters, commandTimeout: _sqlCommandTimeoutInSeconds).Single();
}
like image 28
Aftab Ahmed Avatar answered Sep 18 '22 12:09

Aftab Ahmed


You should use ExecuteScalar:

status = await sqlConnection.ExecuteScalarAsync<short>("SELECT [StatusID] FROM [MyTable] WHERE [ID] = @ID", parameters, commandTimeout: _sqlCommandTimeoutInSeconds)
like image 31
Zein Makki Avatar answered Sep 20 '22 12:09

Zein Makki