问题描述
我正在使用Dapper更新和插入Access DB.代码正在工作,不会引发异常,但不会更新DB中的值.下面是我的代码
I am using Dapper to Update and Insert Access DB. Code is working not throwing exception but its not updating the value in DB. Below is my code
sql.Append("UPDATE drugs_repository SET drug_name = @DrugName ");
sql.Append(" WHERE id = @DrugId");
var parameters = new
{
DrugName = objDrug.DrugName,
DrugId = objDrug.DrugId
};
var t = connection.Query<string>(sql.ToString(), parameters);
有人可以让我知道以上代码中我到底缺少什么吗?当我对值进行硬编码时,要比在数据库中更新它.因此可能与参数有关.
Can someone please let me know what exactly I am missing in the above code?When I hardcode the value than its updating in the DB. So probably its related to parameter.
推荐答案
如果您对删除Dapper代码中的.OrderBy()
可能产生的副作用感到不安,那么一种解决方法是使用这样的方式来命名您的参数:它们将按照在SQL命令中出现的顺序进行排序.例如,我怀疑如果将参数分别命名为@1DrugName
和@2DrugId
,则未修改的Dapper代码可能会正常工作.
If you are nervous about possible side-effects from removing the .OrderBy()
in the Dapper code then a workaround would be to name your parameters in such a way that they will sort in the same order that they appear in the SQL command. For example, I suspect that the unmodified Dapper code would probably work okay if the parameters were named @1DrugName
and @2DrugId
.
这篇关于Dapper与MS Access更新和插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!