数据库操作中使用事务进行提速

 //执行多条语句,实现事务。

        public  void ExecuteSqlTran(ArrayList SQLStringList)
        {
         
            SqlConnection cn = this.getcon();
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            SqlTransaction tx = cn.BeginTransaction();
            cmd.Transaction = tx;
            try
            {
                for (int i = 0; i < SQLStringList.Count; i++)
                {
                    string strSQL = SQLStringList[i].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 @ 2010-04-27 20:58  quietwalk  阅读(266)  评论(0编辑  收藏  举报