linq to sql 三层架构中使用CRUD操作
/// <summary> /// 数据层 /// </summary> public partial class GasBottles : IGasBottles { #region IGasBottles 成员 public Model.GasBottles GetModel(int gasBottlesID) { var db = DbContext.LGSCMSDataContext; try { var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID); if (gs != null) { Model.GasBottles gasBottlesInfo = gs.ConvertToEntity<Model.GasBottles>(); return gasBottlesInfo; } } catch (Exception ex) { throw ex; } return null; } public bool Add(Model.GasBottles gasBottles) { bool flag = false; try { var db = DbContext.LGSCMSDataContext; DataLinqEntity.GasBottles gs = gasBottles.ConvertToEntity<DataLinqEntity.GasBottles>(); db.GasBottles.InsertOnSubmit(gs); db.SubmitChanges(); flag = true; } catch (Exception ex) { flag = false; throw ex; } return flag; } public bool Update(Model.GasBottles gasBottles) { bool flag = false; var changedData = gasBottles.ConvertToEntity<DataLinqEntity.GasBottles>(); var db = DbContext.LGSCMSDataContext; try { var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID); //CopyProperties(ref updateTarget, changedData); changedData.GetType().GetProperties() .Where(p => p.GetCustomAttributes(typeof(ColumnAttribute), false).Any()).ToList() .ForEach(p => p.SetValue(updateTarget, p.GetValue(changedData, null), null)); db.SubmitChanges(); flag = true; } catch (Exception ex) { flag = false; throw ex; } return flag; } public bool Delete(int gasBottlesID) { bool flag = false; var db = DbContext.LGSCMSDataContext; try { var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID); if (gs != null) { gs.deleteflag = 1; db.SubmitChanges(); } } catch (Exception ex) { flag = false; throw ex; } return flag; } /// <summary> /// 新增或更新 /// </summary> /// <param name="gasBottles"></param> /// <param name="gasBottlesID"></param> /// <returns></returns> public bool Save(Model.GasBottles gasBottles, out int gasBottlesID) { bool flag = false; var changedData = gasBottles.ConvertToEntity<DataLinqEntity.GasBottles>(); var db = DbContext.LGSCMSDataContext; try { //=>新增 var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID); if (updateTarget == null) { db.GasBottles.InsertOnSubmit(changedData); db.SubmitChanges(); gasBottlesID = changedData.ID.ToInt(); flag = true; } //=>修改 else { //CopyProperties(ref updateTarget, changedData); changedData.GetType().GetProperties() .Where(p => p.GetCustomAttributes(typeof(ColumnAttribute), false).Any()).ToList() .ForEach(p => p.SetValue(updateTarget, p.GetValue(changedData, null), null)); db.SubmitChanges(); gasBottlesID = updateTarget.ID.ToInt(); flag = true; } } catch (Exception ex) { flag = false; throw ex; } return flag; } #endregion }
private void CopyProperties<T>(ref T Target, T Source) { foreach (PropertyInfo PI in Target.GetType().GetProperties()) { if (PI.CanWrite && PI.CanRead) { PI.SetValue(Target, PI.GetValue(Source, null), null); } } } #endregion
DbContext
public class DbContext { /// <summary> /// /// </summary> private readonly static string connectionString = SqlHelper.SQLConnString; #region [=>Winfrom方式] //private static WLMQGasBottlesDataContext _WLMQGasBottlesDataContext; ///// <summary> ///// ///// </summary> //public static WLMQGasBottlesDataContext WLMQGasBottlesDataContext //{ // get // { // return _WLMQGasBottlesDataContext ?? new WLMQGasBottlesDataContext(connectionString); // } //} #endregion #region [=>Web方式] public static WLMQGasBottlesDataContext WLMQGasBottlesDataContext { get { WLMQGasBottlesDataContext context = HttpContext.Current.Items["WLMQGasBottlesDataContext"] as WLMQGasBottlesDataContext; if (context == null) { context = new WLMQGasBottlesDataContext(connectionString); HttpContext.Current.Items["WLMQGasBottlesDataContext"] = context; } return context; } } public static LGSCMSDataContext LGSCMSDataContext { get { LGSCMSDataContext context = HttpContext.Current.Items["LGSCMSDataContext"] as LGSCMSDataContext; if (context == null) { context = new LGSCMSDataContext(connectionString); HttpContext.Current.Items["LGSCMSDataContext"] = context; } return context; } } #endregion }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。