在C#中,怎么分页显示数据库里的记录?
在C#中,用listview控件怎么分页显示数据库里面的记录,比如有20条记录,但每页只需显示5条!而且是要调用数据库里面已经写好的存储过程!...
在C#中,用listview控件怎么分页显示数据库里面的记录,比如有20条记录,但每页只需显示5条!而且是要调用数据库里面已经写好的存储过程!
展开
3个回答
展开全部
给你一个完整的例子,至于存储过程的调用之类的你应该会吧
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.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 Score";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.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();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
<asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
</form>
</body>
</html>
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.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 Score";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.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();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
<asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
</form>
</body>
</html>
展开全部
//currentpage--- 当前第几页
//pagesize--- 当前页有多少条数据
//standards--- 页面规格(即每页显示多少条数据)
查询的SQL语句为:
string SQL_SELECT= " select * from (select top " + pagesize + " * from (";
SQL_SELECT = SQL_SELECT + " select top " + currentpage * standards + " * from TABEL1 ";
if (条件!= "")
{ SQL_SELECT = SQL_SELECT + "where 条件=" + 条件; }
SQL_SELECT = SQL_SELECT + " order by ID asc) as tb1 order by ID desc) as tb2 order by ID asc";
//pagesize--- 当前页有多少条数据
//standards--- 页面规格(即每页显示多少条数据)
查询的SQL语句为:
string SQL_SELECT= " select * from (select top " + pagesize + " * from (";
SQL_SELECT = SQL_SELECT + " select top " + currentpage * standards + " * from TABEL1 ";
if (条件!= "")
{ SQL_SELECT = SQL_SELECT + "where 条件=" + 条件; }
SQL_SELECT = SQL_SELECT + " order by ID asc) as tb1 order by ID desc) as tb2 order by ID asc";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你一个完整的例子,至于存储过程的调用之类的你应该会吧
<%
@
Page
Language="C#"
%>
<%
@
Import
Namespace="System.Data"
%>
<%
@
Import
Namespace="System.Data.OleDb"
%>
<Script
Language="C#"
Runat="Server">
OleDbConnection
MyConn;
int
PageSize,RecordCount,PageCount,CurrentPage;
public
void
Page_Load(Object
src,EventArgs
e)
{
//设定PageSize
PageSize
=
10;
//连接语句
string
MyConnString
=
"Provider=Microsoft.Jet.OLEDB.4.0;
Data
Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn
=
new
OleDbConnection(MyConnString);
MyConn.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
Score";
OleDbCommand
MyComm
=
new
OleDbCommand(strCount,MyConn);
OleDbDataReader
dr
=
MyComm.ExecuteReader();
if(dr.Read())
{
intCount
=
Int32.Parse(dr["co"].ToString());
}
else
{
intCount
=
0;
}
dr.Close();
return
intCount;
}
ICollection
CreateSource()
{
int
StartIndex;
//设定导入的起终地址
StartIndex
=
CurrentPage*PageSize;
string
strSel
=
"select
*
from
Score";
DataSet
ds
=
new
DataSet();
OleDbDataAdapter
MyAdapter
=
new
OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
return
ds.Tables["Score"].DefaultView;
}
public
void
ListBind()
{
score.DataSource
=
CreateSource();
score.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();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form
runat="server">
共有<asp:Label
id="lblRecordCount"
ForeColor="red"
runat="server"
/>条记录
当前为<asp:Label
id="lblCurrentPage"
ForeColor="red"
runat="server"
/>/<asp:Label
id="lblPageCount"
ForeColor="red"
runat="server"
/>页
<asp:DataList
id="score"
runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%#
DataBinder.Eval(Container.DataItem,"Name")
%>
<asp:LinkButton
id="btnSelect"
Text="编辑"
CommandName="edit"
runat="server"
/>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton
id="lbnPrevPage"
Text="上一页"
CommandName="prev"
OnCommand="Page_OnClick"
runat="server"
/>
<asp:LinkButton
id="lbnNextPage"
Text="下一页"
CommandName="next"
OnCommand="Page_OnClick"
runat="server"
/>
</form>
</body>
</html>
<%
@
Page
Language="C#"
%>
<%
@
Import
Namespace="System.Data"
%>
<%
@
Import
Namespace="System.Data.OleDb"
%>
<Script
Language="C#"
Runat="Server">
OleDbConnection
MyConn;
int
PageSize,RecordCount,PageCount,CurrentPage;
public
void
Page_Load(Object
src,EventArgs
e)
{
//设定PageSize
PageSize
=
10;
//连接语句
string
MyConnString
=
"Provider=Microsoft.Jet.OLEDB.4.0;
Data
Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn
=
new
OleDbConnection(MyConnString);
MyConn.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
Score";
OleDbCommand
MyComm
=
new
OleDbCommand(strCount,MyConn);
OleDbDataReader
dr
=
MyComm.ExecuteReader();
if(dr.Read())
{
intCount
=
Int32.Parse(dr["co"].ToString());
}
else
{
intCount
=
0;
}
dr.Close();
return
intCount;
}
ICollection
CreateSource()
{
int
StartIndex;
//设定导入的起终地址
StartIndex
=
CurrentPage*PageSize;
string
strSel
=
"select
*
from
Score";
DataSet
ds
=
new
DataSet();
OleDbDataAdapter
MyAdapter
=
new
OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
return
ds.Tables["Score"].DefaultView;
}
public
void
ListBind()
{
score.DataSource
=
CreateSource();
score.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();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form
runat="server">
共有<asp:Label
id="lblRecordCount"
ForeColor="red"
runat="server"
/>条记录
当前为<asp:Label
id="lblCurrentPage"
ForeColor="red"
runat="server"
/>/<asp:Label
id="lblPageCount"
ForeColor="red"
runat="server"
/>页
<asp:DataList
id="score"
runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%#
DataBinder.Eval(Container.DataItem,"Name")
%>
<asp:LinkButton
id="btnSelect"
Text="编辑"
CommandName="edit"
runat="server"
/>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton
id="lbnPrevPage"
Text="上一页"
CommandName="prev"
OnCommand="Page_OnClick"
runat="server"
/>
<asp:LinkButton
id="lbnNextPage"
Text="下一页"
CommandName="next"
OnCommand="Page_OnClick"
runat="server"
/>
</form>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询