asp.net data access layer Stored procedure paging statements _ Practical Tips

Source: Internet
Author: User
So it's best to page through the data, if this is the use of stored procedures to pagination. The following is a stored procedure for data paging using the employee table in the pubs database, which you can refer to to create your own stored procedures based on the actual situation.

Note: @pageindex the index of the data page, @dataperpage the number of records per page, @howmanyrecords to get the total number of records.
Copy Code code as follows:

create proc GetData @pageindex int, @dataperpage int, @howmanyrecords int output
As
DECLARE @temptable table
(
rowindex int,
emp_id Char (9),
fname varchar (20),
Minit char (1),
lname varchar (30)
)
INSERT INTO @temptable
Select Row_number () over (emp_id) as Rowindex,emp_id,fname,minit,lname
From employee
Select @howmanyrecords =count (rowindex) from @temptable
SELECT * FROM @temptable
where Rowindex> (@pageindex-1) * @dataperpage
and rowindex<= @pageindex * @dataperpage

DECLARE @howmanyrecords int
exec getdata 2,5, @howmanyrecords output
Select @howmanyrecords
declare @x int, @y int, @z int
Select @x = 1, @y = 2, @z=3
Select @x,@y,@z

create proc getdata2 @pageindex int, @dataperpage int, @howmanyrecords int output
As
DECLARE @temptable table
(
rowindex int,
emp_id Char (9),
fname varchar (20),
Minit char (1),
lname varchar (30)
)
INSERT INTO @temptable
Select Row_number () over (emp_id) as Rowindex,emp_id,fname,minit,lname
From employee
Select @howmanyrecords =count (rowindex) from @temptable
SELECT * FROM @temptable
where Rowindex> (@pageindex-1) * @dataperpage
and rowindex<= @pageindex * @dataperpage

Where the Row_number function can number each retrieved record by sort.

You can then call the stored procedure in the ASP.net Web page background code to get the data you want.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.