1 建议基本分页方法的基础->sql语句函数
/// <summary>
///产生分页必须的sql语句的方法
/// </summary>
/// <param name="tabName">表名称</param>
/// <param name="fieldName">查询的字段名</param>
/// <param name="fldName">主键名</param>
/// <param name="pageSize">每页显示数量</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="strWhere">增加条件语句</param>
/// <param name="orderType">排序类别 TRUE 的话是降序排列</param>
/// <returns></returns>
public static string Asppage(string tabName, string fieldName, string fldName,string ordername, int pageSize, int pageIndex, string strWhere, bool orderTyp)
{
StringBuilder sbSql = new StringBuilder();
string strTemp = "";
string strOrder = "";
if (orderTyp)
{
strOrder = " order by " + ordername + " desc";
strTemp = " not in (select top " + (pageIndex - 1) * pageSize + " " + fldName + " from " + tabName + strOrder + ")";
}
else
{
strOrder = " order by " + ordername + " asc";
strTemp = " not in (select top " + (pageIndex - 1) * pageSize + " " + fldName + " from " + tabName + strOrder + ")";
}
if (pageIndex == 1)
{
strTemp = "";
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strTemp = " WHERE " + strWhere;
}
sbSql.Append("select top " + pageSize + " " + fieldName + " FROM " + tabName + " " + strTemp + " " + strOrder);
}
else
{
if (string.IsNullOrEmpty(strWhere.Trim()))
{
sbSql.Append("select top " + pageSize + " " + fieldName + " from " + tabName + " where " + fldName + strTemp + strOrder);
}
else
{
strTemp = " not in (select top " + (pageIndex - 1) * pageSize + " " + fldName + " from " + tabName + " where " + strWhere + strOrder + ")";
sbSql.Append("select top " + pageSize + " " + fieldName + " from " + tabName + " where " + fldName + strTemp + " and " + strWhere + strOrder);
}
}
return sbSql.ToString();
}
-----------------------------------------------------------------
2 调用上面产生的sql 语句,返回数据datable的方法
/// <summary>
/// 返回页面的信息,全部按降序排列
/// </summary>
/// <param name="everypagenuber">每页分页数量</param>
/// <param name="nubcount">返回总数量</param>
/// <param name="pagecount">返回总页数</param>
/// <param name="indexpage">输入当前页码</param>
/// <param name="strwhere">条件语句</param>
/// <param name="strorderby">排序条件</param>
/// <param name="userid">当前用户id</param>
/// <returns>返回内容列表</returns>
public DataTable pagedate(int everypagenuber, ref Decimal nubcount,ref Decimal pagecount,int indexpage,string strwhere,string strorderby)
{
string sql2 = " select count(1) from lc_order where " + strwhere;
DataTable rs1 = DAL.SQLHelper.ExecuteDataTable(DAL.SQLHelper.CONN_STRING, CommandType.Text, sql2);
nubcount = Decimal.Parse(rs1.Rows[0][0].ToString());//总数
if(nubcount==0)
{
pagecount=0;
}else
{
pagecount = Math.Ceiling(nubcount / everypagenuber);
}
string sql = DLL.Input.Asppage("lc_order", " * ", "id", strorderby, everypagenuber, indexpage, strwhere, true);
DataTable rs = new DataTable();
rs = DAL.SQLHelper.ExecuteDataTable(DAL.SQLHelper.CONN_STRING, CommandType.Text, sql);
return rs;
}
-----------------------------------------------------------
3. 在cs页面中如此使用
DLL.lc_order mode = new DLL.lc_order();
string sqlwhere =" addtime between convert(datetime,'" + btime.Text.Trim() + "') and convert(datetime,'" + etime.Text.Trim() + "') and (order_status=0 and issoft=0 and sh=0) and userid=" + DLL.admins.getuserid();
string page=Request.QueryString["page"];
if(string.IsNullOrEmpty(page))
{
page="1";
}
Decimal nubcount=0,pagecount=0;
rp_NewsList.DataSource=mode.pagedate(2,ref nubcount,ref pagecount,int.Parse(page),sqlwhere,"addtime");
rp_NewsList.DataBind();
Literal1.Text = DLL.Input.pagehtml(int.Parse(page),Convert.ToInt16(pagecount), 0);
-------------------------------------------------------
4. 在aspx页面中使用
<asp:Repeater ID="rp_NewsList" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>