基于Linq搭建三层的通用DAL类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using Models;

namespace DAL
{
    public class CommandDAL<T> where T:class
    {
        public DataProjectDataContext db;

        public CommandDAL()
        {
            //每个用户产生一个独立的TestClassDataContext对象
            //int count = HttpContext.Current.Session.Count;
            //if (HttpContext.Current.Session["Datas"] == null)
            //{
                db = new DataProjectDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
            //    HttpContext.Current.Session["Datas"] = db;
            //}
            //else
            //{
            //    db = HttpContext.Current.Session["Datas"] as JCPPSContext;
            //}
        }

        /// <summary>
        /// 向数据库插入一条数据
        /// </summary>
        /// <param name="obj">要插入的数据</param>
        /// <returns></returns>
        public T Insert(T obj)
        {
            try
            {
               
                db.GetTable<T>().InsertOnSubmit(obj);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
               
                throw new Exception(ex.Message);
            }
            return obj;
        }

        /// <summary>
        /// 向数据库插入多条数据
        /// </summary>
        /// <param name="TEntities">要插入的数据集合</param>
        public void InsertAll(IEnumerable<T> TEntities)
        {
            try`
            {
                db.GetTable<T>().InsertAllOnSubmit(TEntities);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
               
                throw new Exception(ex.Message);
            }
           
        }

        /// <summary>
        /// 根据查询出来的数据,修改一条数据
        /// </summary>
        /// <param name="obj">要修改的实体</param>
        /// <param name="action">把要修改的实体对象按表达式进行赋值</param>
        public void Update(T obj, Action<T> action)
        {
            try
            {
                action(obj);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// ????
        /// </summary>
        /// <param name="obj"></param>
        public void Update(T obj)
        {
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 查找出想要的数据
        /// </summary>
        /// <param name="s">查找条件表达式</param>
        /// <returns>返回一组实体类</returns>
        public List<T> Select(System.Linq.Expressions.Expression<Func<T, bool>> expression)
        {
            try
            {
                return db.GetTable<T>().Where<T>(expression).ToList();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        ///// <summary>
        ///// 查找出想要的数据
        ///// </summary>
        ///// <param name="s">查找条件表达式</param>
        ///// <returns>返回一组实体类</returns>
        //public List<T> Select(System.Linq.Expressions.Expression<Func<T, bool>> expression, System.Linq.Expressions.Expression<Func<T, bool>> expression1,bool bol)
        //{
        //    try
        //    {
        //        return db.GetTable<T>().Where<T>(expression).OrderBy(expression1).ToList() ;
        //    }
        //    catch (Exception ex)
        //    {

        //        throw new Exception(ex.Message);
        //    }
        //}

        /// <summary>
        /// 查出想要的数据,进行分页查找
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <param name="pageindex">当前页,第一页从0开始</param>
        /// <param name="pagesize">每页显示的数据</param>
        /// <param name="count">总记录数</param>
        /// <returns></returns>
        public List<T> Select(System.Linq.Expressions.Expression<Func<T, bool>> expression, int pageindex, int pagesize, ref int count)
        {
            try
            {
                count = db.GetTable<T>().Where<T>(expression).ToList().Count();
                return db.GetTable<T>().Where<T>(expression).Skip(pageindex * pagesize).Take(pagesize).ToList();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 查出所有的数据
        /// </summary>
        /// <param name="s">要查找的数据类型</param>
        /// <returns>返回一组实体类</returns>
        public List<T> SelectAll(int pageindex, int pagesize, ref int count)
        {
            try
            {
                count = db.GetTable<T>().ToList().Count();
                return db.GetTable<T>().Skip(pageindex * pagesize).Take(pagesize).ToList();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 查出所有的数据
        /// </summary>
        /// <param name="s">要查找的数据类型</param>
        /// <returns>返回一组实体类</returns>
        public List<T> SelectAll()
        {
            try
            {
                return db.GetTable<T>().ToList();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 根据条件查找出一条数据
        /// </summary>
        /// <param name="s">查找条件表达式</param>
        /// <returns>返回一个实体类</returns>
        public T SelectSingle(System.Linq.Expressions.Expression<Func<T, bool>> expression)
        {
            try
            {
                return db.GetTable<T>().Where<T>(expression) .FirstOrDefault();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 向数据库删除一条数据
        /// </summary>
        /// <param name="obj">要删除的数据</param>
        public void Delete(T obj)
        {
            try
            {
                db.GetTable<T>().DeleteOnSubmit(obj);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除符合条件的数据
        /// </summary>
        /// <param name="expression">删除条件</param>
        public void Delete(System.Linq.Expressions.Expression<Func<T, bool>> expression)
        {
            try
            {
                db.GetTable<T>().DeleteAllOnSubmit<T>(Select(expression));
                db.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 根据SQL语句进行批量删除或修改,这种批量删除(更新)效率更高
        /// </summary>
        /// <param name="Sql">要删除(更新)的SQL语句</param>
        public void DeleteOrUpdate(String Sql)
        {
            try
            {
                db.ExecuteCommand(Sql);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
    }
}

posted @ 2011-01-05 11:48 Sir。 阅读(...) 评论(...) 编辑 收藏