SQL Sever
declare @pageindex int;--定义变量
declare @pagesize int;
set @pageindex=1;--给变量赋值 页数
set @pagesize=3;--没有的记录数
select top(@pagesize)*from T_News where Id not in
(
select top ((@pageindex-1)*@pagesize)Id from T_News order by Id
)
order by Id
前台
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TextBox ID="txtKey" runat="server"></asp:TextBox>
<asp:ImageButton ID="btnQuery" Width="30" Height="30" ImageUrl="imges/天使.gif"
runat="server" OnClick="btnQuery_Click" />
</td>
</tr>
<tr>
<td>
<div id="divResult" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="btnFirst" runat="server" οnclick="btnFirst_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="btnPre" runat="server" οnclick="btnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" οnclick="btnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" οnclick="btnLast_Click">最后一页</asp:LinkButton>
</td>
</tr>
</table>
</div>
</form>
</body>
后台
public partial class WebForm1 : System.Web.UI.Page
{
int pagesize = 3;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = 1;
LoadData();
}
}
private void GetCount()
{
string strcon = @"Data Source=Lan-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=478950";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM T_News WHERE NewsTitle LIKE @newskey OR NewsContent LIKE @newskey";
cmd.Parameters.AddWithValue("@newskey", "%" + txtKey.Text + "%");
conn.Open();
int totalcount = Convert.ToInt32(cmd.ExecuteScalar());//返回第一行第一列的值
if (totalcount % pagesize == 0)
{
ViewState["pagelastindex"] = totalcount / pagesize;
}
else
{
ViewState["pagelastindex"] = totalcount / pagesize + 1;
}
}
private void LoadData()
{
string strcon = @"Data Source=Lan-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=478950";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT TOP(@pagesize) * FROM T_News WHERE(NewsTitle LIKE @newskey OR NewsContent LIKE @newskey) AND Id NOT IN(SELECT TOP ((@pageindex-1)*@pagesize) Id FROM T_News WHERE NewsTitle LIKE @newskey OR NewsContent LIKE @newskey ORDER BY Id )ORDER BY Id";
cmd.Parameters.AddWithValue("@pageindex", Convert.ToInt32(ViewState["pageindex"]));
cmd.Parameters.AddWithValue("@pagesize", pagesize);
cmd.Parameters.AddWithValue("@newskey", "%" + txtKey.Text + "%");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
StringBuilder sb1 = new StringBuilder();
sb1.Append("<table border=2>");
sb1.Append("<tr><td>标题</td><td>内容</td><td>创建时间</td></tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb1.Append("<tr>");
sb1.Append("<td>" + dt.Rows[i]["NewsTitle"].ToString() + "</td>");
sb1.Append("<td>" + dt.Rows[i]["NewsContent"].ToString() + "</td>");
sb1.Append("<td>" + dt.Rows[i]["CreateTime"].ToString() + "</td>");
sb1.Append("</tr>");
}
sb1.Append("</table>");
divResult.InnerHtml = sb1.ToString();
}
protected void btnQuery_Click(object sender, ImageClickEventArgs e)
{
ViewState["pageindex"] = 1;
GetCount();
LoadData();
}
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"]);
GetCount();
if (pageindex < Convert.ToInt32(ViewState["pagelastindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnLast_Click(object sender, EventArgs e)
{//最后一页
GetCount();
ViewState["pageindex"] = ViewState["pagelastindex"];
LoadData();
}
}