类型化数据集中的事务 [英] Transactions in Typed DataSets

查看:118
本文介绍了类型化数据集中的事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有一个带有几个相关表的类型化数据集,并在这些表之间定义了关系.处理数据Feed时,我要添加,修改和删除记录,然后在每个表上调用update.

Have a typed dataset with several related tables, and relations defined between those tables. As I process a datafeed, I'm adding, modifying, and removing records, then calling update on each table.

Requests        Reapprovals        UserRole
 RequestId ----- RequestId    ----- RoleId 
 Reason          RoleId  ----/      UserId 

使用类型化数据集的原因是我必须检查现有数据以确定是要添加,修改还是删除记录...,因此我需要将正在处理的所有内容全部转储(另一种方法是当我一一处理记录时,将有10,000个针对数据库的查询.

The reason for using a typed dataset is that I have to check existing data to determine whether I'm adding, modifying, or removing records... so I need the full dump of everything I'm working with (the alternative would be 10,000 queries against the database as I process the records one by one).

我需要交易支持,但是我没有找到一种处理类型化数据集的方法.例如,当我创建新的重新批准时,我正在创建一个新的请求.但是,如果重新批准无法更新,我不想保留该请求.

I want transactional support, but I'm not seeing a way to do it with typed datasets. For example, I'm creating a new request when I create a new reapproval. But if the reapproval fails to update, I don't want to keep the request.

将更新调用放在TransactionScope下将意味着,如果任何记录失败,它们都将失败.不是我想要的.

Putting the update calls under a TransactionScope would mean that if any record fails, they all fail. Not what I want.

如何在类型化的数据集中提交或回退相关行?

How would I commit or roll back related rows in a typed dataset?

推荐答案

您可以使用常规事务,还可以通过TableAdapterManager实现类似事务的功能,如以下示例所示.

You can use regular transactions and also achieve transaction like feature from TableAdapterManager as like in below examples.

使用常规交易的第一种方法,

First Approach to use regular transaction,

   public void  savewithTransacition()
    {
        DataSet1TableAdapters.Table1TableAdapter taTbl1 = new DataSet1TableAdapters.Table1TableAdapter();
        DataSet1TableAdapters.Table2TableAdapter taTbl2 = new DataSet1TableAdapters.Table2TableAdapter();
        SqlTransaction st = null;
        SqlConnection sc = new SqlConnection("ur conneciton string");
        try
        {
            sc.Open();
            st = sc.BeginTransaction();

            taTbl1.Transaction = st;
            taTbl2.Transaction = st;
            st.Commit();
        }
        catch (System.Exception ex)
        {
            st.Rollback();
            throw ex;
        }


    }

第二..使用表适配器管理器..

Second..with table adapter manager..

  public void SaveWithManager()
    {
        DataSet1TableAdapters.TableAdapterManager mgr1 = new DataSet1TableAdapters.TableAdapterManager();
        DataSet1TableAdapters.Table1TableAdapter taTbl1 = new DataSet1TableAdapters.Table1TableAdapter();
        DataSet1TableAdapters.Table2TableAdapter taTbl2 = new DataSet1TableAdapters.Table2TableAdapter();

        mgr1.Table1TableAdapter = taTbl1;
        mgr1.Table2TableAdapter = taTbl2;
        mgr1.UpdateOrder = DataSet1TableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete; 
        mgr1.UpdateAll(this);
    }

使用此选项,您可以为要保存的表组创建TAManager.例如,如果您要保存一个组,即使另一个组失败了.

With this option you can create TAManagers for group of tables to save. like if you want one group to save and even if another get fail.

这篇关于类型化数据集中的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆