小弟是新手,前一阵把以前写的DataGrid旧代码改成了用存储过程的方式分页,主要代码如下
--@PageSize:分页大小,PageIndex:页号,@PageCount:总页数,@recordCount:记录数
CREATE PROCEDURE GetDataPage_Misc @pageSize int, @pageIndex int, @pageCount int output, @recordCount int output AS
declare @SQL varchar(1000)
select @recordCount=count(*) from Misc
set @pageCount=ceiling(@recordCount*1.0/@pageSize)
if @pageIndex = 0 or @pageCount<=1
set @SQL='select top '+str(@pageSize)+' * from Misc order by MID desc'
else if @pageIndex = @pageCount -1
 set @SQL='select * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' * from Misc order by MID asc) TempTable order by MID desc'
else  set @SQL='select top '+str(@pageSize) +' * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' * from Misc order by MID asc) TempTable order by MID desc' 
exec(@SQL)
GO
private DataSet GetPageData(uint pageSize, uint pageIndex)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Conn"]);
        conn.Open();
        SqlCommand command = new SqlCommand("GetDataPage_Misc", conn);  
        command.CommandType = CommandType.StoredProcedure; 
        command.Parameters.Add("@pageSize", SqlDbType.Int);
        command.Parameters["@pageSize"].Value = pageSize;
        command.Parameters.Add("@pageIndex", SqlDbType.Int);
        command.Parameters["@pageIndex"].Value = pageIndex;
        command.Parameters.Add("@pageCount", SqlDbType.Int);
        command.Parameters["@pageCount"].Value = pageCount;
        command.Parameters["@pageCount"].Direction = ParameterDirection.Output;  
        command.Parameters.Add("@recordCount", SqlDbType.Int);
        command.Parameters["@recordCount"].Value = recordCount;
        command.Parameters["@recordCount"].Direction = ParameterDirection.Output; 
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        pageCount = Convert.ToUInt32(command.Parameters["@pageCount"].Value);
        recordCount = Convert.ToUInt32(command.Parameters["@recordCount"].Value);
        conn.Close();
        return ds;
    }

    protected void BindDataGrid()
    {
        DataSet ds = GetPageData((uint)DataGrid1.PageSize, (uint)DataGrid1.CurrentPageIndex);
        DataGrid1.VirtualItemCount = (int)recordCount;
        DataGrid1.DataSource = ds;
        DataGrid1.DataBind();
    }
这段代码已经在工作了,感觉数据量大的时候果然效率高很多
现在有一新的问题需要帮助,在select * from tablename后面需要加一个where sid = 一个名叫sid的变量
这个sid由网页link后带的参数取得——int sid = Convert.ToInt32(Request.QueryString["sid"]);
现在我不知道这个where子句应该加在什么地方,请帮忙指出!
改的时候用颜色区分一下,谢谢!

解决方案 »

  1.   

    其实一句sql代码就可以实现分页了 没有必要这么麻烦
    select top m from tablename where id not in(select top 前面页数*每页数 id from tablename)
      

  2.   


    CREATE PROCEDURE GetDataPage_Misc @pageSize int, @pageIndex int, @sid int, @pageCount int output, @recordCount int output AS
    ......
    --在所有的查询语句中加where sid = str(@sid)......
    command.Parameters.Add("@sid", SqlDbType.Int);
    command.Parameters["@sid"].Value = sid;
    ......
    就这么简单