/// <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.INV_StockLocateDBO dbo = new INV_StockLocateDBO(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 } }
//批次刪除 public string BathDelete(ArrayList ParameterList, DataView dv) { string strResult = string.Empty; ArrayList Para = new ArrayList(); string strUPDATEDATE = string.Empty; string strUPDATEUID = string.Empty; string strError1 = string.Empty; //資料不存在 string strError2 = string.Empty; //有庫存 try { DBO.INV_StockLocateDBO dbo = new INV_StockLocateDBO(ref USEDB); Conn = USEDB.CreateConnection(); Conn.Open(); DBT = Conn.BeginTransaction(); int iSeqNo = 0; int iDelRows = 0; for (int i = 0; i < dv.Count; i++) { iSeqNo = i + 1; if (dv[i]["chkSel"].ToString() == "1") { iDelRows++; Para.Clear(); Para.Add(dv[i]["ID"].ToString()); DataTable dtCheck = QueryStockLocateByID(Para); if (dtCheck.Rows.Count == 0) { strError1 += "項次 " + iSeqNo.ToString() + " 儲位 " + dv[i]["LOCATE_SECTION"].ToString() + "<BR />"; } else { if (dtCheck.Rows[0]["STOCK"].ToString() == "1") { strError2 += "項次 " + iSeqNo.ToString() + " 儲位 " + dv[i]["LOCATE_SECTION"].ToString() + "<BR />"; } } } } if (iDelRows == 0) { strResult = "尚未選取刪除項目"; } else if ((strError1 != "") || (strError2 != "")) { if (strError1 != "") strError1 = "以下儲位已不存在,批次刪除停止<BR />" + strError1; if (strError2 != "") strError2 = "以下儲位有庫存,批次刪除停止<BR />" + strError2; strResult = strError1 + strError2; } else { for (int i = 0; i < dv.Count; i++) { if (dv[i]["chkSel"].ToString() == "1") { Para.Clear(); Para.Add(dv[i]["LOCATE_SECTION"].ToString()); Para.Add(ParameterList[0]); Para.Add(ParameterList[1]); dbo.doDelete(Para, DBT); } } DBT.Commit(); strResult = "刪除成功"; } } catch (Exception ex) { if (DBT != null) { DBT.Rollback(); } throw ex; } finally { if (Conn.State == ConnectionState.Connecting) { Conn.Close(); } } return strResult; }