简单的如下: 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 }
//下面这段代码在新添记录时有时执行有时不执行???? this.daMemo.UpdateCommand.CommandText="update mymemo set up_status="+myEnumerator.Value.ToString()+ " where bz_id="+myEnumerator.Key.ToString();
第一步应该是:da.update(ds.dt)
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
}
代码的目的:
对网格中所有添加修改删除的记录在进行上述操作的时候同时在每条记录中的标志字段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("您的数据没有发生任何变化!");
}