ADO.NET事務的實現

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

 public bool Delete(int id)
 {
  bool flag=false;
       using (SqlConnection conn = new SqlConnection("連接字符串"))
            {
                conn.Open();//<span style="color:#FF6666;">打開數據庫(重要)</span>
                SqlTransaction tran = conn.BeginTransaction();             
                SqlCommand cmd = new SqlCommand();

                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("    delete from PM_Product_Property where PSUKID=" + skuID);
                sbSql.Append("    delete from  PM_ProductSKU where ID=" + skuID);
       <pre name="code" class="csharp">                cmd.Connection = conn;
                cmd.Transaction = tran;         
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sbSql.ToString();

                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                    tran.Commit();//<span style="color:#FF6666;">成功提交</span>
                }
                else
                {
                    flag = false;
                    tran.Rollback();//<span style="color:#FF6666;">失敗回滾</span>
                }
                return flag;
         }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章