Stored procedure paging

xiaoxiao2021-03-06  89

I haven't been blog for a long time, write a method of returning data based on the Zou Jian's paging stored procedure

Proc Code:

/ * SQL CODE 2004-08-27 * / create procedure dbo.P_splitpage

@SQL NVARCHAR (4000), - To execute the SQL statement @currentpage int, - the page number @PageSize Int, - the size of each page @Recordcount Int = 0 out, - total record @PageCount Int = 0 OUT - Total number AS

Set NoCount on Declare @ P1 Int

EXEC SP_CURSoropen @ p1 output, @ SQL, @ scrollopt = 1, @ ccopt = 1, @ rowcount = @ PageCount Output

SELECT @ RecordCount = @ PageCount, @ PageCount = ceiling (1.0 * @ PageCount / @ PageSize), @ CurrentPage = (@ CurrentPage-1) * @ PageSize 1 SELECT @RecordCount RecordCount, @ PageCount PageCount, @ CurrentPage CurrentPage EXEC SP_Cursorfetch @ P1, 16, @ CurrentPage, @ Pagesize Exec sp_cursorclose @ p1go

Create a table, such as:

Accounts

{ID, Accountname, Fullname, Password ....

Query process SQL statement:

SELECT * ACCOUNTS

Create this statement into a stored procedure:

/ * Proc code 2004-08-27 * / create procedure dbo.p_accounts_select @intpage int, @intpagesize int, @intpageamount int out @intpageamount int outas

Exec P_SPLITPAGE 'SELECT * ACCUNTS', @ INTPAGE, @ INTPAGESIZE, @ INTRECORDCOUNT OUT, @ INTPAGEAMOUNT OUTGO

Through "query analyzer"

Declare @PageAmount Int, @ recordcount intexec p_accounts_select 2, 1, @ pageamount out, @ recordcount out

Will find that the return result set is three, the first result set is empty, the second result set is the total number of data records returned, the number of page numbers cannot be obtained at the same time, but it is not possible to get The record set and the number of page numbers are done twice, using C # DataReader.nextResult can be implemented. The method is as follows:

///

/// Custom Run Stored Procedure. /// /// name of stored procedure. /// Stored Procedure Params. /// Return Result of procedure. /// DataReader nextResult /////// dataReader RecordCount /// dataReader PageCount public void RunProc (string procName, SqlParameter [] prams, out SqlDataReader dataReader, out int RecordCount , out int PageCount) {int _iRecordCount = 0; int _iPageCount = 0; SqlCommand cmd = CreateCommand (procName, prams); dataReader = cmd.ExecuteReader (System.Data.CommandBehavior.CloseConnection);

. // Return Next Result, Because Proc Return More Data Result dataReader.NextResult (); while (dataReader.Read ()) {_iRecordCount = (int) dataReader [ "RecordCount"]; _iPageCount = (int) dataReader [ "PageCount" ]; RECORDCOUNT = _iRecordcount;

DataReader.nextResult ();

This allows data binding without DataSet, with DataReader, and uses the stored procedure paging, a lot.

2004-08-29

转载请注明原文地址:https://www.9cbs.com/read-122296.html

New Post(0)