public void UpdateData(string productId, string campaignId, int staffTypeId, List <string> branchCodeList, string createdBy) { try { string del = ""; using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { if (!string.IsNullOrEmpty(productId)) { del = @"DELETE FROM " + SLMConstant.SLMDBName + ".dbo.kkslm_ms_access_right WHERE slm_Product_Id = '" + productId + "' AND slm_StaffTypeId = '" + staffTypeId.ToString() + "' "; } else if (!string.IsNullOrEmpty(campaignId)) { del = @"DELETE FROM " + SLMConstant.SLMDBName + ".dbo.kkslm_ms_access_right WHERE slm_CampaignId = '" + campaignId + "' AND slm_StaffTypeId = '" + staffTypeId.ToString() + "' "; } else { throw new Exception("Cannot find productId or campaignId"); } if (!string.IsNullOrEmpty(del)) { slmdb.ExecuteStoreCommand(del); } DateTime createdDate = DateTime.Now; foreach (string branchcode in branchCodeList) { kkslm_ms_access_right obj = new kkslm_ms_access_right() { slm_Product_Id = string.IsNullOrEmpty(productId) ? null : productId, slm_CampaignId = string.IsNullOrEmpty(campaignId) ? null : campaignId, slm_StaffTypeId = staffTypeId, slm_BranchCode = branchcode, slm_CreatedBy = createdBy, slm_CreatedDate = createdDate, slm_UpdatedBy = createdBy, slm_UpdatedDate = createdDate, is_Deleted = false }; slmdb.kkslm_ms_access_right.AddObject(obj); } slmdb.SaveChanges(); ts.Complete(); } } catch (Exception ex) { throw ex; } }
public void InsertData(string productId, string campaignId, int staffTypeId, List <string> branchCodeList, string createdBy) { try { DateTime createdDate = DateTime.Now; List <string> dbBranchCodeList = null; //หา branch ที่มีอยู่ในเบส if (!string.IsNullOrEmpty(productId)) { dbBranchCodeList = slmdb.kkslm_ms_access_right.Where(p => p.slm_Product_Id == productId && p.slm_StaffTypeId == staffTypeId && p.is_Deleted == false).Select(p => p.slm_BranchCode).ToList(); } else if (!string.IsNullOrEmpty(campaignId)) { dbBranchCodeList = slmdb.kkslm_ms_access_right.Where(p => p.slm_CampaignId == campaignId && p.slm_StaffTypeId == staffTypeId && p.is_Deleted == false).Select(p => p.slm_BranchCode).ToList(); } else { throw new Exception("Cannot find productId or campaignId"); } //เอา branch ที่มีอยู่ในเบสแล้ว ออกจาก list ที่ต้องการ insert if (dbBranchCodeList.Count > 0) { branchCodeList = branchCodeList.Except <string>(dbBranchCodeList).ToList(); } if (branchCodeList.Count > 0) { foreach (string branchcode in branchCodeList) { kkslm_ms_access_right obj = new kkslm_ms_access_right() { slm_Product_Id = string.IsNullOrEmpty(productId) ? null : productId, slm_CampaignId = string.IsNullOrEmpty(campaignId) ? null : campaignId, slm_StaffTypeId = staffTypeId, slm_BranchCode = branchcode, slm_CreatedBy = createdBy, slm_CreatedDate = createdDate, slm_UpdatedBy = createdBy, slm_UpdatedDate = createdDate, is_Deleted = false }; slmdb.kkslm_ms_access_right.AddObject(obj); } slmdb.SaveChanges(); } } catch (Exception ex) { throw ex; } }