欢迎光临
我们一直在努力

C#和SQL数据浏览分页-.NET教程,C#语言

建站超值云服务器,限时71元/月

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语句,是希望我和我的好朋友们的劳动成果已经用于商业用途了,还有就是希望各位自己动手写写,应该没有问题的了

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#和SQL数据浏览分页-.NET教程,C#语言
分享到: 更多 (0)