设为首页
加入收藏
联系我们
首页 基础教程 技术文档 实例分析 数 据 库 疑难杂症 ASP.NET 七夕许愿树 技术论坛
同学网,基于同学关系的真实社区
<%-- 最新动态 Begin --%> <%-- 最新动态 End --%>
最新文章
 C#摄像头编程实例
 C#下用P2P技术实现点…
 Asp.net(c#)数…
 SQL Server数据…
 .NET牛人应该知道些什…
 NET牛人应该知道些什么
 SQL Server S…
 备份指定表到另一数据库
 SQLSERVER中快速…
 ASP.NET中url传…
 ASP.NET自动给UR…
 ASP.NET 2.0防…
 ASP.NET 2.0 …
 如何解决ASP.net中…
 sql server 与…
<%-- 最新动态 Begin --%> <%-- 最新动态 End --%>
推荐文章
 关于C#中的REF和黓认…
 读书笔记c#高级编程 委…
 【算法】C#快速排序类
 Visual C#的SQ…
 C#中调用API
 Infragistics…
 C#接口转换
 C#读取设备信息
 用.net操作word
 C# MessageBo…
 Visual C#中的数…
 雅虎公司C#笔试题,看看…
 C#.NET使用NHib…
 .net学习之运算符重载…
 Visual C# 3.…
<%-- 最新动态 Begin --%> <%-- 最新动态 End --%>
热门文章
 ADO.Net与ADO在…
 开发ASP.NET下的M…
 用C#+XMI技术进行U…
 什么是虚拟机?
 C#基础全接触
 C#学习第一天
 雅虎公司C#笔试题,看看…
 C#语言初级入门(1)
 C#中利用正则表达式实现…
 远程重启计算机(C#)
 用.net操作word
 什么是B/S三层?
 VB和C# 语法对比图 …
 Visual C#常用函…
 Visual C#的SQ…
大数据量的分页
阅读正文 文字大小:增大 减小  文字行距:增大 减小   双击自动滚屏
本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

方案一:维护一个表,这个表记录需要显示的这些编号排序顺序。(这个表可以是临时表,也可以是物理表)。下面演示了利用一个全局临时表。这个全局临时表记录需要显示的编号。注意排序,这里的order by 就是需要显示的排序顺序。

create table ##temptable(iid int IDENTITY (1, 1) NOT NULL,mainid int NOT NULL)insert ##temptable(mainid) select OrderID from orders order by OrderID descselect * from ##temptabledrop table ##temptable -- 实际执行时候,删除全部临时表当然不再这里执行。

这个临时表存在,获得指定分页的分块数据就很简单了。看下面代码:

create table ##temptable(iid int IDENTITY (1, 1) NOT NULL,mainid int NOT NULL)insert ##temptable(mainid) select OrderID from orders order by OrderID descdeclare @PageSize int,@CurrPage int,@strSQL varchar(2000),@IDStr varchar(1000)select @PageSize = 30select @CurrPage = 2select @IDStr = ''select @IDStr = @IDStr + ltrim(rtrim(str(MainID))) + ',' from ##temptable where iid between ((@CurrPage-1)*@PageSize+1) and @CurrPage*@PageSizeif @IDStr <> '' begin select @IDStr = left(@IDStr,len(@IDStr)-1)endselect @strSQL = 'select * from orders where OrderID in ('+@IDStr+')  order by OrderID desc 'exec(@strSQL)drop table ##temptable

注意:实际使用这个方案的时候,还要考虑何时更新这个全局临时表,一般是放到计划任务中,定时更新这个汇总表。

方案二:每次都去查询,每次获得最新的编号顺序。由于这时候不存在这个临时表,书写获得需要显示页面的编号的字符串就需要点技巧,看下面的代码:ASP.net 的 DataGrid 提供了使用这种分区的数据的方法。 DataGrid 通过 AllowCustomPaging 和 VirtualItemCount 属性支持块区操作。如果 AllowCustomPaging 为 true,则 DataGrid 不会根据 CurrentPageIndex 计算数据模型中的起始显示位置。DataGrid 将显示数据模型中的所有数据,而页导航栏将当前位置报告为 (VirtualItemCount+PageSize-1)/PageSize 之 CurrentPageIndex 页。下面的示例说明此功能。

declare @PageSize int,@CurrPage int,@topnum int,@previous intselect @PageSize = 30select @CurrPage = 2select @topnum = @CurrPage * @PageSizeselect @previous = (@CurrPage - 1) * @PageSizedeclare @i int,@IDStr nvarchar(500),@strSQL nvarchar(1000)select @i = 0select @strSQL = N''select @strSQL = @strSQL + N' select top '+str(@topnum)+ ' @i = @i + 1 'select @strSQL = @strSQL + N',  @IdStr = 'select @strSQL = @strSQL + N'case when @i > '+str(@previous)+' then  @IdStr + ltrim(rtrim(str(OrderID))) + '','' 'select @strSQL = @strSQL + N'else N''''end 'select @strSQL = @strSQL + N'from Orders 'select @strSQL = ltrim(rtrim(@strSQL)) + N' order by OrderID desc 'Select @IdStr = N''exec sp_executesql @strSQL,N'@i int,@IdStr varchar(500) output',@i,@IdStr outputif len(rtrim(ltrim(@IdStr))) > 0begin select @IdStr = left(@IdStr,len(@IdStr)-1)endselect @strSQL = 'select * from orders where OrderID in ('+@IDStr+')'exec(@strSQL)

 

     protected void BindDataGrid(int currpage) {  string strConn = "Data Source=(local);Integrated Security=SSPI;database=Northwind";  // 请确认 机器名/ASPNET 用户可以访问Northwind数据库  SqlCommand cmd = new SqlCommand();  SqlConnection conn = new SqlConnection(strConn);  SqlParameter[]  parms = new SqlParameter[] {   new SqlParameter("@PageSize",SqlDbType.Int),   new SqlParameter("@CurrPage",SqlDbType.Int),   new SqlParameter("@SearchSql",SqlDbType.NVarChar,128),   new SqlParameter("@Count",SqlDbType.Int),  };  parms[0].Value = DataGrid1.PageSize;  parms[1].Value = (currpage+1);   //  数据库的分页算法第一页是1  DataGrid的第一页是0  parms[2].Value = DBNull.Value;  parms[3].Direction = ParameterDirection.Output;  parms[3].Value = DBNull.Value;  DataSet DS = new DataSet();  try   {   if (conn.State != ConnectionState.Open) conn.Open();   cmd.Connection = conn;   cmd.CommandText = "Selected_Page_List";   cmd.CommandType = CommandType.StoredProcedure;   if (parms != null)    {    foreach (SqlParameter parm in parms)     cmd.Parameters.Add(parm);   }   SqlDataAdapter DA = new SqlDataAdapter(cmd);   DA.Fill(DS);   int aa = Convert.ToInt32(parms[3].Value.ToString());   cmd.Parameters.Clear();   if (currpage == 0)   {    DataGrid1.VirtualItemCount = aa;   }   DataGrid1.CurrentPageIndex = currpage;   DataGrid1.DataSource = DS;   DataGrid1.DataBind();  }  catch(Exception ewx)  {   conn.Close();   Response.Write (ewx.Message.ToString());   Response.End();  } }    void Page_Load(Object sender, EventArgs E ) {  if (!IsPostBack)   {   BindDataGrid(0);   // 第一次打开这个页面,访问分页的第一页  }    }    void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e) {  BindDataGrid(e.NewPageIndex);    }

 



本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

中国C#技术交流QQ群:6337034  10976424  9383681  35248582  35248645
版权所有:中国C#技术学习中心 Copyright ? 2006-2008
建议浏览分辨率使用:1024*768分辨率
粤ICP备05002251号