在读取大量数据的时候我们可以通过DataReader对数据进行分页以提高性能,还有一个更好的方法就是在存储过程中对数据进行分页。

    假设有一个Products表字段有(ProductID,Name,Description, Price)

    以下方法只支持SQLServer 2005 因为ROW_NUMBER()函数是SQLServer 2005新增的。

 1如何在数据层进行分页以提高性能CREATE PROCEDURE GetProducts
 2如何在数据层进行分页以提高性能
 3如何在数据层进行分页以提高性能(@DescriptionLength INT,           --定义参数:描述长度
 4如何在数据层进行分页以提高性能
 5如何在数据层进行分页以提高性能@PageNumber INT,                --页码
 6如何在数据层进行分页以提高性能
 7如何在数据层进行分页以提高性能@ProductsPerPage INT,             --每页产品数 
 8如何在数据层进行分页以提高性能
 9如何在数据层进行分页以提高性能@HowManyProducts INT OUTPUT)  --产品总数
10如何在数据层进行分页以提高性能
11如何在数据层进行分页以提高性能AS
12如何在数据层进行分页以提高性能
13如何在数据层进行分页以提高性能-- 定义一个Table变量
14如何在数据层进行分页以提高性能
15如何在数据层进行分页以提高性能DECLARE @Products TABLE
16如何在数据层进行分页以提高性能
17如何在数据层进行分页以提高性能(RowNumber INT,
18如何在数据层进行分页以提高性能
19如何在数据层进行分页以提高性能 ProductID INT
20如何在数据层进行分页以提高性能
21如何在数据层进行分页以提高性能 Name VARCHAR(50), 
22如何在数据层进行分页以提高性能
23如何在数据层进行分页以提高性能 Description VARCHAR(5000)
24如何在数据层进行分页以提高性能
25如何在数据层进行分页以提高性能Price MONEY)
26如何在数据层进行分页以提高性能
27如何在数据层进行分页以提高性能-- 把数据读到刚定义的@Products 中
28如何在数据层进行分页以提高性能
29如何在数据层进行分页以提高性能INSERT INTO @Products    
30如何在数据层进行分页以提高性能
31如何在数据层进行分页以提高性能SELECT ROW_NUMBER() OVER (ORDER BY Product.ProductID), 
32如何在数据层进行分页以提高性能
33如何在数据层进行分页以提高性能       ProductID, Name, 
34如何在数据层进行分页以提高性能
35如何在数据层进行分页以提高性能       SUBSTRING(Description, 1@DescriptionLength+ '如何在数据层进行分页以提高性能' AS Description, Price,
36如何在数据层进行分页以提高性能
37如何在数据层进行分页以提高性能FROM Product 
38如何在数据层进行分页以提高性能
39如何在数据层进行分页以提高性能-- 返回产品数
40如何在数据层进行分页以提高性能
41如何在数据层进行分页以提高性能SELECT @HowManyProducts = COUNT(ProductID) FROM @Products
42如何在数据层进行分页以提高性能
43如何在数据层进行分页以提高性能-- 返回请求页面的数据
44如何在数据层进行分页以提高性能
45如何在数据层进行分页以提高性能SELECT ProductID, Name, Description, Price
46如何在数据层进行分页以提高性能
47如何在数据层进行分页以提高性能FROM @Products
48如何在数据层进行分页以提高性能
49如何在数据层进行分页以提高性能WHERE RowNumber > (@PageNumber - 1* @ProductsPerPage 
50如何在数据层进行分页以提高性能
51如何在数据层进行分页以提高性能  AND RowNumber <= @PageNumber * @ProductsPerPage
52如何在数据层进行分页以提高性能

 

SQLServer 2000中可以用以下的方法:

 1如何在数据层进行分页以提高性能CREATE PROCEDURE GetProducts
 2如何在数据层进行分页以提高性能
 3如何在数据层进行分页以提高性能(@DescriptionLength INT,           --定义参数:描述长度
 4如何在数据层进行分页以提高性能
 5如何在数据层进行分页以提高性能@PageNumber INT,                --页码
 6如何在数据层进行分页以提高性能
 7如何在数据层进行分页以提高性能@ProductsPerPage INT,             --每页产品数 
 8如何在数据层进行分页以提高性能
 9如何在数据层进行分页以提高性能@HowManyProducts INT OUTPUT)  --产品总数
10如何在数据层进行分页以提高性能
11如何在数据层进行分页以提高性能AS
12如何在数据层进行分页以提高性能
13如何在数据层进行分页以提高性能-- 定义一个Table变量
14如何在数据层进行分页以提高性能
15如何在数据层进行分页以提高性能DECLARE #Products TABLE           --这里一定要用‘#’(声明为本地临时表)
16如何在数据层进行分页以提高性能
17如何在数据层进行分页以提高性能(RowNumber SMALLINT NOT NULL IDENTITY(1,1),    --类型一定要自动递增
18如何在数据层进行分页以提高性能
19如何在数据层进行分页以提高性能 ProductID INT
20如何在数据层进行分页以提高性能
21如何在数据层进行分页以提高性能 Name VARCHAR(50), 
22如何在数据层进行分页以提高性能
23如何在数据层进行分页以提高性能 Description VARCHAR(5000)
24如何在数据层进行分页以提高性能
25如何在数据层进行分页以提高性能Price MONEY)
26如何在数据层进行分页以提高性能
27如何在数据层进行分页以提高性能-- 把数据读到刚定义的#Products 中
28如何在数据层进行分页以提高性能
29如何在数据层进行分页以提高性能INSERT INTO #Products (ProductID, Name, Description, Price)   
30如何在数据层进行分页以提高性能
31如何在数据层进行分页以提高性能SELECT 
32如何在数据层进行分页以提高性能
33如何在数据层进行分页以提高性能       ProductID, Name, 
34如何在数据层进行分页以提高性能
35如何在数据层进行分页以提高性能       SUBSTRING(Description, 1@DescriptionLength+ '如何在数据层进行分页以提高性能' AS Description, Price,
36如何在数据层进行分页以提高性能
37如何在数据层进行分页以提高性能FROM Product 
38如何在数据层进行分页以提高性能
39如何在数据层进行分页以提高性能-- 返回产品数
40如何在数据层进行分页以提高性能
41如何在数据层进行分页以提高性能SELECT @HowManyProducts = COUNT(ProductID) FROM #Products
42如何在数据层进行分页以提高性能
43如何在数据层进行分页以提高性能-- 返回请求页面的数据
44如何在数据层进行分页以提高性能
45如何在数据层进行分页以提高性能SELECT ProductID, Name, Description, Price
46如何在数据层进行分页以提高性能
47如何在数据层进行分页以提高性能FROM #Products
48如何在数据层进行分页以提高性能
49如何在数据层进行分页以提高性能WHERE RowNumber > (@PageNumber - 1* @ProductsPerPage 
50如何在数据层进行分页以提高性能
51如何在数据层进行分页以提高性能  AND RowNumber <= @PageNumber * @ProductsPerPage
52如何在数据层进行分页以提高性能

 

 

大家都清楚了吧,这种方法比在DataReader中速度高效。

 

相关文章:

  • 2021-11-12
  • 2021-10-19
  • 2021-08-05
  • 2021-05-19
  • 2021-08-09
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-27
  • 2022-12-23
  • 2022-01-31
  • 2021-10-19
  • 2021-10-19
  • 2022-03-03
相关资源
相似解决方案