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
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏跟着阿笨一起玩NET

C#中汉字排序简单示例(拼音/笔划)

2901
来自专栏技术之路

动态生成TreeView方法(一)

一般情况下生成TreeView我们用的是用递归不建议用递归数据量大了会慢, 小弟今天用SortedList集合实现一下没有技术含量,一看代码大家就会明白。个人也...

1886
来自专栏菩提树下的杨过

泛型List<T>使用示例

代码来源于"c#高级编程(第4版)",略作修改 using System; using System.Collections; using System.C...

1947
来自专栏C# 编程

线程、委托、lambda运算符的简单示例

using System; using System.Collections.Generic; using System.Linq; using System...

2210
来自专栏hbbliyong

.Net下SQLite的DBHelp

怎样获取SqLite请参考初识SqlLite ---.net连接数据库,怎样在SQLite使用Linq请参考在C#中利用Nuget包使用SQLite数据库和Li...

3244
来自专栏王磊的博客

HttpWebRequest采集读取网站挂载Cookie的通用方法

Asp.net 版本 HttpWebRequest采集时添加:httpWebRequest.CookieContainer = new CookieContai...

3095
来自专栏lgp20151222

JSONObject和JSONArray区别及基本用法

        例如:   { "id" : "123", "courseID" : "huangt-test", "title" : "提交作业", "con...

931
来自专栏跟着阿笨一起玩NET

c# 播放mp3

转载:http://www.cnblogs.com/igrl/archive/2010/03/29/1699975.html

8751
来自专栏hbbliyong

LINQ分页和排序,skip和Take 用法

LINQ分页和排序,skip和Take 用法 dbconn.BidRecord.OrderBy(p=>p.bid_id).ToList<BidRecord>()...

3919
来自专栏c#开发者

asp.net回调javascript

using System; using System.Collections; using System.IO; using System.Text; ...

3305

扫码关注云+社区

领取腾讯云代金券