百万条数据分页

写出代码之前,先说明一下原理,比较简单。有一张表(test)如下 
结构是:id(自动编号) txt   假设40条记录)
现在要每页显示10条记录,则每页要显示的数据应该是:
第一页:1----10
第二页:11----20
第三页:21----30
第四页:31----40

如要显示第一页,最简单的方法就是 select top 10 * from test 就OK了。

第二页开始呢?怎么做呢?请接着看:
比如我要显示第三页:也就是21----30
原理:找出不要的数据也就是1----20,最大的id,这是里20
再找出大于这个id(20) 前10条记录就OK了。

原理知道后写代码就简单了,前台界面比较简单,不多说,代码如下

None.gif < asp:LinkButton  id ="lbtnFirst"  Runat ="server" > 首頁 </ asp:LinkButton >
None.gif
< asp:LinkButton  id ="lbtnBack"  Runat ="server" > 上頁 </ asp:LinkButton >
None.gif
< asp:LinkButton  id ="lbtnNext"  Runat ="server" > 下頁 </ asp:LinkButton >
None.gif
< asp:LinkButton  id ="lbtnLast"  Runat ="server" > 尾頁 </ asp:LinkButton >
None.gif
< asp:Label  id ="Label1"  runat ="server" > 当前页: </ asp:Label >
None.gif
< asp:Label  id ="lblCurrentPage"  runat ="server" > 1 </ asp:Label >
None.gif
< asp:Label  id ="Label2"  runat ="server" > 总页: </ asp:Label >
None.gif
< asp:Label  id ="lblPageCount"  runat ="server" > 200 </ asp:Label >
None.gif
< asp:Label  id ="Label3"  runat ="server" > 跳转: </ asp:Label >
None.gif
< asp:TextBox  id ="txtToPage"  runat ="server"  Width ="88px" ></ asp:TextBox >
None.gif
< asp:Button  id ="btnToPage"  runat ="server"  Text ="go" ></ asp:Button >
None.gif
< asp:DataGrid  id ="DataGrid1"  runat ="server"  AllowPaging ="True"  AllowCustomPaging ="True" ></ asp:DataGrid >

后台代码:

 

None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//开始显示第一页
InBlock.gif
    ShowDate(Convert.ToInt64(lblCurrentPage.Text));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// <summary>
InBlock.gif  
/// 显示数据
InBlock.gif  
/// </summary>
ExpandedBlockEnd.gif  
/// <param name="page"></param>

None.gif    private   void  ShowDate( long  page)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif        
InBlock.gif           
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//*-------------------设置参数------------------*/
InBlock.gif
InBlock.gif   
string tblName  = "test";
InBlock.gif   
string fldName  = "id";
InBlock.gif   
string orderStr = "asc";
InBlock.gif   
int  PageSize = 10;
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//*-------------------设置结束------------------*/
InBlock.gif
InBlock.gif   
byte[] t =Convert.FromBase64String(System.Configuration.ConfigurationSettings.AppSettings["conStr"]);
InBlock.gif
InBlock.gif   
string conStr = System.Text.ASCIIEncoding.ASCII.GetString(t);
InBlock.gif
InBlock.gif
InBlock.gif   SqlConnection conn 
= new SqlConnection(conStr);
InBlock.gif   conn.Open();
InBlock.gif   SqlCommand cmd 
= new SqlCommand("select count(*) from " + tblName,conn);
InBlock.gif   lblPageCount.Text 
= Convert.ToString(((int)cmd.ExecuteScalar() / PageSize + 1));
InBlock.gif   DataGrid1.VirtualItemCount 
= Convert.ToInt32(lblPageCount.Text);//datadrid每次就显示一页,所有要手动加上总页
InBlock.gif
   
InBlock.gif   
string sql = string.Empty;
InBlock.gif
InBlock.gif   sql 
= "select top " + Convert.ToString((page - 1* PageSize) + " " + fldName + " from " + tblName +  " order by " + fldName + " " + orderStr; //排除的记录部分
InBlock.gif
   sql = "select max (" + fldName + ") from ( " + sql + " ) as t"//得到排除记录里的最大ID号
InBlock.gif
   sql = "select top " + PageSize.ToString() + " * from " + tblName + " where " + fldName + ">(" + sql + ") order by " + fldName + " " + orderStr;
InBlock.gif   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
if (page == 1)dot.gif{sql = "select top " + PageSize + " * from " + tblName;lblCurrentPage.Text = "1";}
InBlock.gif
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    SqlDataAdapter da 
= new SqlDataAdapter(sql,conn);
InBlock.gif    System.Data.DataSet ds 
= new DataSet();
InBlock.gif    da.Fill(ds);
InBlock.gif    DataGrid1.DataSource 
= ds.Tables[0].DefaultView;
InBlock.gif    DataGrid1.DataBind();  
InBlock.gif
InBlock.gif    conn.Close();
InBlock.gif
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch ( Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Response.Write(ex.Message.ToString());
ExpandedSubBlockEnd.gif   }

InBlock.gif   
ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// 首页
None.gif    private   void  lbtnFirst_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   lblCurrentPage.Text 
= "1";
InBlock.gif   ShowDate(
1);
ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// 上页
None.gif    private   void  lbtnBack_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if (lblCurrentPage.Text != "1")
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    lblCurrentPage.Text 
= Convert.ToString(Convert.ToInt64(lblCurrentPage.Text) - 1);
InBlock.gif    ShowDate(Convert.ToInt64(lblCurrentPage.Text));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// 下页
None.gif    private   void  lbtnNext_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if (lblCurrentPage.Text != lblPageCount.Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    lblCurrentPage.Text 
= Convert.ToString(Convert.ToInt64(lblCurrentPage.Text) + 1);
InBlock.gif    ShowDate(Convert.ToInt64(lblCurrentPage.Text));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// 尾页
None.gif    private   void  lbtnLast_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   lblCurrentPage.Text 
= lblPageCount.Text;
InBlock.gif   ShowDate(Convert.ToInt64(lblPageCount.Text));
ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// 跳转
None.gif    private   void  btnToPage_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if ( Convert.ToInt64(txtToPage.Text.Trim()) > 0 && Convert.ToInt64(txtToPage.Text.Trim()) < Convert.ToInt64(lblPageCount.Text))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    lblCurrentPage.Text 
= txtToPage.Text;
InBlock.gif    ShowDate(Convert.ToInt64(txtToPage.Text));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**/ /// dataGrid单击的页数
None.gif    private   void  DataGrid1_PageIndexChanged( object  source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   DataGrid1.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif   lblCurrentPage.Text 
= Convert.ToString(e.NewPageIndex + 1);
InBlock.gif   ShowDate(e.NewPageIndex 
+ 1);
ExpandedBlockEnd.gif  }

None.gif

 

亲自测试sql2000 460万条记录,显示速度很快

转载于:https://www.cnblogs.com/lxxnet/archive/2006/08/07/470215.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值