首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在C#中使用带有类型化数据集的事务?

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

Stack Overflow用户
提问于 2011-03-23 09:55:12
回答 3查看 6.4K关注 0票数 3

您好,如何将事务与类型化数据集一起使用?假设我有一个记录表和记录明细表,我必须在记录表中保存一个表,并在记录明细表中保存所有详细信息。如何使用?我发现事务可以与非类型化数据集一起使用,但我不能与类型化数据集一起使用。有人能告诉我我该怎么做吗?

凯文

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-23 10:04:34

CodeProject上有一个nice article (和代码),介绍了如何扩展类型化数据集以启用事务,而不会在使用TransactionScope时将其提升为分布式事务。

摘要:将事务作用域与在分部类上添加的方法一起使用,可以修改底层SqlCommand对象以参与同一事务。

代码语言:javascript
运行
复制
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();
}

参考资料中的适配器代码示例:

代码语言:javascript
运行
复制
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;
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2011-03-23 10:01:11

您可以在TransactionScope对象的作用域内使用TransactionScope并调用这两个更新。

请参阅此链接中提供的示例。TransactionScope

here提供了一个更接近的示例。

票数 1
EN

Stack Overflow用户

发布于 2018-02-03 01:17:33

我不推荐使用transactionscope,因为它很难处理代码重构,因为你可以包装大量的代码,这是我推荐的方法:

这个答案是不同的,因为它为所有命令添加了事务,而不仅仅是持久性

扩展您的部分Adapter类:

代码语言:javascript
运行
复制
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);
        }
    }
}

扩展方法:

代码语言:javascript
运行
复制
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;
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5400013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档