public int ExecuteNonQuery(CommandType commandType, params KeyValuePair<string, SqlParameter[]>[] sqlList)
        {
            int result = 0;
            if (sqlList == null || sqlList.Length < 1)
            {
                return result;
            }            this.Command.CommandType = commandType;
            SqlTransaction transaction = this.Connection.BeginTransaction();
            this.command.Transaction = transaction;            try
            {
                foreach (KeyValuePair<string, SqlParameter[]> item in sqlList)
                {
                    this.Command.CommandText = item.Key;
                    if (item.Value != null && item.Value.Length > 0)
                    {
                        this.Command.Parameters.AddRange(item.Value);
                    }
                    this.Command.ExecuteNonQuery();
                }
                transaction.Commit();
                result = 1;
            }
            catch (Exception)
            {
                try
                {
                    transaction.Rollback();
                }
                catch (Exception)
                {
                    throw;
                }
            }
            finally
            {
                this.Connection.Close();
            }
            return result;
        }只执行了循环的最后一个事务
应该怎么写才能把循环里面的事务都提交啊?

解决方案 »

  1.   


     foreach (KeyValuePair<string, SqlParameter[]> item in sqlList)
                    {
                        this.Command.CommandText = item.Key;
                        if (item.Value != null && item.Value.Length > 0)
                        {
                            this.Command.Parameters.AddRange(item.Value);
                        }
                        this.Command.ExecuteNonQuery();
                    }
    改用 for 循环来写