简体   繁体   English

如何在C#中使用带有类型化数据集的事务?

[英]How to use transaction with typed dataset in C#?

Hi How can I use transaction with typed dataset? 嗨我如何使用带有类型化数据集的事务? Let's say I've a record table and record detail table and I've to save one in the record table and all the details in the record detail table. 假设我有一个记录表和记录详细信息表,我将在记录表中保存一个,并在记录详细信息表中保存所有详细信息。 How can I use ? 我该怎么用? I found transaction can be used with untyped dataset but I don't see it with typed dataset. 我发现事务可以用于非类型化数据集,但我没有看到它与类型化数据集。 Can someone tell me how I should do? 谁能告诉我应该怎么办?

Kevin 凯文

There's a nice article (and code) at CodeProject on how to extend the typed dataset to enable transactions without having them get promoted to a distributed transaction when using the TransactionScope. 在CodeProject上有一篇很好的文章 (和代码),关于如何扩展类型化数据集以启用事务,而不会在使用TransactionScope时将它们提升为分布式事务。

Summary: Use the transaction scope with a method added on the partial class to modify the underlying SqlCommand objects to take part in the same transaction. 简介:使用事务范围和在partial类上添加的方法来修改底层SqlCommand对象以参与同一事务。

using (SqlTransaction transaction = connection.BeginTransaction())
{
       // These methods will update all relevant command objects’ transaction property
       adapter1.EnlistTransaction(transaction);
       adapter2.EnlistTransaction(transaction);

       adapter1.Update(table1);
       adapter2.Update(table2);

       transaction.Commit();
}

Code example for adapter from reference: 来自参考的适配器的代码示例:

public partial class [TableAdapterName]
{
    public void EnlistTransaction(System.Data.SqlClient.SqlTransaction transaction)
    {
        System.Data.SqlClient.SqlTransaction _transaction;

        if (this._transaction != null)
        {
            throw new System.InvalidOperationException
        ("This adapter has already been enlisted in a transaction");
        }
        else
        {
            this._transaction = transaction;
            Adapter.UpdateCommand.Transaction = _transaction;
            Adapter.InsertCommand.Transaction = _transaction;
            Adapter.DeleteCommand.Transaction = _transaction;
        }
    }
}

I don't recommend using transactionscope, since it's difficult for handling code refactor since you can wrap a huge amount of code, this is how I recommend: 我建议不要使用Transactionscope,因为处理代码重构很困难,因为你可以包装大量的代码,这就是我的建议:

This answer it's different because it's adding the transaction for all commands and not only for persistence 这个答案是不同的,因为它为所有命令添加了事务,而不仅仅是为了持久性

Extend your partial Adapter class: 扩展您的部分Adapter类:

partial class YourTableAdapter
{
    public SqlTransaction Transaction
    {
        set
        {
            if (this.CommandCollection != null)
            {
                for (int i = 0; i < this.CommandCollection.Length; i++)
                {
                    this.CommandCollection[i].Connection = value.Connection;
                    this.CommandCollection[i].Transaction = value;
                }
            }

            this.Connection = value.Connection;
            this._adapter.AplicaTransaction(value);
        }
    }
}

Extension method: 扩展方法:

namespace System
{
    public static class DALSqlExtension
    {
        public static void AplicaTransaction(this SqlDataAdapter _adapter, SqlTransaction transaction)
        {
            if (_adapter == null)
            {
                return;
            }
            if (_adapter.InsertCommand != null)
            {
                _adapter.InsertCommand.Transaction = transaction;
                _adapter.InsertCommand.Connection = transaction.Connection;
            }
            if (_adapter.UpdateCommand != null)
            {
                _adapter.UpdateCommand.Transaction = transaction;
                _adapter.UpdateCommand.Connection = transaction.Connection;
            }
            if (_adapter.DeleteCommand != null)
            {
                _adapter.DeleteCommand.Transaction = transaction;
                _adapter.DeleteCommand.Connection = transaction.Connection;
            }
            if (_adapter.SelectCommand != null)
            {
                _adapter.SelectCommand.Transaction = transaction;
                _adapter.SelectCommand.Connection = transaction.Connection;
            }
        }
    }
}

You can use TransactionScope and call both the updates within the scope of the TransactionScope object. 您可以使用TransactionScope并调用TransactionScope对象范围内的更新。

Please see the example provided in this link. 请参阅此链接中提供的示例。 TransactionScope . TransactionScope

An more close example is provided here . 这里提供一个更接近的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM