您的位置:首页 > 数据库

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: