实现事务处理.


        
/// <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);
                }

            }

        }
 

posted on 2007-06-03 18:13  小土泥  阅读(123)  评论(0编辑  收藏  举报

导航