SqlTransaction 使用了Commit()后就是不能在使用了,必须重新BeginTransaction()一个。

解决方案 »

  1.   

    你的SqlTransaction 已经Commit或者RollBack之后,你又使用SqlTransaction所在的Connection去做Select、Insert、 Delete,就会出现这个错误。
      

  2.   

    不是呀,Commit之后,断开了Connection,也重新BeginTransaction()
                                     open();
    string strSql=" ...";

    SqlTransaction myTransaction=sqlConnection.BeginTransaction(); try
    {
    SqlCommand myCommand=sqlConnection.CreateCommand();
    myCommand.Connection=sqlConnection;
    myCommand.Transaction=myTransaction;
    myCommand.CommandText=strSql; SqlDataAdapter myAdapter=new SqlDataAdapter();
    myAdapter.SelectCommand=myCommand;
    SqlCommandBuilder myBuilder=new SqlCommandBuilder(myAdapter);
    myAdapter.FillSchema(TrafficDT,SchemaType.Mapped);
    ....
    Count=myAdapter.Update(TrafficDT);
    myTransaction.Commit();
    }
    catch(Exception ex)
    {

    throw ex;
    }
    finally
    {
    close();
    }
      

  3.   

    Commit之后,没有RollBack;try{
        ...
        trans.Commit();
    }
    catch(Exception err){
        trans.RollBack();
        throw err; 
        
    }//没有RollBack ,如何确定你的trans有效呢?
      

  4.   

    把open和BeginTransaction放到try里头去,并在catch里头添加回滚处理(如果事务开启)。
    上面的方法或许对你的问题没帮助,但应该这么做。
      

  5.   

    试试
    try
    {
        SqlTransaction Trs;
        Trs = sqlConnection.BeginTransaction();
        try
        {
           DataAdapter1.Update(ds,"dt");
           Trs.Commit();
       }
       catch(System.Exception e)
       {
         if(Trs != null)
         {
            Trs.Rollback();
         }
         throw new Exception(e.Message);
       }
       finally
       {
         if(Trs != null)
          {
    Trs.Dispose();
          }
        }
    }
    catch(System.Exception e)
    {
       throw new Exception(e.Message);
    }
      

  6.   

    myTransaction.Rollback();
    是写在catch里边了,是我刚才没贴全!
      

  7.   

    试试不写Open(),不知会不会自动Open
      

  8.   

    //你的代码在有异常的情况下出现问题.Count=myAdapter.Update(TrafficDT);
    myTransaction.Commit();
    }
    catch(Exception ex)
    {
      myTransaction.Rollback();//
    throw ex;
    }