using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Net.Cache; using System.Text; using Dapper; using DapperExtensions; using TestData.Entity; using TestData.Business; namespace TestData.Business { ////// 业务逻辑的基类 包含了一些简单的操作 /// /// public abstract class BaseManager where T : class, IDataEntity { /// /// 查询所有 /// /// public IEnumerable GetAll() { using (var conn = ConnectionFactory.Connection) { return conn.GetList (); } } /// /// 根绝sql查询 /// /// sql /// 参数列表 /// public IEnumerable SelectCommond(string sql, object parameters = null) { using (var conn = ConnectionFactory.Connection) { return conn.Query (sql, parameters); } } /// /// 根据表明查询 /// /// /// public IEnumerable GetAll(string name) { using (var conn = ConnectionFactory.Connection) { return conn.Query (string.Format("select * from {0}", name)); } } /// /// 根据编号查询 /// /// /// public T GetById(int? id) { if (id == null) return default(T); using (var conn = ConnectionFactory.Connection) { return conn.Get (id.Value); } } /// /// 修改实体 /// /// 实体对象 /// public bool Update(T t) { using (var conn = ConnectionFactory.Connection) { t.EditorDate = DateTime.Now; return conn.Update(t); } } /// /// 得到数量 /// /// public int GetCount() { using (var conn = ConnectionFactory.Connection) { return conn.Count (null); } } /// /// 分页 /// /// /// /// /// public Tuple<int, IEnumerable > GetPaged(object predicate, int pageindex, int pageSize) { using (var conn = ConnectionFactory.Connection) { var sort = new List { Predicates.Sort (p=>p.EditorDate) }; var total = conn.Count (predicate); return new Tuple<int, IEnumerable >(total, conn.GetPage (predicate, sort, pageindex, pageSize).ToList()); } } /// /// 添加 /// /// 实体对象 /// public bool Insert(T t) { t.EditorDate = DateTime.Now; return this.Add(t, false) == t.Id; } /// /// 添加实体集合 /// /// /// public bool Insert(List list) { using (var conn = ConnectionFactory.Connection) { list.ForEach(p => p.EditorDate = DateTime.Now); return conn.Insert(list); } } /// /// 添加实体 /// /// 实体对象 /// /// public int Add(T t, bool isAutoGenId = true) { using (var conn = ConnectionFactory.Connection) { //var maxindex = conn.Query (string.Format("select max(indexs) from {0}", typeof(T).Name)).FirstOrDefault() ?? 0; //t.Indexs = maxindex + 1; return conn.Insert(t, isGenId: isAutoGenId); } } ////// 根据编号删除 /// /// /// public bool Delete(int? id) { var obj = this.GetById(id); if (obj == null) return false; return this.Update(obj); } /// /// 根据编号修改 /// /// /// /// public bool UpdataStatus(int? id) { var obj = this.GetById(id); if (obj == null) return false; return this.Update(obj); } /// /// 根据外键得到数据 /// /// 外键名称 /// 外键的值 /// public IEnumerable GetByForeignKey(string foreignKeyName, Guid foreignKeyValue) { using (var conn = ConnectionFactory.Connection) { return conn.Query (string.Format("select * from {0} where {1}=@value", typeof(T).Name, foreignKeyName), new { value = foreignKeyValue }); } } /// /// 根据列查询 /// /// 列名称 /// 列的值 /// public IEnumerable GetByField(string fieldName, dynamic fieldValue) { using (var conn = ConnectionFactory.Connection) { return conn.Query (string.Format("select * from {0} where {1}=@value", typeof(T).Name, fieldName), new { value = fieldValue }); } } /// /// lxh 根据某列查询的方法--带排序 /// /// 查询列名 /// 条件内容 /// 排序列名 /// public IEnumerable GetByField(string fieldName, dynamic fieldValue, string sortFieldName) { using (var conn = ConnectionFactory.Connection) { return conn.Query (string.Format("select * from {0} where {1}=@value order by {2}", typeof(T).Name, fieldName, sortFieldName), new { value = fieldValue }); } } /// /// lxh 获取排序号的方法 /// /// public int GetNextSequence(T t) { using (var conn = ConnectionFactory.Connection) { return conn.Query<int>(string.Format("select isnull(max(Sequence),0)+1 from {0}", typeof(T).Name)).FirstOrDefault(); } } /// /// 存储过程 /// /// /// /// public List<dynamic> SelectProc(string procName, object obj = null) { using (var conn = ConnectionFactory.Connection) { return conn.Query(procName, obj, commandType: CommandType.StoredProcedure).ToList(); } } } }
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Net.Cache;using System.Text;using Dapper;using DapperExtensions;using TestData.Entity;using TestData.Business;
namespace TestData.Business{ ///
///
return conn.Get
///
///
var total = conn.Count
///
///