/// <summary> /// 使用提供的条件查询数据库(表由entityType指定),返回满足条件的记录(分页) /// </summary> /// <typeparam name="T">要查询的实体类型,标记了DbTable和DbField特性的类,并且具有空白构造函数</typeparam> /// <param name="connector"></param> /// <param name="criteria">查询条件字典,键是PropertyName,条件仅支持=比较,条件间仅支持AND运算</param> /// <param name="pageIndex">从0开始的返回页索引</param> /// <param name="pageSize">每页几行记录</param> /// <param name="sortProperty">用于排序的PropertyName(如为空则使用关键字)</param> /// <param name="descending">是否倒序排列</param> /// <param name="total">符合条件的记录总数</param> /// <returns></returns> public static async Task <PagedQuery <T> > SearchEntities <T>(DbConnector connector, Dictionary <string, object> criteria, int pageIndex, int pageSize, string sortProperty, bool descending) where T : new() { Type entityType = typeof(T); if (!(DbTableAttribute.GetCustomAttribute(entityType, typeof(DbTableAttribute)) is DbTableAttribute tableAttr)) { throw new Exception("类型" + entityType.FullName + "没有标记DbTableAttribute特性"); } string selectClause = "SELECT * "; string fromClause = "FROM " + tableAttr.TableName; string orderBy; #region 正确处理orderBy DbFieldAttribute sortFieldAttr = null; if (string.IsNullOrWhiteSpace(sortProperty)) { //如果没有传递sortProperty参数, 则使用PrimaryKey foreach (PropertyInfo property in entityType.GetProperties()) { DbFieldAttribute fldAttr = DbFieldAttribute.GetCustomAttribute(property, typeof(DbFieldAttribute)) as DbFieldAttribute; if (fldAttr != null && fldAttr.IsPrimaryKey) { sortFieldAttr = fldAttr; break; } } } else { PropertyInfo property = entityType.GetProperty(sortProperty); if (property == null) { throw new Exception(string.Format("类型{0}中没有找到{1}属性用于排序", entityType.FullName, sortProperty)); } sortFieldAttr = DbFieldAttribute.GetCustomAttribute(property, typeof(DbFieldAttribute)) as DbFieldAttribute; } if (sortFieldAttr == null) { throw new Exception("类型" + entityType.FullName + "没有标记IsPrimaryKey=true的DbField特性"); } if (descending) { orderBy = sortFieldAttr.FieldName + " DESC"; } else { orderBy = sortFieldAttr.FieldName; } #endregion StringBuilder whereBuilder = new StringBuilder(); List <SqlParameter> sqlParameters = new List <SqlParameter>(); if (criteria != null) { foreach (string conditionName in criteria.Keys) { if (criteria[conditionName] == null || criteria[conditionName].ToString().Length == 0) { continue; } PropertyInfo conditionProperty = entityType.GetProperty(conditionName); if (conditionProperty == null) { throw new Exception(string.Format("类型{0}中没有找到{1}属性用于查询条件", entityType.FullName, conditionName)); } DbFieldAttribute conditionAttr = DbFieldAttribute.GetCustomAttribute(conditionProperty, typeof(DbFieldAttribute)) as DbFieldAttribute; if (conditionAttr == null) { throw new Exception(string.Format("类型{0}的{1}属性没有标记DbFieldAttribute特性, 无法用于数据库查询", entityType.FullName, conditionName)); } SqlParameter parameter = GenerateSqlParameter(conditionAttr, conditionProperty.PropertyType, criteria[conditionName]); sqlParameters.Add(parameter); whereBuilder.AppendFormat("[{0}]=@{0} ", conditionAttr.FieldName); whereBuilder.Append("AND "); } } if (whereBuilder.Length > 0) { whereBuilder.Remove(whereBuilder.Length - 4, 4); } string sql = BuildPagedSelectSql(selectClause, fromClause, whereBuilder.ToString(), orderBy, pageIndex, pageSize); DataSet ds = await connector.ExecuteSqlQuerySet(sql, sqlParameters.ToArray()); PagedQuery <T> result = new PagedQuery <T>(); if (ds != null && ds.Tables.Count == 2) { result.Total = (int)ds.Tables[0].Rows[0][0]; ds.Tables[1].TableName = "result"; result.Records = await Task.Run(() => ValueHelper.WrapEntities <T>(ds.Tables[1])); } else { result.Total = 0; result.Records = new T[0]; } ds.Dispose(); return(result); }
/// <summary> /// 分页读取数据库中全部实体,不带查询条件 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="connector">数据源</param> /// <param name="pageIndex">读取的页(从0开始)</param> /// <param name="pageSize">每页包含记录条数</param> /// <param name="sortProperty">排序方法</param> /// <returns></returns> public static async Task <PagedQuery <T> > QueryPagedEntity <T>(DbConnector connector, int pageIndex, int pageSize, string sortProperty, bool descending) where T : new() => await SearchEntities <T>(connector, null, pageIndex, pageSize, sortProperty, descending);