键入的数据集和多个事务

时间:2012-10-10 11:51:10

标签: c# .net ado.net

我使用Typed Dataset(.xsd)来访问和更新我的数据库。 我有两个表适配器用于更新两个不同表中的记录。

我无法找到在单笔交易中执行两次更新的方法。

1 个答案:

答案 0 :(得分:2)

您可以使用TransactionScope

            using (var ts = new TransactionScope())
            {
                // Perform updates using different table adapters
                using (var ta1 = new tbl1TableAdapter())
                using (var ta2 = new tbl2TableAdapter())
                {
                    ta1.Update(yourDataSet.tbl1);
                    ta2.Update(yourDataSet.tbl2);
                }

                ts.Complete();
                yourDataSet.AcceptChanges();
            }

您可以阅读TransactionScope课程here