datagridview 有两行数据,一行正确,一行错误,用SqlDataAdapter.update(table) 插入数据,
一行插入,一行没有插入,有什么方法让两行数据都不能插入,或者插入的一行回滚?
我已经用了事物如下:
      SqlTransaction tx = con.BeginTransaction();
      apdate.UpdateCommand = new SqlCommand();
      apdate.UpdateCommand.Transaction = tx;
        try
        {                                              
                            
          apdate.Update(tabl);         
        }
         catch
              {
                MessageBox.Show("运行错误");
                tx.Rollback();
                 return;
                }
但是没有用仍然一行插入,一行没有插入

解决方案 »

  1.   


    datagridview 的两行数据
    在同一个事务里面
      

  2.   

    datagridview 的两行数据 (一行正确,一行错误)
    一次性插入
    在同一个事务里面
      

  3.   

    填充数据:
      string sqlstr = "select       xsid,spid,spjm,spmc,sptm,spgg,xssl,ddw,xxssl,xdw,xsdj,xsje,xsrq,";
                sqlstr += "ckid,ckmc,xsph,hsdw,ps,bz,khid,khmc,gsid,czyid,xshs,che,xsdh from spxs where xsph='" + txtph.Text + "'";
                cmd = new SqlCommand(sqlstr, new SqlConnection(Coon.getcoon()));
                apdate = new SqlDataAdapter(cmd);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(apdate);
                apdate.Fill(tabl);
                datagridview.DataSource = tabl;
    插入数据:       SqlTransaction tx = con.BeginTransaction(); 
          apdate.UpdateCommand = new SqlCommand(); 
          apdate.UpdateCommand.Transaction = tx; 
            try 
            {                                              
                                
              apdate.Update(tabl);        
            } 
            catch 
                  { 
                    MessageBox.Show("运行错误"); 
                    tx.Rollback(); 
                    return; 
                    } 
      

  4.   

    在同一个事务里面,你的代码应该类似这样:SqlTransaction tx = con.BeginTransaction();
          apdate.UpdateCommand = new SqlCommand();
          apdate.UpdateCommand.Transaction = tx;
            try
            {                                             
              //正确数据操作的SQL命令
              apdate.Update(tabl);   
              //错误数据操作的SQL命令  
               apdate.Update(tabl);  
               tx.Commit();  
            }
            catch
                  {
                    MessageBox.Show("运行错误");
                    tx.Rollback();
                    return;
                    } 
      

  5.   

          apdate.UpdateCommand = new SqlCommand(); 
          apdate.UpdateCommand.Transaction = tx; 这句话是没有用的,只写apdate.UpdateCommand.Transaction = tx;
    apdate.UpdateCommand是会自动生成的!
      

  6.   


    apdate.UpdateCommand = new SqlCommand(); 
    未将对象引用设置到对象的实例。
      

  7.   

    填充数据: 
      string sqlstr = "select      xsid,spid,spjm,spmc,sptm,spgg,xssl,ddw,xxssl,xdw,xsdj,xsje,xsrq,"; 
                sqlstr += "ckid,ckmc,xsph,hsdw,ps,bz,khid,khmc,gsid,czyid,xshs,che,xsdh from spxs where xsph='" + txtph.Text + "'"; 
                cmd = new SqlCommand(sqlstr, new SqlConnection(Coon.getcoon())); 
                apdate = new SqlDataAdapter(cmd); 
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(apdate); 
                apdate.Fill(tabl); 
                datagridview.DataSource = tabl; 
    插入数据:       SqlTransaction tx = con.BeginTransaction(); 
          apdate.UpdateCommand = new SqlCommand(); 
          apdate.UpdateCommand.Transaction = tx; 
            try 
            {                                             
                                
              apdate.Update(tabl);  
              tx.Commit();     
            } 
            catch 
                  { 
                    MessageBox.Show("运行错误"); 
                    tx.Rollback(); 
                    return; 
                    } 
      

  8.   

    apdate.UpdateCommand应该是真正的更新语句,而不是InsertCommand,导致事务不成功吧,只是猜想,楼主尝试一下。
      

  9.   

    或者使用隐式事务
    using(TransactionScope tran = new TransactionScope())
    {
        try
        {    }
        catch
        {
           //此处不用写捕获异常后的回滚,隐性自动回滚
        }
    }
      

  10.   

    apdate.UpdateCommand = new SqlCommand(); 
    光看你这句就有问题,怎么创建了一个没有任何SQL语句的更新命令?而且还没有设置数据库连接,这能正常执行才怪。
      

  11.   

    TransactionScope 缺少程序集的引用,要添加什么吗?
      

  12.   

    需要添加System.Transactions的程序集
    using(TransactionScope tran = new TransactionScope()) 

        try 
        { 
           ..........
           tran.Complete();//放在操作段结尾指示事务已完成,如果发生异常,这个不会被调用并且自动回滚
        } 
        catch 
        { 
          //此处不用写捕获异常后的回滚,隐性自动回滚 
        } 
    }