public bool UpdatePurchaseCard(DataTable PurchaseCard_Main, DataTable PurchaseCard_Sale_Area, DataTable PurchaseCard_Chain, DataTable PurchaseCard_Nondiss_St) { int PID = 0; using (DbConnection connection = USEDB.CreateConnection()) { connection.Open(); DbTransaction transaction = connection.BeginTransaction(); try { if (PurchaseCard_Main.Rows.Count == 0) { throw new Exception("沒有採購卡資料"); } PID = Convert.ToInt32(PurchaseCard_Main.Rows[0]["ID"].ToString()); //Update PurchaseCard_Main DBO.MKT_PurchaseCardDBO dbo = new MKT_PurchaseCardDBO(ref USEDB); dbo.doUpdate(PurchaseCard_Main.Rows[0], ref transaction); //Insert Sale_Area DBO.MKT_PurchaseCardSaleAreaDBO dboArea = new MKT_PurchaseCardSaleAreaDBO(ref USEDB); dboArea.doDeleteByPID(PID.ToString(), ref transaction); foreach (DataRow dr in PurchaseCard_Sale_Area.Rows) { dr["PID"] = PID; dboArea.doCreate(dr, ref transaction); } //Insert Chain DBO.MKT_PurchaseCardChainDBO dboChain = new MKT_PurchaseCardChainDBO(ref USEDB); dboChain.doDeleteByPID(PID.ToString(), ref transaction); foreach (DataRow dr in PurchaseCard_Chain.Rows) { dr["PID"] = PID; dboChain.doCreate(dr, ref transaction); } //Insert Nondiss_st DBO.MKT_PurchaseCardNondissStDBO dboNondiss = new MKT_PurchaseCardNondissStDBO(ref USEDB); dboNondiss.doDeleteByPID(PID.ToString(), ref transaction); foreach (DataRow dr in PurchaseCard_Nondiss_St.Rows) { dr["PID"] = PID; dboNondiss.doCreate(dr, ref transaction); } // Commit the transaction transaction.Commit(); return true; } catch { // Rollback transaction transaction.Rollback(); return false; } connection.Close(); } }
/// <summary> /// 測試SP交易失敗 /// </summary> /// <param name="ParameterList">變數</param> /// <param name="RootDBT">是否有主交易,無主交易輸入null</param> public void TestTranscation(ArrayList ParameterList1, ArrayList ParameterList2, ArrayList ParameterList3, DbTransaction RootDBT ) { bool IsRootTranscation = false; try { DBO.MKT_PurchaseCardChainDBO dbo = new MKT_PurchaseCardChainDBO(ref USEDB); //判斷是否有傳入Root Transcation IsRootTranscation = (RootDBT == null) ? true : false; #region 啟動交易或指定RootTranscation if (IsRootTranscation) { //獨立呼叫啟動Transcation Conn = USEDB.CreateConnection(); Conn.Open(); DBT = Conn.BeginTransaction(); } else { DBT = RootDBT; } #endregion dbo.doCreate(ParameterList1, DBT); dbo.doUpdate(ParameterList2, DBT); dbo.doDelete(ParameterList3, DBT); throw new Exception("交易失敗"); #region 交易成功 if (IsRootTranscation) { //獨立呼叫Transcation成立 DBT.Commit(); } #endregion } catch (Exception ex) { #region 交易失敗 if (IsRootTranscation) { //獨立呼叫Transcation失敗 DBT.Rollback(); } #endregion throw ex; } finally { #region 判斷是否關閉交易連線 if (IsRootTranscation) { //獨立呼叫Transcation,關閉連線 if (Conn.State == ConnectionState.Connecting) { Conn.Close(); } } #endregion } }