在我的类型化数据集中,Update 方法会作为事务运行吗?

发布于 2024-11-08 02:42:30 字数 1685 浏览 5 评论 0原文

我有一个名为 People 的表的类型化数据集。当您调用表适配器的更新方法并传入表时,它是否作为事务运行?

我担心在某个时刻,xsd 中设置的约束会通过,但数据库会出于某种原因拒绝此项。我想确保整个更新被拒绝,并且我不确定它是否只接受它可以接受的内容,直到发生错误。

如果它作为事务运行,我有这个

Auth_TestDataSetTableAdapters.PeopleTableAdapter tableAdapter = new Auth_TestDataSetTableAdapters.PeopleTableAdapter();
Auth_TestDataSet.PeopleDataTable table = tableAdapter.GetDataByID(1);

table.AddPeopleRow("Test Item", 5.015);
tableAdapter.Update(table);

但是如果我必须在事务中手动捕获它,我最终会得到这个

Auth_TestDataSetTableAdapters.PeopleTableAdapter tableAdapter = new Auth_TestDataSetTableAdapters.PeopleTableAdapter();
Auth_TestDataSet.PeopleDataTable table = tableAdapter.GetDataByID(1);

tableAdapter.Connection.Open();
tableAdapter.Transaction = tableAdapter.Connection.BeginTransaction();

table.AddPeopleRow("Test Item", 5.015);

try
{
    tableAdapter.Update(table);
    tableAdapter.Transaction.Commit();
}
catch
{
    tableAdapter.Transaction.Rollback();
}
finally
{
    tableAdapter.Connection.Close();
}

无论哪种方式都有效,但我对内部工作感兴趣。我决定处理此类行添加的方式还有其他问题吗?

-- 编辑 --

确定它不能作为事务工作,并且会提交,但许多记录都是成功的,直到错误发生。感谢下面有用的帖子,一些事务代码已经被压缩,以便更容易地控制事务:

Auth_TestDataSetTableAdapters.PeopleTableAdapter tableAdapter = new Auth_TestDataSetTableAdapters.PeopleTableAdapter();
Auth_TestDataSet.PeopleDataTable table = tableAdapter.GetDataByID(1);

try
{
    using (TransactionScope ts = new TransactionScope())
    {
        table.AddPeopleRow("Test Item", (decimal)5.015);
        table.AddPeopleRow("Test Item", (decimal)50.015);
        tableAdapter.Update(table);

        ts.Complete();
    }
}
catch (SqlException ex)
{ /* ... */ }

I have a typed dataset for a table called People. When you call the update method of a table adapter and pass in the table, is it run as a transaction?

I'm concerned that at some point the constraints set in the xsd will pass but the database will reject this item for one reason or another. I want to make sure that the entire update is rejected and I'm not sure that it just accepts what it can until that error occurs.

If it runs as a transaction I have this

Auth_TestDataSetTableAdapters.PeopleTableAdapter tableAdapter = new Auth_TestDataSetTableAdapters.PeopleTableAdapter();
Auth_TestDataSet.PeopleDataTable table = tableAdapter.GetDataByID(1);

table.AddPeopleRow("Test Item", 5.015);
tableAdapter.Update(table);

But if I have to manually trap this in a transaction I wind up with this

Auth_TestDataSetTableAdapters.PeopleTableAdapter tableAdapter = new Auth_TestDataSetTableAdapters.PeopleTableAdapter();
Auth_TestDataSet.PeopleDataTable table = tableAdapter.GetDataByID(1);

tableAdapter.Connection.Open();
tableAdapter.Transaction = tableAdapter.Connection.BeginTransaction();

table.AddPeopleRow("Test Item", 5.015);

try
{
    tableAdapter.Update(table);
    tableAdapter.Transaction.Commit();
}
catch
{
    tableAdapter.Transaction.Rollback();
}
finally
{
    tableAdapter.Connection.Close();
}

Either way works but I am interested in the inner workings. Any other issues with the way I've decided to handle this type of row addition?

-- EDIT --

Determined that it does not work as a transaction and will commit however many records are successful until the error occurs. Thanks to the helpful post below a bit of that transactional code has been condensed to make controlling the transaction easier on the eyes:

Auth_TestDataSetTableAdapters.PeopleTableAdapter tableAdapter = new Auth_TestDataSetTableAdapters.PeopleTableAdapter();
Auth_TestDataSet.PeopleDataTable table = tableAdapter.GetDataByID(1);

try
{
    using (TransactionScope ts = new TransactionScope())
    {
        table.AddPeopleRow("Test Item", (decimal)5.015);
        table.AddPeopleRow("Test Item", (decimal)50.015);
        tableAdapter.Update(table);

        ts.Complete();
    }
}
catch (SqlException ex)
{ /* ... */ }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光瘦了 2024-11-15 02:42:30

你的方法应该有效。

不过你可以稍微简化一下:

using (TransactionScope ts = new TransactionScope())
{
     // your old code here
     ts.Complete();
}

Your approach should work.

You can simplify it a little though:

using (TransactionScope ts = new TransactionScope())
{
     // your old code here
     ts.Complete();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文