.Execute(sQuery,...) in Dapper returns integer.
string sQuery = "SELECT FirstName FROM Customers WHERE Id = @Id";
How can I implement this to return FirstName?
For executing query with Asp.Net Core and Dapper, try SqlConnection
with Query
.
Here is complete code.
using Dapper;
public class CustomersController : Controller
{
private readonly ApplicationDbContext _context;
private readonly IConfiguration _configuration;
public CustomersController(ApplicationDbContext context
, IConfiguration configuration)
{
_context = context;
_configuration = configuration;
}
// GET: Customers/Details/5
public async Task<IActionResult> Details(int? id)
{
using (var connection = new SqlConnection(_configuration.GetConnectionString("DefaultConnection")))
{
string sQuery = "SELECT Name FROM Customer WHERE Id = @Id";
var customer = connection.QueryFirstOrDefault<string>(sQuery, new { Id = id});
}
//Rest Code
}