zoukankan      html  css  js  c++  java
  • linq 数据库操作封装

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using CRM.Model;
    using System.Configuration;
    using System.Linq.Expressions;
    using System.Data.SqlClient;
    using System.Data.Linq;

    namespace CRM.DAL
    {
        /// <summary>
        /// Linq数据访问层
        /// </summary>
        public class LinqHelper
        {
            /// <summary>
            /// 获得数据上下文
            /// </summary>
            /// <returns></returns>
            public CRMDBDataContext GetDataContext()
            {
               
                string conn = ConfigurationManager.ConnectionStrings["LinqConnection"].ConnectionString;
                //string conn = "server=.;uid=sa;pwd=123456;database=lf_crm_db2";
                CRMDBDataContext crmDB = new CRMDBDataContext(conn);
                crmDB.Connection.Open();
                return crmDB;
            }
            /// <summary>
            /// 获取所有的数据
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public List<T> GetList<T>() where T : class
            {
                var db = GetDataContext();
                if (!IsAtOnceLoad.IsLazyLoad)//为false时立即加载
                {
                    return db.GetTable<T>().ToList();
                }
                else
                {
                    db.DeferredLoadingEnabled = false;
                    return db.GetTable<T>().ToList();
                }
            }
            /// <summary>
            /// 获取指定的单个实体
            /// 如果不存在则返回null
            /// 如果存在多个则抛异常
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <param name="predicate">Lamda表达式</param>
            /// <returns>Entity</returns>
            public T GetEntity<T>(Expression<Func<T, bool>> predicate) where T : class
            {
                var db = GetDataContext();
                if (!IsAtOnceLoad.IsLazyLoad)
                {
                    return db.GetTable<T>().Where<T>(predicate).SingleOrDefault();
                }
                else
                {
                    db.DeferredLoadingEnabled = false;
                    return db.GetTable<T>().Where<T>(predicate).SingleOrDefault();
                }
            }
            /// <summary>
            /// 用SQL语句查询
            /// </summary>
            /// <typeparam name="T">实体类类型</typeparam>
            /// <param name="sql">sql语句</param>
            /// <param name="parameters">sql参数</param>
            /// <returns>集合</returns>
            public List<T> GetListBySql<T>(string sql, params SqlParameter[] parameters) where T : class
            {
                var db = GetDataContext();
                if (!IsAtOnceLoad.IsLazyLoad)
                {
                    return db.ExecuteQuery<T>(sql).ToList();
                }
                else
                {
                    db.DeferredLoadingEnabled = false;
                    return db.ExecuteQuery<T>(sql).ToList();
                }
            }

            /// <summary>
            /// 添加实体
            /// </summary>
            /// <typeparam name="T">实体类类型</typeparam>
            /// <param name="eneiey">实体对象</param>
            public void InsertEntity<T>(T eneiey) where T : class
            {
                using (CRMDBDataContext db = GetDataContext())
                {
                    db.GetTable<T>().InsertOnSubmit(eneiey);
                    db.SubmitChanges();
                }
            }
            /// <summary>
            /// 删除实体
            /// </summary>
            /// <typeparam name="T">实体类类型</typeparam>
            /// <param name="predicate">Lamda表达式</param>
            public void DeleteEntity<T>(Expression<Func<T, bool>> predicate) where T : class
            {
                using (CRMDBDataContext db = GetDataContext())
                {
                    T entity = db.GetTable<T>().Where(predicate).First();
                    db.GetTable<T>().DeleteOnSubmit(entity);
                    db.SubmitChanges();
                }
            }
            /// <summary>
            /// 批量删除
            /// </summary>
            /// <typeparam name="T">实体类类型</typeparam>
            /// <param name="list">实体集合</param>
            public void DeletesEntity<T>(List<T> list) where T : class
            {
                using (CRMDBDataContext db = GetDataContext())
                {
                    db.Transaction = db.Connection.BeginTransaction();
                    try
                    {
                        foreach (var item in list)
                        {
                            db.GetTable<T>().Attach(item);
                            db.Refresh(RefreshMode.KeepCurrentValues, item);
                        }
                        db.GetTable<T>().DeleteAllOnSubmit(list.AsEnumerable<T>());
                        db.SubmitChanges();
                        db.Transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        db.Transaction.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }

            /// <summary>
            /// 修改实体
            /// </summary>
            /// <typeparam name="T">实体类类型</typeparam>
            /// <param name="entity">实体对象</param>
            public void UpadateEntity<T>(T entity) where T : class
            {
                using (CRMDBDataContext db = GetDataContext())
                {
                    db.GetTable<T>().Attach(entity);
                    db.Refresh(RefreshMode.KeepCurrentValues, entity);
                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                }
            }
        }
    }

  • 相关阅读:
    多层结构中,事务的运用。
    A private conversation
    Sql Server 日志清理 (数据库压缩方法)
    Basic of Ajax
    Persin Buttons
    不知为什么无缘无故加到了一个“邯郸.net俱乐部”,想退出,找不到入口.....
    Wokflow designer not working when openning workflow in nonworkflow VS 2005 project
    GridView中如何取得隐藏列的值?
    Error: cannot obtain value
    Too late
  • 原文地址:https://www.cnblogs.com/swarb/p/9924384.html
Copyright © 2011-2022 走看看