c# 通用分页查询类

时间:2022-08-09 16:23:19
        #region DataTable 分页
/// <summary>
/// 通用分页查询 DataTable
/// </summary>
/// <param name="pageSize">分页大小</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="TableName">查询表名</param>
/// <param name="ColumnName">查询字段,逗号分割</param>
/// <param name="WhereStr">查询条件,不带where</param>
/// <param name="SelectOrder">排序字段</param>
/// <param name="OrderType">排序类型,0降序 1升序</param>
/// <returns></returns>
public static DataTable getDataTable(int pageSize, int pageIndex, string TableName, string ColumnName, string WhereStr, string SelectOrder, int OrderType)
{
string @TemStr;
string @TemOrder;
int @Pages = pageSize * (pageIndex - 1);

if (OrderType != 0)//判断排序
{
@TemStr = ">(SELECT MAX";
@TemOrder = " ORDER BY " + SelectOrder + " ASC";
}
else
{
@TemStr = "<(SELECT MIN";
@TemOrder = " ORDER BY " + SelectOrder + " DESC";
}
StringBuilder SqlStr = new StringBuilder();
if (pageIndex == 1) //当页码为第一页时
{
if (WhereStr == "") //判断是否有条件查询
{
SqlStr.Append("SELECT TOP " + pageSize + " " + ColumnName + " FROM " + TableName + " " + @TemOrder);
}
else
{
SqlStr.Append("SELECT TOP " + pageSize + " " + ColumnName + " FROM " + TableName);
SqlStr.Append(" WHERE " + WhereStr + " " + @TemOrder);
}
}
else
{
if (WhereStr == "") //判断是否有条件查询
{
SqlStr.Append("SELECT TOP " + pageSize + " " + ColumnName + " FROM " + TableName + " WHERE ");
SqlStr.Append(SelectOrder + " " + @TemStr + "(" + SelectOrder + ") FROM (SELECT TOP " + @Pages + " ");
SqlStr.Append(SelectOrder + " FROM " + TableName + " " + @TemOrder + ")as t)" + @TemOrder);
}
else
{
SqlStr.Append("SELECT TOP " + pageSize + " " + ColumnName + " FROM " + TableName + " WHERE " + SelectOrder + " ");
SqlStr.Append(@TemStr + "(" + SelectOrder + ") FROM (SELECT TOP " + @Pages + " " + SelectOrder + " FROM ");
SqlStr.Append(TableName + " WHERE " + WhereStr + " " + @TemOrder + ")as t) and (" + WhereStr + ") " + @TemOrder);
}
}

SqlParameter[] parameters = {
};
return SqlHelper30.Fill(CommandType.Text, SqlStr.ToString(),parameters).Tables[0];
}
#endregion

/// <summary>
/// 获取查询记录总数
/// </summary>
/// <param name="TableName">表名</param>
/// <param name="WhereStr">查询条件,可为空</param>
/// <returns></returns>
public static int getCount(string TableName, string WhereStr)
{
StringBuilder SqlStr = new StringBuilder();
SqlStr.Append("");
if (WhereStr != "")
{
SqlStr.Append("SELECT count(*) FROM ");
SqlStr.Append(TableName + " WHERE " + WhereStr);
}
else
{
SqlStr.Append("SELECT count(*) FROM " + TableName);
}
Object[] objectValues = new Object[] { };
return int.Parse(SqlHelper30.ExecuteScalar(SqlStr.ToString(), objectValues).ToString());
}


调用方法:

        protected void AspNetPager1_PageChanged(object src, PageChangedEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BusinessListBind();
        }
        protected void BusinessListBind()
        {
            string ColumnN = " id,rq,sj,yhxm,yhdz,yhdh,lr,slr,lb,xq,zt,pdr,pdrq,pdsj,jdr,hdrq,hdsj,hdjlr,jg,tdyy,ts,kfbfzrsj,jdbfzrsj,slbm,jdry,bztd,jdtd,gzfy,rgf,gzry,kfbmbzwcsj,jdbmbzwcsj,hdwcsj,jdbm,bbh,wcsj,jdwcsj,jdsj,bztdyy,jdzt,yqsj,tdzt,jdrid,dybs,hfbs,cdyy,lrr,yhyj,xdsj,xdry,hfsj,hfnr,hfr,yhpj,yhyj1,yhpj1,xdsj1,xdry1,sdpdsj,ckpdsj,clhfsj,wcqrsj,pdlx,userid,flagck,jdrdh,firstcdsj,secondcdsj,tplj,thircdsj,sfje,azlx,sfyl,lgylsfrh,snsfazhndg,bjlx,bjk,bxh,bjbaztj,ktsj,ktr,ktbz,lrbs ";

            RepList.DataSource = Hjzx_BLL.getDataTable(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "hjzx1", ColumnN, getStrSql(), "id", 0);
            RepList.DataBind();

            AspNetPager1.RecordCount = Hjzx_BLL.getCount("hjzx1",getStrSql());
            AspNetPager1.CustomInfoText = "派单总数:<font color=\"blue\"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>";
        }