asp.net中用DataReader高效率分頁

來源:互聯網
上載者:User

分享一下兩種方式的分頁代碼
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];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.