首页 > 代码库 > linq to sql 三层架构中使用CRUD操作
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 = http://www.mamicode.com/gasBottles.ConvertToEntity(); 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 = http://www.mamicode.com/gasBottles.ConvertToEntity (); 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 }
linq to sql 三层架构中使用CRUD操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。