/// <summary> /// 分页查询,itemsPerPage=0时查询全部 /// </summary> /// <param name="sql"></param> /// <param name="itemsPerPage">每页条数</param> /// <param name="currentPage">当前页数</param> /// <param name="args">查询参数</param> /// <returns></returns> public TablePage QueryTablePage(string sql, int itemsPerPage = 20, int currentPage = 1, params object[] args) { using (var db = new LcLib.DBLib.DBHelper()) { TablePage page = new TablePage() { CurrentPage = currentPage, PerPageItems = itemsPerPage }; BuilderPageSql(sql, itemsPerPage, currentPage, db.dbType); if (itemsPerPage == 0) { page.Items = db.ExecuteDataTable(_sqlPage, args); page.TotalItems = page.Items.Rows.Count; page.TotalPages = 1; } else { int itemcount = db.ExecuteScalar <int>(_sqlCount, args); page.TotalItems = itemcount; page.TotalPages = (itemcount % itemsPerPage == 0 ? itemcount / itemsPerPage : itemcount / itemsPerPage + 1); if (itemcount == 0) { page.Items = new DataTable(); } else { page.Items = db.ExecuteDataTable(_sqlPage, args); } } return(page); } }
/// <summary> /// 根据语句,返回list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="args"></param> /// <returns></returns> public IEnumerable <T> Query <T>(string sql, params object[] args) where T : class { using (var db = new LcLib.DBLib.DBHelper()) { return(ConvertFun.ConvertToIEnumerable <T>(db.ExecuteDataTable(sql, args))); } }
/// <summary> /// 根据语句,返回对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="args"></param> /// <returns></returns> public T QueryModel <T>(string sql, params object[] args) where T : class { using (var db = new LcLib.DBLib.DBHelper()) { DataTable dt = db.ExecuteDataTable(sql, args); if (dt.Rows.Count > 0) { return(ConvertFun.ConvertToModel <T>(dt.Rows[0])); } else { return(default(T)); } } }