让我们从解决这个问题开始:我被困在使用 MS Access DB 并且无法更改它。
这工作正常:
using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
conn.Open();
var results = conn.Query<string>(
"select FirstName from Students where LastName = @lastName",
new { lastName= "Smith" }
);
conn.Close();
}
这工作正常:
using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
OleDbCommand cmd = new OleDbCommand(
"update Students set FirstName = @firstName, City = @city where LastName = @lastName",
conn
);
cmd.Parameters.AddWithValue("firstName", "John");
cmd.Parameters.AddWithValue("city", "SomeCity");
cmd.Parameters.AddWithValue("lastName", "Smith");
conn.Open();
var result = cmd.ExecuteNonQuery();
conn.Close();
}
这不是......它执行时没有错误,但它在数据库中将 FirstName 设置为“SomeCity”,将 City 设置为“John”:
using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
conn.Open();
var results = conn.Query<string>(
"update Students set FirstName = @firstName, City = @city where LastName = @lastName",
new { firstName = "John", city = "SomeCity", lastName = "Smith" }
);
conn.Close();
}
有任何想法吗?
在下面编辑
如果我使用 DynamicParameters,Dapper 可以工作:
using (OleDbConnection conn = ConnectionHelper.GetConnection())
{
DynamicParameters parameters = new DynamicParameters();
parameters.Add("firstName", "John");
parameters.Add("city", "SomeCity");
parameters.Add("lastName", "Smith");
conn.Open();
var result = conn.Query<string>(
"update Students set FirstName = @firstName, City = @city where LastName = @lastName",
parameters
);
conn.Close();
}