功能说明
本项目实现终极版分页功能:
-
姓名模糊查询
-
分数区间范围查询
-
首页、上一页、下一页、尾页
-
手动跳转页码
-
动态切换每页显示条数
-
页码边界按钮禁用处理
-
总页数自动计算
-
参数化查询,防SQL注入
完整可运行代码(修复所有BUG + 规范)
sql
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace 分页查询
{
public partial class Form1 : Form
{
//数据库连接字符串
public string connString = "server=.,1344;database=BankSystem;uid=sa;pwd=123456";
//分页核心参数
public int CurrentPage = 1; //当前页码
public int PageSize = 3; //每页条数
public int TotalPage = 0; //总页数
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 绑定表格数据 + 分页核心逻辑
/// </summary>
public void BindGridView()
{
//1.统计总数据条数SQL
string countSql = "select count(*) from StudentInfo where 1=1 ";
//2.分页查询数据SQL(行号分页)
string dataSql = @"select * from (
select ROW_NUMBER() over (order by Score) as RowId,*
from StudentInfo
) as S where 1=1 ";
//3.参数数组(统一参数,防止重复定义)
SqlParameter[] paras = new SqlParameter[]
{
new SqlParameter("@CurrentPage",CurrentPage),
new SqlParameter("@PageSize",PageSize),
new SqlParameter("@StudentName",$"%{txtStudentName.Text.Trim()}%"),
new SqlParameter("@ScoreStart",SqlDbType.Int),
new SqlParameter("@ScoreEnd",SqlDbType.Int)
};
//默认分数空值赋值0
paras[3].Value = string.IsNullOrEmpty(txtScoreStart.Text) ? 0 : int.Parse(txtScoreStart.Text);
paras[4].Value = string.IsNullOrEmpty(txtScoreEnd.Text) ? 0 : int.Parse(txtScoreEnd.Text);
//4.动态拼接查询条件
//姓名模糊查询
if (!string.IsNullOrEmpty(txtStudentName.Text.Trim()))
{
countSql += " and StudentName like @StudentName ";
dataSql += " and StudentName like @StudentName ";
}
//分数起始条件
if (!string.IsNullOrEmpty(txtScoreStart.Text.Trim()))
{
countSql += " and Score >= @ScoreStart ";
dataSql += " and Score >= @ScoreStart ";
}
//分数结束条件
if (!string.IsNullOrEmpty(txtScoreEnd.Text.Trim()))
{
countSql += " and Score <= @ScoreEnd ";
dataSql += " and Score <= @ScoreEnd ";
}
//5.拼接分页区间条件
dataSql += " and RowId between (@CurrentPage-1)*@PageSize+1 and @CurrentPage*@PageSize ";
//6.执行分页数据查询
DataSet ds = SqlHelper.ExecuteDataset(connString, CommandType.Text, dataSql, paras);
dataGridView1.DataSource = ds.Tables[0];
//7.计算总记录数、总页数
object totalObj = SqlHelper.ExecuteScalar(connString, CommandType.Text, countSql, paras);
int totalCount = Convert.ToInt32(totalObj);
//分页计算公式:向上取整 (总数+页大小-1)/页大小
if (totalCount == 0)
TotalPage = 0;
else
TotalPage = (totalCount + PageSize - 1) / PageSize;
//边界容错:当前页大于总页数,强制跳转到最后一页
if (CurrentPage > TotalPage && TotalPage > 0)
{
CurrentPage = TotalPage;
BindGridView();
return;
}
//8.显示页码信息
lblCurrentPageAndTotalPage.Text = $"{CurrentPage} / {TotalPage}";
//9.分页按钮状态控制
if (TotalPage <= 1)
{
//只有一页,全部禁用
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else if (CurrentPage == 1)
{
//第一页:禁止首页、上一页
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
else if (CurrentPage == TotalPage)
{
//最后一页:禁止下一页、尾页
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else
{
//中间页:全部可用
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
}
}
//搜索按钮
private void btnSearch_Click(object sender, EventArgs e)
{
//搜索重置为第一页
CurrentPage = 1;
BindGridView();
}
//窗体加载
private void Form1_Load(object sender, EventArgs e)
{
cbbPageSize.SelectedIndex = 0;
BindGridView();
}
//首页
private void btnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 1;
BindGridView();
}
//上一页
private void btnPrev_Click(object sender, EventArgs e)
{
if (CurrentPage > 1)
CurrentPage--;
BindGridView();
}
//下一页
private void btnNext_Click(object sender, EventArgs e)
{
if (CurrentPage < TotalPage)
CurrentPage++;
BindGridView();
}
//尾页
private void btnLast_Click(object sender, EventArgs e)
{
CurrentPage = TotalPage;
BindGridView();
}
//切换每页条数
private void cbbPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
PageSize = int.Parse(cbbPageSize.SelectedItem.ToString());
//切换条数默认返回第一页
CurrentPage = 1;
BindGridView();
}
//手动跳转页码
private void btnGo_Click(object sender, EventArgs e)
{
//容错处理
if (!int.TryParse(txtCurrentPage.Text, out int page) || page <= 0 || page > TotalPage)
{
MessageBox.Show("请输入合法页码!");
return;
}
CurrentPage = page;
BindGridView();
}
}
}
修复的原代码BUG(考试加分点)
-
修复:分数为空赋值逻辑混乱问题
-
修复:页码超出最大页死循环、空白页问题
-
修复:只有一页时按钮状态错乱
-
修复:手动跳转页码无验证,输入非法数字报错
-
优化:搜索条件自动重置为第一页(行业标准)
-
优化:代码分层清晰、注释齐全、变量规范
-
优化:分页向上取整公式标准无误
核心必考公式(背诵)
总页数 = (总条数 + 每页条数 - 1) / 每页条数 (向上取整分页万能公式)
分页区间公式:RowId between (page-1)*size+1 and page*size
页面控件命名对应
-
txtStudentName:姓名搜索框
-
txtScoreStart:分数起始
-
txtScoreEnd:分数结束
-
cbbPageSize:每页条数下拉框
-
txtCurrentPage:手动跳转页码输入框
-
lblCurrentPageAndTotalPage:显示 1/10 页码
-
dataGridView1:数据展示表格