zoukankan      html  css  js  c++  java
  • 实现事务处理.


            
    /// <summary>
            
    /// 执行多条SQL语句,实现数据库事务。
            
    /// </summary>
            
    /// <param name="SQLStringList">多条SQL语句</param>        

            public static void ExecuteTransaction(ArrayList SQLStringList)
            
    {
                
    using (SqlConnection conn = new SqlConnection(connectionString))
                
    {
                    conn.Open();
                    SqlCommand cmd 
    = new SqlCommand();
                    cmd.Connection
    =conn;                
                    SqlTransaction tx
    =conn.BeginTransaction();            
                    cmd.Transaction
    =tx;                
                    
    try
                    
    {           
                        
    for(int n=0;n<SQLStringList.Count;n++)
                        
    {
                            
    string strsql=SQLStringList[n].ToString();
                            
    if (strsql.Trim().Length>1)
                            
    {
                                cmd.CommandText
    =strsql;
                                cmd.ExecuteNonQuery();
                            }

                        }
                                            
                        tx.Commit();                    
                    }

                    
    catch(System.Data.SqlClient.SqlException E)
                    
    {        
                        tx.Rollback();
                        
    throw new Exception(E.Message);
                    }

                }

            }
     
  • 相关阅读:
    【JAVA】java 堆溢出分析
    【数据结构】链表的基本学习 实现
    【其他】博客园自定义样式指南
    【JAVA】一些好用的开源java类库
    【Shell】Shell 常用命令
    【Spring】Spring Boot 要点整理
    【数据库】Mysql 连接相关
    【Linux】Shell 参数解析
    Erlang 中类型转换
    erlang 中 maps 练习
  • 原文地址:https://www.cnblogs.com/xiaotuni/p/2365749.html
Copyright © 2011-2022 走看看