你的1好像是查询吧,而2才是更新数据,
为什么不先更新数据库,再更新记录集呢?
数据库的transaction,对数据库操作(添加、删除和修改)才有作用,而普通的查询没什么意义吧。

解决方案 »

  1.   

    对不起,写错了。
    第一步应该是:da.update(ds.dt)
      

  2.   

    简单的如下:
    SqlTransaction myTrans;
    try
    {
          // Start a local transaction
          myTrans = sqlConn.BeginTransaction(IsolationLevel.ReadCommitted,
                 "yourTransaction");//sqlConn is a connection to DB      // Must assign both transaction object and connection
          // to Command object for a pending local transaction
          sqlComm.Transaction = myTrans;//Set transaction to your command      //Execute your sql here      myTrans.Commit();//Commit
    }
    catch
    {
          myTrans.Rollback();//Roll back
    }
      

  3.   

    用同一个transaction传给你的Da和SqlCommand,使它俩用同一个transaction。
      

  4.   

    我的代码如下:daMemo为数据适配器,myHT为哈希表,作用为记录主键和标志位。
    代码的目的:
    对网格中所有添加修改删除的记录在进行上述操作的时候同时在每条记录中的标志字段up_status置为相应的状态(1添加,2修改)。
    现在发生的问题:
    System.Data.DataTable changedRecords = this.dsMemo1.myMemo.GetChanges();
    if(changedRecords !=null && changedRecords.Rows.Count>0)
    {
    for(int i=0;i<changedRecords.Rows.Count;i++)
    {
    if(changedRecords.Rows[i].RowState.ToString()=="Added")
    {

    this.myHT.Add(changedRecords.Rows[i]["bz_id"].ToString(),1);

    }
    if(changedRecords.Rows[i].RowState.ToString()=="Deleted")
    {

    common.up_status("station",changedRecords.Rows[i]["bz_id",DataRowVersion.Original].ToString(),"3");


    }
    if(changedRecords.Rows[i].RowState.ToString()=="Modified")
    {

    this.myHT.Add(changedRecords.Rows[i]["bz_id"].ToString(),2);

    }
    }
    if(MessageBox.Show(String.Format("您大约准备更新 {0} 条记录.\n\r是否继续?",changedRecords.Rows.Count),"物流管理系统",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
    {
    try
    {
    this.conn.Open();
    myTrans=this.conn.BeginTransaction();
    this.daMemo.DeleteCommand.Transaction=myTrans;
    this.daMemo.InsertCommand.Transaction=myTrans;
    this.daMemo.UpdateCommand.Transaction=myTrans;
    this.daMemo.Update(this.dsMemo1.myMemo);
    //
    IDictionaryEnumerator myEnumerator = this.myHT.GetEnumerator();
    while ( myEnumerator.MoveNext() )
    {

    //下面这段代码在新添记录时有时执行有时不执行???? this.daMemo.UpdateCommand.CommandText="update mymemo set up_status="+myEnumerator.Value.ToString()+ " where bz_id="+myEnumerator.Key.ToString();

    this.daMemo.UpdateCommand.ExecuteNonQuery();

    }

    myTrans.Commit();
    this.conn.Close();


    }
    catch(Exception exc)
    {
    myTrans.Rollback();
    common.ShowError("保存数据发生错误!\n\r"+exc.Message );
    }

    }
    }
    else
    {
    common.ShowInfo("您的数据没有发生任何变化!");
    }