SQlserver的分页,以及连接vs的Ajax无刷新分页
2013-05-21 19:34
127 查看
一
1.sql简单分页
//以id为标准,从5到10的数据。
select * from(select row_number() over(order by Id)as rownumber,*from T_News1)T
where rownumber>=5 and rownumber<=10
2.sql分页
//定义两个变量于存储过程中,每页5条数据,第二页
GO
create proc pro_fenye
@pagesize int,
@pageindex int
as
select * from(select ROW_NUMBER() over(order by Id) as rownumber,* from T_News1)T
WHERE rownumber>(@pageindex-1)*@pagesize AND rownumber<=@pageindex*@pagesize
GO
EXEC pro_fenye 5,2
二
三层架构构建分页
1.用动软生成三层并改写其中无用内容
2.前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Height="173px" Width="301px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="编号" />
<asp:BoundField DataField="NewsTitle" HeaderText="标题" />
<asp:BoundField DataField="NewsContent" HeaderText="内容" />
<asp:BoundField DataField="CreateTime" HeaderText="创建时间" />
</Columns>
</asp:GridView>
</div>
<div>
<asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="btnPre" runat="server" onclick="btnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" onclick="btnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一页</asp:LinkButton>
<asp:TextBox ID="txtPageindex" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton5" runat="server" onclick="LinkButton5_Click">Go</asp:LinkButton>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace _5_21分页
{
public partial class WebForm1 : System.Web.UI.Page
{
int pagesize = 10;
int pageindex = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = 1;
GetLastPageindex();
LoadData();
}
}
private void GetLastPageindex()
{
BLL.T_News1 bnews = new BLL.T_News1();
int totalcount= bnews.GetRecordCount("");
if (totalcount % pagesize == 0)
{
ViewState["lastpageindex"] = totalcount / pagesize;
}
else
{
ViewState["lastpageindex"] = totalcount / pagesize+1;
}
}
private void LoadData()
{
BLL.T_News1 bnews = new BLL.T_News1();
DataTable dt = bnews.GetListDataTable(pagesize, Convert.ToInt32(ViewState["pageindex"]));
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = 1;
LoadData();
}
protected void btnPre_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex>1)
{
pageindex--;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex<Convert.ToInt32(ViewState["lastpageindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LoadData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
int result;
if (int.TryParse(txtPageindex.Text, out result) == true)
{
ViewState["pageindex"] = txtPageindex.Text.Trim();
LoadData();
}
else
{
txtPageindex.Text = "请输入合法的数字";
}
}
}
}
Web.config://连接数据库
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="sqlservercon" connectionString="Data Source=PC-201303062250;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=ls785869"/>
</connectionStrings>
</configuration>
1.sql简单分页
//以id为标准,从5到10的数据。
select * from(select row_number() over(order by Id)as rownumber,*from T_News1)T
where rownumber>=5 and rownumber<=10
2.sql分页
//定义两个变量于存储过程中,每页5条数据,第二页
GO
create proc pro_fenye
@pagesize int,
@pageindex int
as
select * from(select ROW_NUMBER() over(order by Id) as rownumber,* from T_News1)T
WHERE rownumber>(@pageindex-1)*@pagesize AND rownumber<=@pageindex*@pagesize
GO
EXEC pro_fenye 5,2
二
三层架构构建分页
1.用动软生成三层并改写其中无用内容
2.前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Height="173px" Width="301px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="编号" />
<asp:BoundField DataField="NewsTitle" HeaderText="标题" />
<asp:BoundField DataField="NewsContent" HeaderText="内容" />
<asp:BoundField DataField="CreateTime" HeaderText="创建时间" />
</Columns>
</asp:GridView>
</div>
<div>
<asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="btnPre" runat="server" onclick="btnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" onclick="btnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一页</asp:LinkButton>
<asp:TextBox ID="txtPageindex" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton5" runat="server" onclick="LinkButton5_Click">Go</asp:LinkButton>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace _5_21分页
{
public partial class WebForm1 : System.Web.UI.Page
{
int pagesize = 10;
int pageindex = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = 1;
GetLastPageindex();
LoadData();
}
}
private void GetLastPageindex()
{
BLL.T_News1 bnews = new BLL.T_News1();
int totalcount= bnews.GetRecordCount("");
if (totalcount % pagesize == 0)
{
ViewState["lastpageindex"] = totalcount / pagesize;
}
else
{
ViewState["lastpageindex"] = totalcount / pagesize+1;
}
}
private void LoadData()
{
BLL.T_News1 bnews = new BLL.T_News1();
DataTable dt = bnews.GetListDataTable(pagesize, Convert.ToInt32(ViewState["pageindex"]));
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = 1;
LoadData();
}
protected void btnPre_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex>1)
{
pageindex--;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex<Convert.ToInt32(ViewState["lastpageindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = ViewState["lastpageindex"];
LoadData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
int result;
if (int.TryParse(txtPageindex.Text, out result) == true)
{
ViewState["pageindex"] = txtPageindex.Text.Trim();
LoadData();
}
else
{
txtPageindex.Text = "请输入合法的数字";
}
}
}
}
Web.config://连接数据库
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="sqlservercon" connectionString="Data Source=PC-201303062250;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=ls785869"/>
</connectionStrings>
</configuration>
相关文章推荐
- 临时表vs.表变量以及它们对SQLServer性能的影响
- JavaScript连接SqlServer并实现分页(二)
- JAVA连接Oracle以及SQLserver 2008数据库代码
- SQL Server 存储过程的分页方案比拼,以及SQLServer的API服务游标信息!
- 自动化打开并连接visual studio emulator for android,支持eclipse vs 以及as调试
- 关于SqlServer的DBHelper类以及数据分页
- sqlserver2005的连接以及与VS的连接问题
- 临时表vs.表变量以及它们对SQLServer性能的影响
- JavaScript连接SqlServer并实现分页(二)
- 临时表vs.表变量以及它们对SQLServer性能的影响
- 临时表vs.表变量以及它们对SQLServer性能的影响
- JDBC实现增删改查、模糊查询、分页查询、子查询以及体现单例设计模式连接数据库
- 临时表vs.表变量以及它们对SQLServer性能的影响
- VS环境下 Ibatis .Net 连接sqlserver 2005 碰到的问题 Unable to open connection to "Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0".
- C#连接SqlServer以及插入数据的实例
- sqlserver和oracle以及mysql的分页
- mysql、sqlserver、db2、oracle、hsql数据库获取数据库连接方法及分页函数
- jdbc 连接mysql 以及sqlserver 心得
- 安装vs2013 Sqlserver 无法连接远程服务器的解决方法
- SQLSERVER,ORACLE,MYSQL的分页与java连接字符串总结