//BLL 类
     public void Update(HIS.Model.zy_actpatientModels model)
        {
            CommittableTransaction tx = new CommittableTransaction();
            try
            {
                dal.Update(model, tx);
                tx.Commit();            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine();
                tx.Rollback();
            }
        }
//dll 类
public void Update(HIS.Model.zy_actpatientModels model, Transaction TX)
{
         StringBuilder strSql=new StringBuilder();
strSql.Append("update zy_actpatient set ");
strSql.Append("ward='"+model.ward+"',");
strSql.Append("dept='"+model.dept+"',");
strSql.Append("bed_no='"+model.bed_no+"'");
strSql.Append(" where patient_id='"+ model.patient_id+"' and admiss_times="+ model.admiss_times+" ");         Database db = DatabaseFactory.CreateDatabase();
            SqlConnection conn = new SqlConnection(db.ConnectionString);
            conn.Open();
            try
            {
                if (TX != null)
                conn.EnlistTransaction(TX);                SqlCommand sqlcmd = conn.CreateCommand();
                sqlcmd.CommandText = strSql.ToString();
                sqlcmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();      
            }}
请教高手,伤处代码什么原因导致数据更新失败。

解决方案 »

  1.   

    CommittableTransaction tx = new CommittableTransaction();          
    using (SqlConnection conn1 = new SqlConnection(connStr1))            
    {  
    conn1.Open();               
    conn1.EnlistTransaction(tx);                
    SqlCommand cmd = new SqlCommand(strSql1, conn1);               
    cmd.ExecuteNonQuery();            
    }          
    tx.Commit();
    //tx.BeginCommit(OnCommited, tx);
    或using (TransactionScope txScope = new TransactionScope())           
    {}