zoukankan      html  css  js  c++  java
  • C# 执行多条SQL更新语句,实现数据库事务

    class Program
        {
            class Result<T>
            {
                public T data;
                public string Message;
                public bool Success;
                public string StackTrace;
            }
     
            struct ExecuteableUnit
            {
                public string SQL;
                public SqlParameter[] param;
            }
     
            /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
            private static Result<int> ExecuteSqlTransaction(params ExecuteableUnit[] executeableUnits)
            {
                using (SqlConnection connection = new SqlConnection(""))
                {
                    connection.Open();
                    SqlCommand command = connection.CreateCommand();
                    SqlTransaction transaction = connection.BeginTransaction();
                    command.Connection = connection;
                    command.Transaction = transaction;
                    int result = 0;
                    try
                    {
                        foreach(ExecuteableUnit exeUnit in executeableUnits)
                        {
                            command.CommandText = exeUnit.SQL;
                            if(exeUnit.param.GetLength(1) > 0)
                            {
                                foreach(SqlParameter p in exeUnit.param)
                                    command.Parameters.Add(p);
                            }
                            result += command.ExecuteNonQuery();
                        }
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        // Attempt to roll back the transaction.
                        try
                        {
                            transaction.Rollback();
                        }
                        catch (Exception ex2)
                        {
                            return new Result<int>()
                            {
                                Success = false, Message = ex2.Message, StackTrace = ex2.StackTrace
                            };
                        }
                        return new Result<int>()
                        {
                            Success = false, Message = ex.Message, StackTrace = ex.StackTrace
                        };
                    }
                    finally
                    {
                        // Attempt to roll back the transaction.
                        try
                        {
                            connection.Close();
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    return new Result<int>()
                    {
                        Success = true, data = result
                    };
                }
            }
     
            public static void Main(string[] args)
            {
            }
        }
  • 相关阅读:
    EF中嵌套类的where查询
    VS中添加Web References
    DropDownList绑定数据源后,要插入项的处理
    CheckBoxList选中某项,获取其它项是否是选中
    WebAPI的使用
    HTML5中像网页中保存cookie的实现
    日志切割之Logrotate
    CentOS防火墙iptables使用
    CentOS7安装Python3
    Keepalived高可用
  • 原文地址:https://www.cnblogs.com/rinack/p/4623993.html
Copyright © 2011-2022 走看看