显示第一页的最后10行,而不是显示下一页的下一个最后10行

本文关键字:显示 10行 最后 一页 下一个 第一页 | 更新日期: 2023-09-27 18:30:11

我使用这个函数在datagridview中使用分页显示数据,在page 1上使用这个函数,我可以正确显示最后10行,但当我单击下一个按钮时,它应该显示next last 10 rows,但它在每个下一页上显示前10行,我认为它们在我的cmd2 query of else part中是错误的,但那是什么?我正在使用Access数据库

private DataTable GetCurrentRecords(int page, OleDbConnection con)
        {
            dt = new DataTable();
            if (page == 1)
            {
                cmd2 = new OleDbCommand("Select TOP " + PageSize + " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions ORDER BY QID DESC", con);
                // CurrentPageIndex++;
            }
            else
            {
                int PreviouspageLimit = (page - 1) * PageSize;
                cmd2 = new OleDbCommand("Select TOP " + PageSize +
                    " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions " +
                    "WHERE QID NOT IN " +
                "(Select TOP " + PreviouspageLimit + " QID from Questions ORDER BY QID DESC) ", con); // +
                //"order by MedicalClgID", con);
            }
            try
            {
                // con.Open();
                this.adp1.SelectCommand = cmd2;
                this.adp1.Fill(dt);
                txtPaging.Text = string.Format("{0} of {1} ", this.CurrentPageIndex, this.TotalPage);
            }
            finally
            {
                // con.Close();
            }
            return dt;
        }

显示第一页的最后10行,而不是显示下一页的下一个最后10行

您可以在t-sql 中使用over()实现分页

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY task_id ) AS RowNum, QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks
          FROM      [Questions]         
        ) AS RowConstrainedResult
WHERE   RowNum >= 10
    AND RowNum < 20 
ORDER BY RowNum

cmd2更改为该值将给出DESCENDING ORDER 中的所有行

cmd2 = new OleDbCommand("Select TOP " + PageSize +
                    " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions " +
                    "WHERE QID NOT IN " +
                "(Select TOP " + PreviouspageLimit + " QID from Questions ORDER BY QID DESC) " +
                "ORDER BY QID DESC", con);

您有两个选项

选项1

在数据库中创建一个新的存储过程,如下所示:

USE [YourDatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE RelatedSchemaName.StoredProcedureName
    @bintStartingRowNumber bigint,
    @bintRowLength bigint
AS
BEGIN
    SET NOCOUNT OFF;
    WITH AllRows AS
    (
        SELECT ROW_NUMBER() OVER (ORDER BY QID, DESC) AS RowNumber, *
        FROM Questions AS Q 
    ) 
    SELECT QID, Question, Opt1, Opt2, Opt3, Opt4, AnsOp, Marks
    FROM AllRows
    WHERE RowNumber BETWEEN @bintStartingRowNumber AND @bintStartingRowNumber + @bintRowLength
    ORDER BY QID, DESC
END
GO

现在,将其添加到您的项目中,然后更改以下代码行:

if (page == 1)
{
    cmd2 = new OleDbCommand("Select TOP " + PageSize + " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions ORDER BY QID DESC", con);
    // CurrentPageIndex++;
}
else
{
    int PreviouspageLimit = (page - 1) * PageSize;
    cmd2 = new OleDbCommand("Select TOP " + PageSize +
        " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions " +
        "WHERE QID NOT IN " +
        "(Select TOP " + PreviouspageLimit + " QID from Questions ORDER BY QID DESC) ", con); // +
    //"order by MedicalClgID", con);
}

这个:

int intFirstRowNumber = (page - 1) * PageSize;
cmd2 = new SqlCommand(StoredProcedureName);
cmd2.CommandType = CommandType.StoredProcedure; 
cmd2.Parameters.Add("@bintStartingRowNumber", SqlDbType.BigInt).Value = intFirstRowNumber;
cmd2.Parameters.Add("@bintRowLength", SqlDbType.BigInt).Value = PageSize;

选项2

更改以下代码行:

if (page == 1)
{
    cmd2 = new OleDbCommand("Select TOP " + PageSize + " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions ORDER BY QID DESC", con);
    // CurrentPageIndex++;
}
else
{
    int PreviouspageLimit = (page - 1) * PageSize;
    cmd2 = new OleDbCommand("Select TOP " + PageSize +
        " QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions " +
        "WHERE QID NOT IN " +
        "(Select TOP " + PreviouspageLimit + " QID from Questions ORDER BY QID DESC) ", con); // +
    //"order by MedicalClgID", con);
}

这个:

int intFirstRowNumber = (page - 1) * PageSize;
cmd2 = new OleDbCommand(
    "WITH AllRows AS (" +
    "SELECT ROW_NUMBER() OVER (ORDER BY QID, DESC) AS RowNumber, * " +
    "FROM Questions AS Q) " +
    "SELECT QID, Question, Opt1, Opt2, Opt3, Opt4, AnsOp, Marks " +
    "FROM AllRows " +
    "WHERE RowNumber BETWEEN " + intFirstRowNumber + " AND " + (intFirstRowNumber + PageSize) +
    " ORDER BY QID, DESC", con);