1

我正在使用此功能使用分页在 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;
        }
4

3 回答 3

3

您可以在 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
于 2013-09-04T10:03:45.257 回答
1

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);
于 2013-09-07T16:22:04.353 回答
0

您有2 个选项

选项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);
于 2013-09-06T06:45:15.030 回答