大数据量下的分页

14 篇文章 0 订阅
10 篇文章 0 订阅
大数据量下的分页
郭红俊
select * from orders where orderid between 10248 and 10253
select * from orders where orderid in (10248,10249,10250,10251,10252,10253) order by orderid desc
create table ##temptable(
iid int IDENTITY (1, 1) NOT NULL,
mainid int NOT NULL
)

insert ##temptable(mainid) select OrderID from orders order by OrderID desc

select * from ##temptable

drop 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 desc
declare @PageSize int,@CurrPage int,@strSQL varchar(2000),@IDStr varchar(1000)
select @PageSize = 30
select @CurrPage = 2
select @IDStr = ''
select @IDStr = @IDStr + ltrim(rtrim(str(MainID))) + ',' from ##temptable 
where iid between ((@CurrPage-1)*@PageSize+1) and @CurrPage*@PageSize
if @IDStr <> '' 
begin
 select @IDStr = left(@IDStr,len(@IDStr)-1)
end
select @strSQL = 'select * from orders where OrderID in ('+@IDStr+')  order by OrderID desc '
exec(@strSQL)
drop table ##temptable
declare @PageSize int,@CurrPage int,
@topnum int,@previous int
select @PageSize = 30
select @CurrPage = 2
select @topnum = @CurrPage * @PageSize
select @previous = (@CurrPage - 1) * @PageSize
declare @i int,@IDStr nvarchar(500),@strSQL nvarchar(1000)
select @i = 0
select @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 output
if len(rtrim(ltrim(@IdStr))) > 0
begin
 select @IdStr = left(@IdStr,len(@IdStr)-1)
end
select @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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值