我有一个mysql语句,我传递给dapper查询方法,它返回一个null对象。我创建了一个POCO类来保存返回的结果。我直接通过sql编辑器测试了查询,并返回了结果。这是代码:
public ViewEmail GetViewEmail(string xId, int headerId)
{
using (IDbConnection connection = new MySqlConnection(_connectionString))
{
string sql = "SELECT * FROM ViewEmail " +
"WHERE Header_id = @Header_id " +
"AND x_id= '@x_id'";
var viewEmail = connection
.Query<ViewEmail>(sql, new { Header_id = HeaderId, x_id = xId })
.SingleOrDefault();
return viewEmail;
}
}
答案 0 :(得分:2)
使用预备语句时,无需使用'
包装参数。试试这个:
string sql = "SELECT * FROM ViewEmail " +
"WHERE Header_id = @Header_id " +
"AND x_id= @x_id";