【问题标题】:How use Transaction with more than functions using c#如何使用 C# 将 Transaction 与多个函数一起使用
【发布时间】:2018-11-05 10:15:03
【问题描述】:

如何在 C# 中将 Transaction 与多个函数一起使用 例如 我有三个功能

//first function 
save();//------to save data-----
//second function 
saveDetailes(); //-----to save detiales ----------
//third function 
updateStauts(); //--------to update onother table ---------------

我想确保所有这些都使用 TransAction 实现或不实现 谢谢

【问题讨论】:

  • 我们需要更多细节在这里......
  • 你需要什么细节
  • 问如何使用transacton和三个功能一起使用
  • 看来这些方法将一些数据保存到数据库中。我们需要知道您使用的是 ado.net、EF 还是其他一些 ORM。我们需要知道这些方法是使用存储过程还是内联 sql(事实上,如果这实际上是一个关系数据库或 NoSql 数据库)——你给我们的只是你如何调用这些方法——这还不够开始描述问题,更不用说解决它了。
  • 使用 ado.net 和使用 sqlinside visualstideo

标签: c# transactions


【解决方案1】:

经过多次尝试和搜索,我找到了解决问题的方法

解决方案 将 sqlcommand 传递给所有函数并从每个函数返回作为布尔值保存完成返回 true 如果三个函数返回 true 事务被提交 其他方式事务回滚 谢谢

【讨论】:

    【解决方案2】:

    如果我理解正确,您需要在多个方法中使用一个通用的 SqlTransaction。我就是这样做的。首先,您必须将所有方法收集到一个通用方法中。然后,您将一个 SqlConenction 和 SqlTransaction 传递给您的所有方法,并返回一个布尔标志以通知您的主要方法您的查询是否成功。

    using System.Data.SqlClient;
    
    namespace SqlTransationDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Do your sql logic here 
                DoSomething();
            }
    
            private static bool DoSomething()
            {
                try
                {
                    using (var connection = new SqlConnection("SqlConnectionString"))
                    {
                        connection.Open();
    
                        //If not commited, transaction is rolled-back as soon as it is disposed 
                        using (var transaction = connection.BeginTransaction())
                        {
                            //Either use a false loop to break or throw an exception. Your choice. 
                            do
                            {
                                if (!Foo1(connection, transaction))
                                    break;
                                if (!Foo2(connection, transaction))
                                    break;
                                if (!Foo3(connection, transaction))
                                    break;
    
                                //Commit 
                                transaction.Commit();
    
                                return true;
                            }
                            while (false);
                        }
                    }
    
                }
                catch
                {
                    return false;
                }
            }
    
            private static bool Foo1(SqlConnection Connection, SqlTransaction Transaction)
            {
                try
                {
                    using (var command = new SqlCommand())
                    {
                        command.Connection = Connection;
                        command.Transaction = Transaction;
                        command.CommandText = "Query1";
                        command.ExecuteNonQuery();
                    }
    
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            private static bool Foo2(SqlConnection Connection, SqlTransaction Transaction)
            {
                try
                {
                    using (var command = new SqlCommand())
                    {
                        command.Connection = Connection;
                        command.Transaction = Transaction;
                        command.CommandText = "Query2";
                        command.ExecuteNonQuery();
                    }
    
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            private static bool Foo3(SqlConnection Connection, SqlTransaction Transaction)
            {
                try
                {
                    using (var command = new SqlCommand())
                    {
                        command.Connection = Connection;
                        command.Transaction = Transaction;
                        command.CommandText = "Query3";
                        command.ExecuteNonQuery();
                    }
    
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-02
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      相关资源
      最近更新 更多