SQL Server paging stored procedure with input and output parameters (most efficient)

Source: Internet
Author: User

CREATE PROCEDURE Proc_page_withtopmax (@pageIndexint,--Page Index @pageSizeint,--number of displays per page @pageCountintOutput,--total pages, output parameters @totalCountintoutput--total number of articles) asbeginSetnocount on;declare @sql nvarchar ( +)Set@sql ='Select top * from Tb_testtable where (the id> (select Max (ID) from (select Top'+str (@pageIndex-1) * @pageSize) +'ID from tb_testtable ORDER by ID) (as temp)) Order by ID'Execute (@sql) declare @sqlRecordCount nvarchar ( +) --The statement that gets the total number of recordsSet@sqlRecordCount =n'Select @recordCount =count (*) from tb_testtable'Declare @recordCountint--variable that holds the total number of records exec sp_executesql @sqlRecordCount, N'@recordCount int Output', @recordCount outputif(@recordCount% @pageSize =0) --if the total number of records can be divisible by the page sizeSet@pageCount = @recordCount/@pageSize--The total number of pages is equal to the total record bar divided by the page sizeElse--if the total number of records cannot be divisible by the page sizeSet@pageCount = @recordCount/@pageSize +1--The total number of pages is equal to the total record bar divided by the page size plus 1Set@totalCount =@recordCountSetnocount Off;end--the stored procedure is executed in the database declare @pageCountint, @totalCountintexec Proc_page_withtopmax2,95955, @pageCount output, @totalCount outputSelect 'Total Pages:'+Str (@pageCount)Select 'Total number of bars:'+str (@totalCount)

C # code calls the paging stored procedure with input and output parameters

   Public StaticDataSet Getrecordbypage (intPageSize,intPageIndex, out intPageCount, out inttotalcount) {DataSet DS=NewDataSet (); Try            {                using(SqlConnection conn =NewSqlConnection (@"server=;d atabase=data_test;uid=; pwd=;") ) {SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; Cmd. Parameters.Add (NewSqlParameter ("@pageSize", SqlDbType.Int)); Cmd. Parameters.Add (NewSqlParameter ("@pageIndex", SqlDbType.Int)); SqlParameter param=NewSqlParameter ("@totalCount", SqlDbType.Int); Param. Direction=ParameterDirection.Output; Cmd.                    Parameters.Add (param); SqlParameter param1=NewSqlParameter ("@pageCount", SqlDbType.Int); param1. Direction=ParameterDirection.Output; Cmd.                    Parameters.Add (param1); Cmd. parameters[0]. Value =pageSize; Cmd. parameters[1]. Value =PageIndex; Cmd. parameters[2]. Value =0; Cmd. parameters[3]. Value =0; Cmd.commandtype=CommandType.StoredProcedure; Cmd.commandtext="Proc_page_withtopmax"; Cmd.commandtimeout= the; SqlDataAdapter Adapter=NewSqlDataAdapter (); Adapter. SelectCommand=cmd; DataSet Source=NewDataSet (); Adapter.                                     Fill (DS); Objecto = cmd. parameters["@totalCount"].                    Value; TotalCount= (O = =NULL|| o = = DBNull.Value)?0: System.Convert.ToInt32 (o); Objectb = cmd. parameters["@pageCount"].                    Value; PageCount= (b = =NULL|| o = = DBNull.Value)?0: System.Convert.ToInt32 (b); }            }            Catch(SqlException e) {Throwe; }            returnds; }    }

SQL Server paging stored procedure with input and output parameters (most efficient)

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.