1、DAL层
using Night.Models; using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace NightCat.DAL { public class SysLogService { //public SysLogService() //{ // //删除使用 // DelByCondition(p => p.F_Id == "00003"); // //修改使用 // Sys_Log sl = new Sys_Log() // { // F_Id = "00003", // F_Account = "adf", // F_CreatorTime = DateTime.Now // }; // this.Modify(sl, "F_Account", "F_CreatorTime");//或 // this.Modify(sl, new string[] { "F_Account", "F_CreatorTime" }); // //排序 // List<Sys_Log> ssl = this.GetListByCondition(p => p.F_Id == "00003" && p.F_Account == "1000"); // List<Sys_Log> ssl2 = this.GetListByCondition(p => p.F_Id == "00003" && p.F_Account == "1000", p2 => p2.F_Id); //} /// <summary> /// EF数据上下文 /// </summary> NightCatEntities nc = new NightCatEntities(); #region 新增实体 /// <summary> /// 新增实体 /// </summary> /// <param name="log"></param> /// <returns></returns> public int Add(Sys_Log log) { try { nc.Sys_Log.Add(log); //保存成功之后,会将自增的ID和新增的行数设置给log return nc.SaveChanges(); } catch (Exception ex) { throw ex; } } #endregion #region 根据ID删除一条数据 /// <summary> /// 根据ID删除一条数据 /// </summary> /// <param name="Id"></param> /// <returns></returns> public int DelById(string Id) { try { //创建对象 Sys_Log sl = new Sys_Log() { F_Id = Id }; //把要删除的对象添加到集合中 nc.Sys_Log.Attach(sl); nc.Sys_Log.Remove(sl); return nc.SaveChanges(); } catch (Exception ex) { throw ex; } } #endregion #region 根据条件删除数据 /// <summary> /// 根据条件删除数据 /// </summary> /// <param name="delWhere">返回值为bool类型的委托对象(方法)</param> /// <returns></returns> public int DelByCondition(Expression<Func<Sys_Log, bool>> delWhere) { try { //查询 List<Sys_Log> pDels = nc.Sys_Log.Where(delWhere).ToList(); pDels.ForEach(p => nc.Sys_Log.Remove(p)); return nc.SaveChanges(); } catch (Exception ex) { throw ex; } } #endregion #region 修改+Modify(Sys_Log p, params string[] proNames) /// <summary> /// 修改数据 /// </summary> /// <param name="p">要修改的对象</param> /// <param name="proName">要修改的属性名称集合</param> /// params 可变参数 /// <returns></returns> public int Modify(Sys_Log p, params string[] proNames) { try { DbEntityEntry entry = nc.Entry<Sys_Log>(p); entry.State = System.Data.Entity.EntityState.Unchanged; foreach (string proName in proNames) { entry.Property(proName).IsModified = true; } return nc.SaveChanges(); } catch (Exception) { throw; } } #endregion #region 根据条件检索集合中的数据 List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda) /// <summary> /// 根据条件检索集合中的数据 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda) { try { return nc.Sys_Log.Where(whereLambda).ToList(); } catch (Exception ex) { throw ex; } } #endregion #region + 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba) /// <summary> /// 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> /// 这么的Tkey不是必要的使用的时候可以不写 public List<Sys_Log> GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba) { try { return nc.Sys_Log.Where(whereLambda).OrderBy(orderLamba).ToList(); } catch (Exception ex) { throw ex; } } #endregion #region 根据条件和排序查询一分页的形式展示 /// <summary> /// 根据条件和排序查询一分页的形式展示 /// </summary> /// <typeparam name="Tkey">排序字段类型</typeparam> /// <param name="pageIndex">查询的页面</param> /// <param name="pageSize">每页显示的条数</param> /// <param name="orderLambda">排序的字段</param> /// <param name="whereLambda">查询的条件</param> /// <returns></returns> public List<Sys_Log> GetPageList<Tkey>(int pageIndex, int pageSize, Expression<Func<Sys_Log, Tkey>> orderLambda, Expression<Func<Sys_Log, bool>> whereLambda) { try { return nc.Sys_Log.Where(whereLambda).OrderBy(orderLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } catch (Exception ex) { throw ex; } } #endregion #region 批量修改+Modify(Sys_Log p, params string[] proNames) /// <summary> /// 批量修改数据 /// </summary> /// <param name="model">要修改的列及修改后列的值的集合</param> /// params 可变参数 /// <returns></returns> public int Modifys(Sys_Log model,Expression<Func<Sys_Log,bool>> whereLambda,params string[] ModifyProNames) { try { //1、查询出要修改的数据 List<Sys_Log> listModif = nc.Sys_Log.Where(whereLambda).ToList(); //2、获取实体类对象 Type t = typeof(Sys_Log); //3、获取实体类所有的共有属性 List<Sys_Log> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); //4、创建一个实体属性字典集合 Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>(); //5、将实体属性中要修改的属性,添加到字典集合中,键:属性名,值:属性对象 proInfos.ForEach(p => { if (ModifyProNames.Contains(p.F_NickName)) { dictPros.Add(p.F_NickName, p); } }); //循环要修改的属性名 foreach (string proName in ModifyProNames) { //判断要修改的属性名是否在实体类的属性集合中 if (dictPros.ContainsKey(proName)) { //如何存在,则取出要修改的属性对象 PropertyInfo proInfo = dictPros[proName]; //取出要修改的值 object newValue = proInfo.GetValue(model, null); //批量设置要修改的对象的属性 foreach (Sys_Log item in listModif) { //为要修改的对象的要修改的属性,设置新的值 proInfo.SetValue(item, newValue,null); } } } } catch (Exception) { throw; } } #endregion } }
2、BLL层
using Night.Models; using NightCat.DAL; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using NightCat.DAL; namespace NightCat.BLL { public class SysLogManager { SysLogService ss = new SysLogService(); #region 新增实体 /// <summary> /// 新增实体 /// </summary> /// <param name="log"></param> /// <returns></returns> public int Add(Sys_Log log) { try { return ss.Add(log); } catch (Exception ex) { throw ex; } } #endregion #region 根据ID删除一条数据 /// <summary> /// 根据ID删除一条数据 /// </summary> /// <param name="Id"></param> /// <returns></returns> public int DelById(string Id) { try { return ss.DelById(Id); } catch (Exception ex) { throw ex; } } #endregion #region 根据条件删除数据 /// <summary> /// 根据条件删除数据 /// </summary> /// <param name="delWhere">返回值为bool类型的委托对象(方法)</param> /// <returns></returns> public int DelByCondition(Expression<Func<Sys_Log, bool>> delWhere) { try { //查询 return ss.DelByCondition(delWhere); } catch (Exception ex) { throw ex; } } #endregion #region 修改+Modify(Sys_Log p, params string[] proNames) /// <summary> /// 修改数据 /// </summary> /// <param name="p">要修改的对象</param> /// <param name="proName">要修改的属性名称集合</param> /// params 可变参数 /// <returns></returns> public int Modify(Sys_Log p, params string[] proNames) { try { return ss.Modify(p, proNames); } catch (Exception) { throw; } } #endregion #region 根据条件检索集合中的数据 List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda) /// <summary> /// 根据条件检索集合中的数据 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda) { try { return ss.GetListByCondition(whereLambda); } catch (Exception ex) { throw ex; } } #endregion #region + 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba) /// <summary> /// 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> /// 这么的Tkey不是必要的使用的时候可以不写 public List<Sys_Log> GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda, Expression<Func<Sys_Log, Tkey>> orderLamba) { try { return ss.GetListByCondition<Tkey>(whereLambda,orderLamba); } catch (Exception ex) { throw ex; } } #endregion #region 根据条件和排序查询一分页的形式展示 /// <summary> /// 根据条件和排序查询一分页的形式展示 /// </summary> /// <typeparam name="Tkey">排序字段类型</typeparam> /// <param name="pageIndex">查询的页面</param> /// <param name="pageSize">每页显示的条数</param> /// <param name="orderLambda">排序的字段</param> /// <param name="whereLambda">查询的条件</param> /// <returns></returns> public List<Sys_Log> GetPageList<Tkey>(int pageIndex, int pageSize, Expression<Func<Sys_Log, Tkey>> orderLambda, Expression<Func<Sys_Log, bool>> whereLambda) { try { return ss.GetPageList(pageIndex, pageSize, orderLambda, whereLambda); } catch (Exception ex) { throw ex; } } #endregion } }
3、UI层
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NightCatWeb.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="gvParas" runat="server"> </asp:GridView> </div> </form> </body> </html> --------------------------------------------------------------------- using Night.Models; using NightCat.BLL; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace NightCatWeb { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { SysLogManager sm = new SysLogManager(); List<Sys_Log> sl = sm.GetPageList(1, 5, p => p.F_Id, p => p.F_IPAddress != "").ToList(); this.gvParas.DataSource = sl; this.gvParas.DataBind(); } } } }