1

ページングを使用して datagridview にデータを表示するためにこの関数を使用してpage 1いますnext last 10 rows。私の間違いですが、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

1

これに変更cmd2すると、すべての行が降順になります

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 に答える