我在2个选项上有些困惑:
List<TransactionRecord> trList = new List<TransactionRecord>(){..initialize the list with transactionRecords, let say 200 record}
选项A:
foreach(var transaction in trList)
{
try
{
***per iteration make database call to update
UpdateToDB(transaction)
***e.g simple sql update statement without complex joining or other stuff
catch
{
Console.write("Transaction id:" + transaction.Id + "failed update");
}
}
***Will make a lot of db rountrip but every record failure is traceable
选项B
Define a table value parameter
@TVP_TransactionRecords TVP_TransactionRecords
Update tblTransactionRecords SET Value = tr.Value INNER JOIN TVP_TransactionRecords tr ON
tblTransactionRecords.ID = tr.ID
***在代码中
UpdateDbByTableValueParameter(trList);
**This would execute the sql directly without go through several loop
***通过将事务列表作为表值参数传递来更新表,并通过联接进行批量更新。我认为这是更明智的性能,因为它只花费一次数据库往返,但是如果更新有问题,我将无法跟踪导致失败的记录。即使只有一条记录失败,也无法更新200条记录。
你们认为我应该选择选项A以获得最佳的维护工作以进行故障排除吗,或者选项B还有其他解决方法?
我正在使用Dapper .net顺便处理代码