使用临时表配合ObjectDataSource进行分页

TempTablePaging_ObjectDataSource.aspx

        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="LoadAllProduct" TypeName="ProductBLL" DataObjectTypeName="Product"

        EnablePaging="True" MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex" SelectCountMethod="CountAll" SortParameterName="sortedBy"

        ></asp:ObjectDataSource>

   

 

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Web;

public class ProductBLL

{

    protected int _count = -1;

    public ProductBLL()

     {    }

 

    public List<Product> LoadAllProduct(int startIndex, int maxRows, string sortedBy)

    {

        List<Product> products = new List<Product>();

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

           

    string commandText = @"

     -- 为分页建立一张临时表

    CREATE TABLE #TempPageTable

    (

        IndexId int IDENTITY (0, 1) NOT NULL,

         id int   

     )

 

    -- 读取数据插入临时表

    INSERT INTO #TempPageTable

     (

         [id]

     )

     SELECT

         [Productid]   

     FROM Products";

 

    if (sortedBy != "")

        {     commandText += " ORDER BY " + sortedBy;        }

         commandText += @" SET @totalRecords = @@ROWCOUNT

      

    SELECT

         src.[ProductID],

         src.[ProductName],

         src.[CategoryID],

         src.[Price],

        src.[InStore],

        src.[Description]  

     FROM Products src, #TempPageTable p

     WHERE 

         src.[productid] = p.[id] AND

        p.IndexId >= @StartIndex AND p.IndexId < (@startIndex + @maxRows)";

        

        if (sortedBy != "") {

              commandText += " ORDER BY " + sortedBy;

         }

 

         SqlCommand command = new SqlCommand(commandText, conn);

         command.Parameters.Add(new SqlParameter("@startIndex", startIndex));

         command.Parameters.Add(new SqlParameter("@maxRows", maxRows));

         command.Parameters.Add(new SqlParameter("@totalRecords", SqlDbType.Int));

         command.Parameters["@totalRecords"].Direction = ParameterDirection.Output;

 

         conn.Open();

         SqlDataReader dr = command.ExecuteReader();

         while (dr.Read()) {

              Product prod = new Product();

 

              prod.ProductID = (int)dr["ProductID"];

              prod.ProductName= (string)dr["ProductName"];

            prod.CategoryID = (int)dr["CategoryID"];

              prod.Price = (decimal)dr["price"];

            prod.InStore=(Int16)dr["InStore"];

            prod.Description=(String)dr["Description"];

              products.Add(prod);

         }

 

         dr.Close();

         conn.Close();

         _count = (int)command.Parameters["@totalRecords"].Value;

         return products;

     }

 

    public int CountAll()

    {        return _count;    }

 

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值