Asp.net data access layer Stored Procedure paging statement

Source: Internet
Author: User

Therefore, it is best to use the Stored Procedure for paging at the data access layer. the following uses the employee table in the pubs database as an example to store data paging. You can refer to it to create your own stored procedure based on your actual situation.

Note: @ pageindex refers to the index of the data page, @ dataperpage refers to the number of records on each page, and @ howmanyrecords is used to obtain the total number of records.
Copy codeThe Code is 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 (order by 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 (order by 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

The Row_number function can number each retrieved record in order.

Next, you can call the stored procedure in the background code of the asp.net webpage to obtain the desired data.

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.