zoukankan      html  css  js  c++  java
  • Linq To Sql数据库操作基类实现不同条件的增删改查

    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);
                }
            }
        }
    }
  • 相关阅读:
    ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径
    【翻译】.NET 5 Preview2发布
    ASP.NET Core AutoWrapper 自定义响应输出
    TypeScript数据类型
    classpath和classpath*区别
    关联查询报错org.apache.ibatis.builder.BuilderException: Ambiguous collection type for property 'episodeList'. You must specify 'javaType' or 'resultMap'.
    IDEA无法创建Spring Initializr项目
    npm ERR! xd_demo@0.1.0 serve: `vue-cli-service serve`
    打开cmd的几种不常用方式
    idea导入maven项目后有红线
  • 原文地址:https://www.cnblogs.com/zcm123/p/2913912.html
Copyright © 2011-2022 走看看