DataSet分页
主要利用DataSet的筛选数据的方法
DataAdapter用Fill方法填充DataSet的时候把所取的整个记录加入到DataSet
例如:
MyAdapter.Fill(DataSet,TableName);
有时候并不需要将整个查询数据都导入DataSet,真正需要的只是数据的一部。Fill的另一种方法可以满足这种需要。
MyAdapter.Fill(DataSet,StartRow,RowsCount,TableName);
可以看到在这种使用方法中有多出了两个整型参数StartRow与RowCout,他们标识将数据源中从StartRow位置取出RowsCount条记录导入DataSet。得到数据后,程序还将DataSet中的数据显示出来。
代码如下:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fenye.aspx.cs" Inherits="fenye" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DataList分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="good" runat="server" Width="530px">
<ItemTemplate>
编号:<%#DataBinder.Eval(Container.DataItem,"Record_id") %>
</ItemTemplate>
</asp:DataList></div>
共有<asp:Label ID="lblRecordCount" runat="server"></asp:Label>条记录
当前为<asp:Label ID="lblCurrentPage" runat="server"/>/<asp:Label ID="lblPageCount" runat="server" Text="Label"/>页
<asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick">上一页</asp:LinkButton>
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class fenye : System.Web.UI.Page
{
SqlConnection sqlcon;
int PageSize, RecordCount, PageCount, CurrentPage;
public void Page_Load(object sender, EventArgs e)
{
PageSize = 10;
sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=.;database=sony;User Id=sa;Password=local";
sqlcon.Open();
//第一次请求执行
if (!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少条记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共多少页
PageCount = RecordCount / PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from good";
SqlCommand sqlcmd = new SqlCommand(strCount, sqlcon);
SqlDataReader sdr = sqlcmd.ExecuteReader();
if (sdr.Read())
{
intCount = Int32.Parse(sdr["co"].ToString());
}
else
{
intCount = 0;
}
sdr.Close();
return intCount;
}
ICollection CreateSource() //ICollection为何使用?
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from good";
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(strSel, sqlcon);
sda.Fill(ds, StartIndex, PageSize, "good");//分页的关键所在,该句表示将数据源中从StartIndex位置取出PageSize条记录导入DataSet.
return ds.Tables["good"].DefaultView;
}
public void ListBind()
{
good.DataSource = CreateSource();
good.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fenye.aspx.cs" Inherits="fenye" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DataList分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="good" runat="server" Width="530px">
<ItemTemplate>
编号:<%#DataBinder.Eval(Container.DataItem,"Record_id") %>
</ItemTemplate>
</asp:DataList></div>
共有<asp:Label ID="lblRecordCount" runat="server"></asp:Label>条记录
当前为<asp:Label ID="lblCurrentPage" runat="server"/>/<asp:Label ID="lblPageCount" runat="server" Text="Label"/>页
<asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick">上一页</asp:LinkButton>
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class fenye : System.Web.UI.Page
{
SqlConnection sqlcon;
int PageSize, RecordCount, PageCount, CurrentPage;
public void Page_Load(object sender, EventArgs e)
{
PageSize = 10;
sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=.;database=sony;User Id=sa;Password=local";
sqlcon.Open();
//第一次请求执行
if (!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少条记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共多少页
PageCount = RecordCount / PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from good";
SqlCommand sqlcmd = new SqlCommand(strCount, sqlcon);
SqlDataReader sdr = sqlcmd.ExecuteReader();
if (sdr.Read())
{
intCount = Int32.Parse(sdr["co"].ToString());
}
else
{
intCount = 0;
}
sdr.Close();
return intCount;
}
ICollection CreateSource() //ICollection为何使用?
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from good";
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(strSel, sqlcon);
sda.Fill(ds, StartIndex, PageSize, "good");//分页的关键所在,该句表示将数据源中从StartIndex位置取出PageSize条记录导入DataSet.
return ds.Tables["good"].DefaultView;
}
public void ListBind()
{
good.DataSource = CreateSource();
good.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
}