时间:2021-05-28
分享一下两种方式的分页代码
1.用DataReader分页
复制代码 代码如下:
/// <summary>
/// PageList for DataReader
/// </summary>
/// <param name="connectionString"></param>
/// <param name="sql"></param>
/// <param name="pageSize"></param>
/// <param name="curPage"></param>
/// <param name="pageCount"></param>
/// <param name="count"></param>
/// <param name="cmdParms"></param>
/// <returns></returns>
public DataTable PageListReader(string connectionString, string sql, int pageSize, int curPage, out int pageCount, out int count, params DbParameter[] cmdParms)
{
int first = 0;
int last = 0;
int fieldCount = 0;
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = conn.CreateCommand();
PrepareCommand(cmd, conn, null, CommandType.Text, sql, cmdParms);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
fieldCount = reader.FieldCount;
for (int i = 0; i < fieldCount; i++)
{
DataColumn col = new DataColumn();
col.ColumnName = reader.GetName(i);
col.DataType = reader.GetFieldType(i);
dt.Columns.Add(col);
}
count = 0;
first = (curPage - 1) * pageSize+1;
last = curPage * pageSize;
while (reader.Read())
{
count++;
if (count >= first && last >= count)
{
DataRow r = dt.NewRow();
for (int i = 0; i < fieldCount; i++)
{
r[i] = reader[i];
}
dt.Rows.Add(r);
}
}
reader.Close();
pageCount = Convert.ToInt32(Math.Ceiling((double)count / (double)pageSize));
return dt;
}
}
2.用ROW_NUMBER()分页
复制代码 代码如下:
/// <summary>
/// 分页获取数据(Sql Server 2005) for ROW_NUMBER()
/// </summary>
/// <param name="connectionString">数据库链接</param>
/// <param name="sql">获取数据集的Sql</param>
/// <param name="fldSort">排序字段,可以多个</param>
/// <param name="pageSize">每页显示多少条</param>
/// <param name="curPage">当前页码</param>
/// <param name="pageCount">总页数</param>
/// <param name="count">总记录数</param>
///<param name="cmdParms">DbParameter</param>
/// <returns>DataTable</returns>
public DataTable PageList(string connectionString, string sql, string fldSort, int pageSize, int curPage, out int pageCount, out int count, params DbParameter[] cmdParms)
{
StringBuilder strSql = new StringBuilder();
strSql.AppendFormat(@"SELECT count(0) from {0} as MyTableCount;
select * from (
SELECT ROW_NUMBER() OVER(order by {1}) RowNumber,*
from {0} mytable
) mytable2
where RowNumber between {2} and {3}"
, sql, fldSort, Convert.ToString((curPage - 1) * pageSize + 1), Convert.ToString((curPage * pageSize)));
DataSet ds = ExecuteQuery(connectionString, CommandType.Text, strSql.ToString(), cmdParms);
count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
pageCount = Convert.ToInt32(Math.Ceiling((double)count / (double)pageSize));
return ds.Tables[1];
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
asp.net操作access数据库是常见的数据库操作应用,本文就来实例讲解一下asp.net实现access数据库分页的方法。希望对大家的asp.net程序设
在ASP.NET包含文件的方法有:1.2.3.StreamReader对象将包含文件写到HTTP内容流中//me:网上说asp.net中用include也可以的
本文实例讲述了asp.net中的GridView分页问题。分享给大家供大家参考。具体分析如下:在ASP.NET中,经常会使用到GridView的分页,一般情况下
一、AspNetPager分页控件分页是Web应用程序中最常用到的功能之一,在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net1.1)
本文实例讲述了asp.net控件DataList分页用法。分享给大家供大家参考,具体如下:protectedvoidPage_Load(objectsender