[Gerry] 2006-5-9
<%@ Page Language="C#" Debug="true" %>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<Script Language="C#" runat="server">
String PageName="wznPage"; //配置本页面的页面名字与本文件名相同
String TableName="Ticket"; //配置要查询的数据库表的名字
String IndexID="TicketAutoId"; //配置数据库表的主索引排序ID
String ConnString="server=localhost;uid=sa;pwd=;database=qq3"; //配置数据库连接字符串
int PageSize=7; //配置每页显示记录数
SqlConnection conn; //定义连接数据库全局变量
void Page_Load(Object src,EventArgs e)
{
conn =new SqlConnection(ConnString);
conn.Open(); //建立与数据库的连接
int RecordCount=0; //总共记录数
int PageCount=0; //总共页数
int CurrentPage=0; //当前页码
RecordCount = CountRecord(); //计算总共有多少记录
PageCount = RecordCount/PageSize; //计算总共有多少页 取整
if (RecordCount%PageSize > 0) {
PageCount = PageCount + 1; //如果不整除则加一页来显示除后剩余记录
}
if (Request.QueryString["page"]!=null) {
CurrentPage = Convert.ToInt32(Request.QueryString["page"]); //获得页的值
}
else {
CurrentPage = 1;
}
//核心sql语句部分
string sql;
sql="select top "+PageSize+" * from "+ TableName +" where "+ IndexID +" not in (";
sql+=" select top "+((CurrentPage-1)*PageSize)+" "+ IndexID +" from "+ TableName +" order by "+ IndexID +"";
sql+=" ) order by "+ IndexID +"";
//Response.Write("sql:"+sql);
SqlCommand cmd=new SqlCommand(sql,conn);
SqlDataReader myReader = cmd.ExecuteReader();
string mainHtml="<table border='0' cellpadding='0' cellspacing='0' width='100%' height='139'>";
int i=1;
while(myReader.Read())
{
mainHtml+="<tr><td width='20%'></td><td>本页第"+ i +"条数据:</td></tr>";
mainHtml+="<tr><td></td><td>数据表第1个字段的值是:"+myReader[0].ToString().Trim()+"</td></tr>";
mainHtml+="<tr><td></td><td>数据表第2个字段的值是:"+myReader[1].ToString().Trim()+"</td></tr>";
mainHtml+="<tr><td></td><td>数据表第3个字段的值是:"+myReader[2].ToString().Trim()+"</td></tr>";
mainHtml+="<tr><td></td><td><hr></td></tr>";
i++;
}
mainHtml+="</table>";
myReader.Close();
int pagePre,pageNext;
pagePre=CurrentPage-1;
pageNext=CurrentPage+1;
string pageHtml;
if (pagePre==0) {
pageHtml="首页 上页";
}
else {
pageHtml="<a href='"+ PageName +".aspx?page=1'>首页</a> <a href='"+ PageName +".aspx?page="+pagePre+"'>上页</a>";
}
if (pageNext>PageCount) {
pageHtml+=" 下页 末页";
}
else {
pageHtml+=" <a href='"+ PageName +".aspx?page="+ pageNext +"'>下页</a>";
pageHtml+=" <a href='"+ PageName +".aspx?page="+ PageCount +"'>末页</a>";
}
mblist.InnerHtml = mainHtml; //给分页主内容标签赋值
lblPageCount.Text = PageCount.ToString(); //给总页数标签赋值
lblRecordCount.Text = RecordCount.ToString();//给总记录数标签赋值
lblCurrentPage.Text = CurrentPage.ToString();//给当前页码标签赋值
pagelist.InnerHtml = pageHtml; //给翻页链接标签赋值
}
//统计数据库表中记录总数的函数
public int CountRecord()
{
int intCount = 0;
string sql = "select count(*) from "+ TableName;
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader myReader = cmd.ExecuteReader();
if(myReader.Read()) {
intCount = Int32.Parse(myReader[0].ToString());
}
myReader.Close();
return intCount;
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>#</title>
<style type="text/css">
<!--
td {
font-family: "宋体";
font-size: 14px;
}
a:link {
text-decoration: none;
color: blue;
}
a:visited {
text-decoration: none;
color: blue;
}
a:hover {
text-decoration: underline;
color: blue;
}
a:active {
text-decoration: none;
color: blue;
}
-->
</style>
</head>
<body topmargin="0" leftmargin="0">
<form runat=server>
<table width="778" border="1" cellpadding="0" cellspacing="0" bgcolor="#e2e2e2" align="center">
<tr>
<td width="562" height="400" align="center" valign="top"><br>
<!-- 数据显示位置 -->
<span id="mblist" runat="server"></span>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
<tr>
<td align="center">共有
<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />
条记录 当前为
<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />
/
<asp:Label id="lblPageCount" ForeColor="red" runat="server" />
页 </td>
<td align=center><!-- 分页显示位置 -->
<span id="pagelist" runat="server"></span> </td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>