今天我遇到了一个我从未见过的有趣问题。我在我的MVC应用程序中使用Dapper v1.42.0来更新SQL数据库中的记录。更新运行后,我将重定向到一个操作,该操作选择刚刚更新的相同数据。问题是,SELECT语句没有获得更新的记录。看起来数据库在运行SELECT之前还没有提交UPDATE,但我不知道这是怎么回事。以下是从客户端ajax调用UPDATE调用的代码:
[HttpPost]
public ActionResult UpdateCommission(CommissionDetailVM updatedCommModel)
{
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
string queryTable;
if (updatedCommModel.DocType == "Invoice")
{
queryTable = "[MSI$Sales Invoice Line]";
}
else
{
queryTable = "[MSI$Sales Cr_Memo Line]";
}
sqlConn.Execute(String.Format("UPDATE {0} SET FreightCharge = @freight, PalletCharge = @pallet, [Territory Code] = @territory, BrokerCommission = @broker, MiscCharge = @misc WHERE [Document No_] = @docNo AND [Line No_] = @lineNo", queryTable),
new
{
@docNo = updatedCommModel.InvoiceNo,
@lineNo = updatedCommModel.LineNum,
@freight = updatedCommModel.Freight,
@territory = updatedCommModel.Broker,
@broker = updatedCommModel.Commission,
@misc = updatedCommModel.MiscCharge,
@pallet = updatedCommModel.Pallets
});
}
CommissionUpdate model = new CommissionUpdate();
model.DocumentNumber = updatedCommModel.InvoiceNo;
model.DocumentType = updatedCommModel.DocType;
return RedirectToAction("Commission", new { commModel = model });
}
这是在UPDATE之后重定向到的动作(SELECT):
[HttpPost]
public ActionResult Commission(CommissionUpdate commModel)
{
string queryTable;
if (commModel.DocumentType == "Invoice")
{
queryTable = "[MSI$Sales Invoice Line]";
}
else
{
queryTable = "[MSI$Sales Cr_Memo Line]";
}
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
var result = sqlConn.Query<CommissionDetail>(String.Format("SELECT [Document No_] as invoiceNo, [Line No_] as lineNum, Description, FreightCharge as freight, PalletCharge as pallets, MiscCharge, [Territory Code] as broker, BrokerCommission as commission FROM {0} WHERE [Document No_] = @docNo Order by [Line No_]", queryTable),
new { @docNo = commModel.DocumentNumber }).ToList();
ViewBag.Brokers = sqlConn.Query<KeyValue>("SELECT LTRIM(RTRIM(Code)) as [Key], LTRIM(RTRIM(Name)) as [Value] FROM [MSI$Territory]").ToList();
commModel.CommissionDetails = result;
}
return View("Commission", commModel);
}
非常感谢任何帮助/想法!干杯!