日期:2014-05-18  浏览次数:20517 次

怎样让SqlDataAdapter.Update 支持事务
利用SqlDataAdapter.Update方法将DATASET中的数据更新到数据库,功能已经实现了。但是缺少事务,不能保证数据的完整性。以下是源代码,请问怎么添加事务功能。
C# code

DataTable t = (DataTable)Session["product"];
 using (SqlConnection con = Ares.WebSite.App_Code.DB.CreateConn())
            {
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter("select top 1 * from ProductTab", con);
                  DataSet ds = new DataSet();
                    sda.Fill(ds, "prouct");
                    SqlCommandBuilder builder = new SqlCommandBuilder(sda);
                    for (int i = 0; i < t.Rows.Count; i++)
                    {
                        DataRow r = t.NewRow();
                        ds.Tables["car"].Rows.Add(null, null, t.Rows[i]["GUID"], Guid.NewGuid(), t.Rows[i]["SalesPrice"], t.Rows[i]["Amount"], true, "jsjd");

                    }
                    sda.Update(ds, "product");
                    
                    


------解决方案--------------------
C# code

public void UpdateDataSet(DataSet ds, string tableName, SqlCommand insertCommand, SqlCommand updateCommand, SqlCommand deleteCommand) {
            using (SqlConnection conn = this.CretaeSqlConnection()) {
                conn.Open();
                using (SqlTransaction transaction = conn.BeginTransaction()) {
                    if(insertCommand == null && updateCommand == null && deleteCommand ==null){
                        throw new Exception("insertCommand updateCommand deleteCommand 不能全部為空");
                    }
                    using (SqlDataAdapter dapter = new SqlDataAdapter()) {
                        if (insertCommand != null) {
                            insertCommand.Connection = conn;
                            insertCommand.Transaction = transaction;
                            dapter.InsertCommand = insertCommand;
                        }
                        if (updateCommand != null) {
                            updateCommand.Connection = conn;
                            updateCommand.Transaction = transaction;
                            dapter.UpdateCommand = updateCommand;
                        }
                        if (deleteCommand != null) {
                            deleteCommand.Connection = conn;
                            deleteCommand.Transaction = transaction;
                            dapter.DeleteCommand = deleteCommand;
                        }
                        try {                         
                            dapter.Update(ds, tableName);
                            transaction.Commit();
                        }
                        catch (SqlException ex) {
                            transaction.Rollback();
                            throw ex;
                        }
                    }
                }
            }
        }

------解决方案--------------------
C# code
//执行事务处理
public void DoTran()
{  //建立连接并打开
 SqlConnection myConn=GetConn();
 myConn.Open();
 SqlCommand myComm=new SqlCommand();
 //SqlTransaction myTran=new SqlTransaction();
 //注意,SqlTransaction类无公开的构造函数
 SqlTransaction myTran;
 //创建一个事务
 myTran=myConn.BeginTransaction();
 try
 {
  //从此开始,基于该连接的数据操作都被认为是事务的一部分
  //下面绑定连接和事务对象
  myComm.Connection=myConn;
  myComm.Transaction=myTran; //定位到pubs数据库
  myComm.CommandText="USE pubs";
  myComm.ExecuteNonQuery();//操作1
  myComm.CommandText=""; //操作2
  myComm.ExecuteNonQuery();
   //提交事务
  myTran.Commit();
 }
 catch(Exception err)
 {
  myTran.rollback();
  throw new ApplicationException("事务操作出错,系统信息:"+err.Message);
  }
 finally
 {
  myConn.Close();
  }
}