技术文章 > C#和SQL数据浏览分页

C#和SQL数据浏览分页

2018-04-26 17:42

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:

作者:19811104的 Blog
“>C#和SQL数据浏览分页
如果需要考虑如时间的过滤、其他条件的加入,可以在SQL语句进行编辑,普通的网站,下面的数据浏览分页
就可以了。
aspx代码:
<%@ Page language=“c#“ Codebehind=“StockOrderFormBrower.aspx.cs“ AutoEventWireup=“false“ Inherits=“GSP.StockOrderFormBrower“ %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN“ >
<HTML>
<HEAD>
<title>
用C#和SQL结合进行数据浏览分页
</title>
<LINK href=“css/main.css“ type=“text/css“ rel=“stylesheet“>
<meta http-equiv=“Content-Type“ content=“text/html; charset=GB2312“>
<meta content=“Microsoft Visual Studio .NET 7.1“ name=“GENERATOR“>
<meta content=“C#“ name=“CODE_LANGUAGE“>
<meta content=“JavaScript“ name=“vs_defaultClientScript“>
</HEAD>

<body MS_POSITIONING=“GridLayout“>
<form id=“form1“ method=“post“ runat=“server“>
<table id=“ShowData“ cellSpacing=“0“ cellPadding=“0“ align=“center“ border=“0“>
<%ShowData();%><!--输出数据-->
</table>
<table align=“right“>
<tr>
<td>
<%PageLoad_Count();%>
<INPUT id=“first“ type=“button“ value=“ |< “ name=“first“ runat=“server“><!--第一页-->
<INPUT id=“prior“ type=“button“ value=“ < “ name=“prior“ runat=“server“><!--上一页-->
<INPUT id=“next“ type=“button“ value=“ > “ name=“next“ runat=“server“><!--下一页-->
<INPUT id=“last“ type=“button“ value=“ >| “ name=“last“ runat=“server“><!--最后一页-->
</td>
</tr>
</table>
</form>
</body>
</HTML>
aspx.cs代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace ASWBLM
{
/// <summary>
///
/// </summary>
public class UnionInfo : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton first;
protected System.Web.UI.HtmlControls.HtmlInputButton prior;
protected System.Web.UI.HtmlControls.HtmlInputButton last;
protected System.Web.UI.HtmlControls.HtmlInputButton next;

protected static int CurrentPage = 1;//初始化开始页面
protected static int RowCount = 0 ;//本页有多少条
private static bool IsPrior = false;//有“前一页”
private static bool IsNext = false;//有“下一页”
private static bool IsLast = false;//有“最后一页”
protected static int not_shown_records=0;//计算未显示记录数
private static string startID = ““;//设置上一页开始ID
private static string endID = ““;//设置下一页结束ID
private static int page_count = 10;//初始化页面记录数

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
this.CountRecord().ToString();// 记录总数
this.Page_Count().ToString();//分页总数
Init_Brower();//初始化浏览
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.first.ServerClick += new System.EventHandler(this.first_ServerClick);
this.prior.ServerClick += new System.EventHandler(this.prior_ServerClick);
this.next.ServerClick += new System.EventHandler(this.next_ServerClick);
this.last.ServerClick += new System.EventHandler(this.last_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion


/// <summary>
/// 显示数据
/// </summary>
protected void ShowData()
{
DataSet ds = new DataSet();//数据集
ASWBLM.Include.UnionInfo_Provider _uip = new ASWBLM.Include.UnionInfo_Provider();
string vSQL = ““;
vSQL = GetSQLCommond(vSQL,startID,endID);

ds = _uip.ShowAllUnionInfo(vSQL);//取得全部数据的数据集
try
{
Response.Write(“<p align=center>“);
foreach(DataRow dr in ds.Tables[“Table“].Rows)
{
Response.Write(“<tr align = center onmouseover = \“this.bgColor = ““““#cccccc““““\“ onmouseout = \“this.bgColor=““““““““;\“>“);

Response.Write(“<td align=\“left\“ width=\“60%\“><font color=\“#00309C\“>“);
Response.Write(“<a href=\“UnionInfo_Read.aspx?id=“+dr[“Id“].ToString()+“\“ target=\“_self\“>“);
Response.Write(dr[“Title“].ToString());
Response.Write(“</a>“);
Response.Write(“</td>“);
Response.Write(“<td align=\“right\“>“);
Response.Write(“<font color=\“#999999\“>“);
Response.Write(“( “+dr[“SummaryDateTime“].ToString()+“ )“);
Response.Write(“ ( 已阅读“+dr[“ReadTimes“].ToString()+“次 )“);
Response.Write(“</font>“);
Response.Write(“</td>“);
Response.Write(“</tr>“);
}
Response.Write(“</p>“);
startID = ds.Tables[“Table“].Rows[0].ItemArray[0].ToString(); //通过数组,取第一个数据,得到开始号“startID”
RowCount = ds.Tables[“Table“].DefaultView.Count;//得到表的行数
endID = ds.Tables[“Table“].Rows[RowCount-1].ItemArray[0].ToString();//通过数组,取最后一个数据,得到结束号“endID”
}
catch(SqlException e)
{
Response.Write(e.Message);
}
}

/// <summary>
/// 计算未显示记录数
/// </summary>
/// <returns></returns>
protected void NotShownRecords()
{
not_shown_records = this.CountRecord()/*查询总记录数*/ - (CurrentPage/*当前页*/ - 1) * page_count/*每页记录数*/;
}

/// <summary>
/// 进行输出信息
/// </summary>
protected void PageLoad_Count()
{
this.NotShownRecords();
Response.Write(“总共“+this.CountRecord()+“条记录 “);
Response.Write(“共有“+this.Page_Count()+“页 “);
Response.Write(“第“+CurrentPage.ToString()+“页 “);
Response.Write(“本页共有“+RowCount.ToString()+“条记录 “);
}

/// <summary>
/// 获得总记录总数
/// </summary>
/// <returns>时间条件范围内记录总数intCount</returns>
protected int CountRecord()
{
int intCount = 0;
SqlConnection SqlCon = new SqlConnection(Common._DBConnStr);
SqlCon.Open ();

//找到条件范围内的记录总数
string strCount = “select count(*) from UnionInfo“;

//找到符合条件的第一个记录
//string strNum = “select top 1 Id from UnionInfo“;
SqlCommand MyComm = new SqlCommand(strCount,SqlCon);
SqlDataReader dr = MyComm.ExecuteReader();//读取数据流
if(dr.Read())
{
  intCount = Int32.Parse(dr[0].ToString());
}
else
{
  intCount = 0;
}
dr.Close();
SqlCon.Close();
return intCount;
}

/// <summary>
/// 总分页数
/// </summary>
/// <returns>分页总数</returns>
protected int Page_Count()
{
int pageSum = 0;//分页总数
pageSum = this.CountRecord() / page_count; ///记录总数/分页的页数
if ((this.CountRecord() % page_count) > 0) pageSum++;
return pageSum;
}

/// <summary>
/// 取得SQL语句
/// </summary>
/// <param name=“vCmd“>返回命令行</param>
/// <returns></returns>
private string GetSQLCommond(string vCommond,string startID,string endID)
{
this.NotShownRecords();//执行未显示的行
vCommond = “SELECT TOP “+page_count+“ {0},{1},{2},{3} FROM [UnionInfo]“;

if(IsPrior)//判断“上一页”
{

}

if(IsNext)//判断“下一页”
{
}
if (IsLast)//判断“最后一页”
{
}
vCommond = string.Format(vCommond,“Id“,“Title“,“SummaryDateTime“,“ReadTimes“);//这个是数据表的字段
return vCommond;
}

/// <summary>
/// 输入按钮的状态,进行是否可用
/// </summary>
/// <param name=“first“>第一页的状态</param>
/// <param name=“prior“>上一页的状态</param>
/// <param name=“next1“>下一页的状态</param>
/// <param name=“last“>最后一页的状态</param>
protected void SetButtonState(bool first_,bool prior_,bool next_,bool last_)
{
if (CurrentPage==1)//到“第一页”
{
first.Disabled = true;//第一页状态
prior.Disabled = true;//上一页状态
next.Disabled = false; //下一页状态
last.Disabled = false; //最后一页状态
}
else if (CurrentPage==this.Page_Count())//到“最后一页”
{
first.Disabled = false;//第一页状态
prior.Disabled = false;//上一页状态
next.Disabled = true; //下一页状态
last.Disabled = true; //最后一页状态
}
else
{
first.Disabled = first_;//第一页状态
prior.Disabled = prior_;//上一页状态
next.Disabled = next_; //下一页状态
last.Disabled = last_; //最后一页状态
}
}

/// <summary>
/// 第一页按钮
/// </summary>
/// <param name=“sender“></param>
/// <param name=“e“></param>
private void first_ServerClick(object sender, System.EventArgs e)
{
CurrentPage = 1;
this.SetButtonState(true,true,false,false);
startID = ““;
endID = ““;
RowCount = ““““0““““;
IsLast = false;
IsPrior = false;
IsNext = false;
}

/// <summary>
/// 上一页按钮
/// </summary>
/// <param name=“sender“></param>
/// <param name=“e“></param>
private void prior_ServerClick(object sender, System.EventArgs e)
{
if( CurrentPage == 1)//判断“当前页”是否为1
{
this.SetButtonState(true,true,false,false);
}
else
{
CurrentPage=CurrentPage - 1;//“当前页”自减
this.SetButtonState(false,false,false,false);
}
IsPrior = true;
IsNext = false;
IsLast = false;
}

/// <summary>
/// 最后一页
/// </summary>
/// <param name=“sender“></param>
/// <param name=“e“></param>
private void last_ServerClick(object sender, System.EventArgs e)
{
CurrentPage=this.Page_Count();//到最后一页
this.SetButtonState(false,false,true,true);
IsLast = true;
IsPrior = false;
IsNext = false;
}

/// <summary>
/// 下一页
/// </summary>
/// <param name=“sender“></param>
/// <param name=“e“></param>
private void next_ServerClick(object sender, System.EventArgs e)
{
if(CurrentPage == this.Page_Count())//判断“当前页”是否为“分页总数”
{
this.SetButtonState(false,false,true,true);
}
else
{
CurrentPage=CurrentPage + 1;//“当前页”自加
this.SetButtonState(false,false,false,false);
}
IsNext = true;
IsLast = false;
IsPrior = false;
}

/// <summary>
/// 初始浏览按钮
/// </summary>
/// <param name=“sender“></param>
/// <param name=“e“></param>
private void Init_Brower()
{
CurrentPage = 1;//肯定是从第一页开始
if ((CurrentPage == 1) && (this.Page_Count() == 1))
{
first.Disabled = true;//第一页状态
prior.Disabled = true;//上一页状态
next.Disabled = true;//下一页状态
last.Disabled = true; //最后一页状态
}
else
{
first.Disabled = true;//第一页状态
prior.Disabled = true;//上一页状态
next.Disabled = false;//下一页状态
last.Disabled = false; //最后一页状态
}
startID = ““;//开始号
endID = ““;//结束号
IsLast = false;
IsPrior = false;
IsNext = false;
}
}
}
本文没有列出SQL语句,是希望我和我的好朋友们的劳动成果已经用于商业用途了,还有就是希望各位自己动手写写,应该没有问题的了