如何使用Dapper从数据库中检索单个值

时间:2022-09-25 12:33:04

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.

我正在使用Dapper并试图从数据库中检索一个短信息,并希望在不从集合中获取它的情况下进行此操作。

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

我有下面的代码,它不起作用,因为QueryAsync 返回IEnumerable

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?

我可以让Dapper返回一个值,而不是一个集合吗?

2 个解决方案

#1


4  

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

您可以使用ExecuteScalarAsync或Single()与ExecuteScalarAsync,也可以使用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();
}

#2


0  

You should use ExecuteScalar :

你应该使用ExecuteScalar:

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

#1


4  

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

您可以使用ExecuteScalarAsync或Single()与ExecuteScalarAsync,也可以使用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();
}

#2


0  

You should use ExecuteScalar :

你应该使用ExecuteScalar:

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