我正在使用此函数在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;
}
答案 0 :(得分: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
答案 1 :(得分: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);
答案 2 :(得分:0)
您有 2个选项:
在数据库中创建一个新的存储过程,如下所示:
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;
更改以下代码行:
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);