使用三层开发时,两个连续的添加,如果第二个出现错误,如何使第一个回滚呢?
mainFeeDAL1.Add(mianFee1);//如果这条命令成功添加chuandaiFeeDAL1.Add(chuandaiFee1);//但是这条命令出现了错误如何使第一条回滚呢?

解决方案 »

  1.   

    1.
      把2个sql写在一个事务里面2.
      第2条出错就把第一条记录删除。。呵呵
      

  2.   

    你可以写成Sql的存储过程啊
    在其中用Sql的事物
      

  3.   

    两种方式:1.使用存储过程,在里面使用事务。
    2.使用ADO.net事务处理机制。
      

  4.   


    事务处理,三层的话,在BLL层开启事物进行处理。简单点话,用存储过程里的事务吧
      

  5.   

    无非是事务二字。
    try
       {
        cmd.CommandText = "";//插入信息1
        cmd.ExecuteNonQuery();
        cmd.CommandText = "";//插入信息2
        cmd.ExecuteNonQuery();
        tran.Commit();
        MessageBox.Show("事务提交成功!");
       }
       catch(Exception ex)
       {
        tran.Rollback();
        MessageBox.Show("Error!"+ex.Message);
       }    
      }
      

  6.   

    //建议增加一个事务层,并从现有DAL层增加若干方法,返回SqlList
    public bool ExecuteCommand(ArrayList SqlList)
            {
                DbConnection con = CreateConnection();
                con.Open();
                bool iserror = false;
                string strerror = "";
                DbTransaction SqlTran = con.BeginTransaction();
                try
                {
                    for (int i = 0; i < SqlList.Count; i++)
                    {                    DbCommand _command = GetDbProviderFactory().CreateCommand();
                        _command.Connection = con;
                        _command.CommandTimeout = 500;
                        _command.CommandText = SqlList[i].ToString();
                        _command.Transaction = SqlTran;
                        _command.ExecuteNonQuery();
                        _command.Dispose();
                    }            }
                catch (Exception ex)
                {
                    iserror = true;
                    strerror = ex.Message;            }
                finally
                {                if (iserror)
                    {
                        SqlTran.Rollback();
                        throw new Exception(strerror);
                    }
                    else
                    {
                        SqlTran.Commit();
                    }
                    con.Close();
                }
                if (iserror)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }        public int ExecuteCommand(ArrayList SqlList, List<System.Data.Common.DbParameter[]> paramlist)
            {
                DbConnection con = CreateConnection();
                con.Open();
                bool iserror = false;
                string strerror = "";
                int count = 0;
                DbTransaction SqlTran = con.BeginTransaction();
                string s = "";
                try
                {
                    for (int i = 0; i < SqlList.Count; i++)
                    {
                        DbCommand _command = GetDbProviderFactory().CreateCommand();
                        _command.Connection = con;
                        _command.CommandTimeout = 500;
                        _command.CommandText = SqlList[i].ToString();
                        s = SqlList[i].ToString();
                        for (int n = 0; n < paramlist[i].Length; n++)
                        {
                            DbParameter p = paramlist[i][n];
                            if (p.Value == null || p.Value.ToString() == "")
                            {
                                p.Value = DBNull.Value;
                            }
                            _command.Parameters.Add(p);
                        }
                        _command.Transaction = SqlTran;
                        count += _command.ExecuteNonQuery();
                        _command.Dispose();
                    }            }
                catch (Exception ex)
                {
                    iserror = true;
                    strerror = ex.Message;
                    count = 0;
                }
                finally
                {                if (iserror)
                    {
                        SqlTran.Rollback();
                        throw new Exception(strerror);
                    }
                    else
                    {
                        SqlTran.Commit();
                    }
                    con.Close();
                }
                return count;
            }