使用 WPF 和使用存储过程进行分页

Posted

技术标签:

【中文标题】使用 WPF 和使用存储过程进行分页【英文标题】:Paging using WPF and using stored procedure 【发布时间】:2015-11-18 23:09:01 【问题描述】:

分页控件用于 WPF 应用程序中。但是分页控件是在 WCF 应用程序引用存储过程之后在存储过程中创建的。

【问题讨论】:

请告诉我答案 【参考方案1】:

我在存储过程中使用了分页控制。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create procedure [dbo].[usp_Employee]
(
    /* Properties*/
    @EmployeeID INT=null,
    @EmployeeName nvarchar(50)=null,
    @EmployeeAddress nvarchar(50)=null,
    @EmployeePhoneNo nvarchar(50)=null,
    @Action varchar(10)='ACTION',

    /*Paging Parameter */
    @PageNumber INT=1,
    @PageSize INT=5,

    /*Sorting Parameter */
    @SortColumn nvarchar(20)='Title',
    @SortOrder nvarchar(10)=''
 )

AS
BEGIN
    /* Declaring Local variables corresponding to parameter for modifications*/
    DECLARE
    @lEmployeeID INT,
    @lEmployeeName nvarchar(50),
    @lEmployeeAddress nvarchar(50),
    @lEmployeePhoneNo nvarchar(50),

    @lPageNumber INT,
    @lPageSize INT,
    @lSortColumn NVARCHAR(20),
    @lFirstRecord INT,
    @lLastRecord INT,
    @lTotalRows INT

SET NOCOUNT ON;

  IF @Action = 'INSERT'
    BEGIN 
        INSERT INTO utb_Employee(EmployeeName,EmployeeAddress,EmployeePhoneNo)
         VALUES (@EmployeeName,@EmployeeAddress,@EmployeePhoneNo)      
   END

   IF @Action = 'UPDATE'                                                                                                                                                                                                                                                                                                                                                                              
     BEGIN 
         UPDATE utb_Employee
         SET EmployeeName=@EmployeeName,EmployeeAddress=@EmployeeAddress,EmployeePhoneNo=@EmployeePhoneNo 
         WHERE EmployeeID=@EmployeeID
     END          

   IF @Action = 'SELECT'
     BEGIN
           SELECT * FROM utb_Employee
           WHERE EmployeeID = @EmployeeID
     END

    IF @Action='GET'
    BEGIN
    /*Setting Local Variables*/
    SET @lEmployeeID=@EmployeeID
    SET @lEmployeeName=@EmployeeName
    SET @lEmployeeAddress=@EmployeeAddress
    SET @lEmployeePhoneNo=@EmployeePhoneNo

    SET @lPageNumber = @PageNumber
    SET @lPageSize = @PageSize
    SET @lSortColumn = LTRIM(RTRIM(@SortColumn))

    SET @lFirstRecord = ( @lPageNumber - 1 ) * @lPageSize
    SET @lLastRecord = ( @lPageNumber * @lPageSize + 1 )
    SET @lTotalRows = @lFirstRecord - @lLastRecord + 1; 

    WITH CTE_Results
    AS (
        SELECT ROW_NUMBER() OVER (ORDER BY
        CASE WHEN @lSortColumn='EmployeeID' AND @SortOrder='ASC'
            THEN EmployeeID
        END ASC,
        CASE WHEN @lSortColumn='EmployeeID' AND @SortOrder='DESC'
            THEN EmployeeID
        END DESC,

        CASE WHEN @lSortColumn='EmployeeName' AND @SortOrder='ASC'
            THEN EmployeeName
        END ASC,
        CASE WHEN @lSortColumn='EmployeeName' AND @SortOrder='DESC'
            THEN EmployeeName
        END DESC,

        CASE WHEN @lSortColumn='EmployeeAddress' AND @SortOrder='ASC'
            THEN EmployeeAddress
        END ASC,
        CASE WHEN @lSortColumn='EmployeeAddress' AND @SortOrder='DESC'
            THEN EmployeeAddress
        END DESC,

        CASE WHEN @lSortColumn='EmployeePhoneNo' AND @SortOrder='ASC'
            THEN EmployeePhoneNo
        END ASC,
        CASE WHEN @lSortColumn='EmployeePhoneNo' AND @SortOrder='DESC'
            THEN EmployeePhoneNo
        END DESC

        ) AS ROWNUM,
    COUNT(*) over() AS TotalCount,
    EmployeeID,
    EmployeeName,
    EmployeeAddress,
    EmployeePhoneNo From utb_Employee 
    )
    SELECT EmployeeID,EmployeeName,EmployeeAddress,EmployeePhoneNo,TotalCount From CTE_Results AS CPC
    Where  ROWNUM > @lFirstRecord
            AND ROWNUM < @lLastRecord
    ORDER BY ROWNUM ASC
    END 

END

【讨论】:

大家好,首先,我已经在 sql server 中创建了表。我的表名是 Employee.after 我在我的表中创建列字段这个我的表名是 utb_Employee.finally 我创建了存储过程这个存储过程名称是 usp_employee。

以上是关于使用 WPF 和使用存储过程进行分页的主要内容,如果未能解决你的问题,请参考以下文章

使用存储过程进行数据分页

使用存储过程进行分页

调用存储过程进行分页实例

在 WCF 中使用分页

数据库sqlserver如何用存储过程做分页

老码农怀旧一个简单好用的分页存储过程