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?
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.
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.
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.
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.
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);
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();
}
You should use ExecuteScalar
:
status = await sqlConnection.ExecuteScalarAsync<short>("SELECT [StatusID] FROM [MyTable] WHERE [ID] = @ID", parameters, commandTimeout: _sqlCommandTimeoutInSeconds)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With