尝试在Dapper中使用参数化查询时遇到问题。我发现其他一些用户遇到类似问题,但无法解决问题。
代码
public User GetUser(int employeeId)
{
var args = new
{
EmployeeId = employeeId
};
const string sql = @"
select
first_name 'FirstName',
last_name 'LastName'
from
users
where
employee_id = @EmployeeId
";
using (var con = MakeConnection())
{
var r = con.Query<User>(sql, args);
return r.FirstOrDefault();
}
}
错误
A first chance exception of type 'System.Data.Odbc.OdbcException' occurred in System.Data.dll
Additional information: ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@EmployeeId".
我也尝试过使用DynamicParameters
并传递它,但这不起作用
var p = new DynamicParameters();
p.Add("@EmployeeId", employeeId); // I have also tried without the @
//...
var r = con.Query<User>(sql,p);