news 2026/4/16 15:46:49

打造学生信息管理系统:从构思到实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
打造学生信息管理系统:从构思到实现

简单学生信息管理系统(附源码),原生无边框winform+sqlite,主要运用窗体继承+动态导航菜单+反射创建窗体对象家+事件刷新数据,自定义4种类型弹窗类型对话框,数据分层,增删查改都实现了,其余功能可以买回去自己加,学习demo(注释都有)只是学习用

最近捣鼓了一个简单的学生信息管理系统,想着跟大家分享下过程,说不定对正在学习编程的小伙伴有点帮助。这个系统基于原生无边框 winform 搭配 sqlite 数据库,还融入了不少有意思的技术点,像窗体继承、动态导航菜单啥的。

技术栈与整体架构

1. 原生无边框 winform

用 winform 来构建界面,选择无边框形式是为了打造更简洁现代的 UI 风格。在 winform 项目创建后,设置FormBorderStyle属性为None就能实现无边框效果:

public partial class MainForm : Form { public MainForm() { InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None; } }

这样,整个窗口就没有了传统的标题栏和边框,接下来可以自己绘制标题栏,添加拖动、关闭、最小化等功能。

2. sqlite 数据库

sqlite 小巧轻便,对于这种小型学习 demo 再合适不过。使用System.Data.SQLite库来操作数据库。以下是简单的连接数据库代码:

using System.Data.SQLite; string connectionString = "Data Source=student.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); // 这里可以执行 SQL 语句,比如创建表 string createTableQuery = "CREATE TABLE IF NOT EXISTS Students (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INT)"; using (SQLiteCommand command = new SQLiteCommand(createTableQuery, connection)) { command.ExecuteNonQuery(); } }

这段代码首先定义了连接字符串,指定数据库文件名为student.db,然后打开连接并创建了一个Students表,包含IdNameAge字段。

核心功能实现

1. 窗体继承

通过窗体继承,可以复用一些通用的属性和方法。比如说有一个基础的BaseForm,包含一些通用的样式设置和初始化逻辑:

public class BaseForm : Form { public BaseForm() { // 通用的样式设置,比如字体、背景色 this.Font = new Font("微软雅黑", 10); this.BackColor = Color.White; } }

然后其他具体的功能窗体,如StudentListForm继承自BaseForm

public class StudentListForm : BaseForm { // 这里编写展示学生列表的具体逻辑 }

这样StudentListForm就自动拥有了BaseForm的样式设置,减少了重复代码。

2. 动态导航菜单

动态导航菜单根据用户的操作来展示不同的功能界面。使用ToolStripMenuItem来构建菜单,然后通过事件处理来加载相应的窗体。

private void studentListToolStripMenuItem_Click(object sender, EventArgs e) { // 创建并显示学生列表窗体 StudentListForm studentListForm = new StudentListForm(); studentListForm.MdiParent = this; studentListForm.Show(); }

这里当点击 “学生列表” 菜单项时,创建StudentListForm实例,并设置其MdiParent为当前主窗体,然后显示出来。

3. 反射创建窗体对象

反射机制能在运行时动态创建对象。假设我们有一个FormFactory类来通过反射创建窗体:

public class FormFactory { public static Form CreateForm(string formName) { try { // 获取当前程序集 Assembly assembly = Assembly.GetExecutingAssembly(); // 根据名称创建类型实例 Type formType = assembly.GetType("YourNamespace." + formName); return (Form)Activator.CreateInstance(formType); } catch (Exception ex) { // 处理异常 MessageBox.Show($"创建窗体失败: {ex.Message}"); return null; } } }

在使用时:

string formName = "StudentListForm"; Form studentListForm = FormFactory.CreateForm(formName); if (studentListForm!= null) { studentListForm.MdiParent = this; studentListForm.Show(); }

这样可以通过字符串名称灵活地创建不同的窗体,方便扩展和维护。

4. 事件刷新数据

当数据发生变化,比如添加、删除学生信息后,需要刷新相关的界面展示。通过自定义事件来实现。在数据操作类(如StudentDataAccess)中定义事件:

public class StudentDataAccess { public event EventHandler DataChanged; protected virtual void OnDataChanged() { DataChanged?.Invoke(this, EventArgs.Empty); } public void AddStudent(Student student) { // 执行添加学生的数据库操作 OnDataChanged(); } }

在界面类(如StudentListForm)中注册事件:

public class StudentListForm : BaseForm { private StudentDataAccess studentDataAccess; public StudentListForm() { InitializeComponent(); studentDataAccess = new StudentDataAccess(); studentDataAccess.DataChanged += StudentDataAccess_DataChanged; } private void StudentDataAccess_DataChanged(object sender, EventArgs e) { // 刷新学生列表数据显示 RefreshStudentList(); } }

这样当添加学生操作完成后,会触发DataChanged事件,进而刷新学生列表界面。

5. 自定义弹窗类型对话框

自定义了 4 种类型的弹窗,比如信息提示、确认删除等。以确认删除弹窗为例:

public class ConfirmDeleteDialog : Form { public ConfirmDeleteDialog() { // 设置弹窗界面布局,添加提示文本、确认和取消按钮等 Label promptLabel = new Label(); promptLabel.Text = "确定要删除该学生信息吗?"; promptLabel.Location = new Point(20, 20); this.Controls.Add(promptLabel); Button confirmButton = new Button(); confirmButton.Text = "确定"; confirmButton.Location = new Point(50, 60); confirmButton.Click += ConfirmButton_Click; this.Controls.Add(confirmButton); Button cancelButton = new Button(); cancelButton.Text = "取消"; cancelButton.Location = new Point(120, 60); cancelButton.Click += CancelButton_Click; this.Controls.Add(cancelButton); } private void ConfirmButton_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } private void CancelButton_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } }

在使用时:

ConfirmDeleteDialog confirmDialog = new ConfirmDeleteDialog(); if (confirmDialog.ShowDialog() == DialogResult.OK) { // 执行删除操作 }

数据分层与增删查改

数据分层将业务逻辑和数据访问分离。有数据访问层(DAL)负责与数据库交互,业务逻辑层(BLL)处理业务规则,表现层(UI)负责界面展示。

1. 增加学生信息

在 DAL 层:

public class StudentDataAccess { public void AddStudent(Student student) { string connectionString = "Data Source=student.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string insertQuery = "INSERT INTO Students (Name, Age) VALUES (@Name, @Age)"; using (SQLiteCommand command = new SQLiteCommand(insertQuery, connection)) { command.Parameters.AddWithValue("@Name", student.Name); command.Parameters.AddWithValue("@Age", student.Age); command.ExecuteNonQuery(); } } } }

在 BLL 层可以进行一些简单的验证,比如年龄是否合法等,然后调用 DAL 层方法。

2. 删除学生信息

public void DeleteStudent(int id) { string connectionString = "Data Source=student.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string deleteQuery = "DELETE FROM Students WHERE Id = @Id"; using (SQLiteCommand command = new SQLiteCommand(deleteQuery, connection)) { command.Parameters.AddWithValue("@Id", id); command.ExecuteNonQuery(); } } }

3. 查询学生信息

public List<Student> GetAllStudents() { List<Student> students = new List<Student>(); string connectionString = "Data Source=student.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string selectQuery = "SELECT Id, Name, Age FROM Students"; using (SQLiteCommand command = new SQLiteCommand(selectQuery, connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Student student = new Student(); student.Id = reader.GetInt32(0); student.Name = reader.GetString(1); student.Age = reader.GetInt32(2); students.Add(student); } } } } return students; }

4. 修改学生信息

public void UpdateStudent(Student student) { string connectionString = "Data Source=student.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string updateQuery = "UPDATE Students SET Name = @Name, Age = @Age WHERE Id = @Id"; using (SQLiteCommand command = new SQLiteCommand(updateQuery, connection)) { command.Parameters.AddWithValue("@Name", student.Name); command.Parameters.AddWithValue("@Age", student.Age); command.Parameters.AddWithValue("@Id", student.Id); command.ExecuteNonQuery(); } } }

这个学生信息管理系统目前只是一个学习 demo,注释都很详细,增删查改等基础功能都实现了,如果有小伙伴感兴趣,买回去可以自己再添加更多功能,希望能给大家的学习带来一些启发。源码都在,欢迎一起探讨交流呀。

简单学生信息管理系统(附源码),原生无边框winform+sqlite,主要运用窗体继承+动态导航菜单+反射创建窗体对象家+事件刷新数据,自定义4种类型弹窗类型对话框,数据分层,增删查改都实现了,其余功能可以买回去自己加,学习demo(注释都有)只是学习用

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:56:57

基于Qt的温度湿度传感器采样上位机:从代码到应用

Qt温度湿度传感器采样上位机源代码C语言Qt源代码数据记录功能1.功能介绍&#xff1a; 采用C/C语言编写的&#xff0c;通过串口发送AT指令&#xff0c;获取温度、湿度传感器的采样数据并显示的Qt上位机程序源 采用独立的文本类型串口通信处理类&#xff0c;可方便进行二次开发。…

作者头像 李华
网站建设 2026/4/16 10:56:26

论文AI率98%怎么办?5步降到10%以下超全攻略

论文AI率98%怎么办&#xff1f;5步降到10%以下超全攻略 TL;DR&#xff1a;论文AI率太高不要慌&#xff0c;核心策略是「两步走」——先用DeepSeek做粗处理把AI率降到50%-60%&#xff0c;再用专业工具深度降到10%以下。本文详细拆解5个步骤&#xff0c;从定位问题到最终校对&…

作者头像 李华
网站建设 2026/4/16 6:03:29

AI实时监控测试进度:预警延误与风险‌

测试进度管理的范式变革 随着DevOps与持续交付的普及&#xff0c;传统手工跟踪测试进度的模式已难以应对复杂系统迭代。本文基于2025年行业调研数据&#xff08;Gartner报告显示83%企业遭遇测试延误&#xff09;&#xff0c;深度解析AI监控系统的技术架构、预警机制及落地路径…

作者头像 李华
网站建设 2026/4/16 10:16:21

测试团队的知识管理:AI自动归纳最佳实践

知识管理的迫切性与AI的变革作用 在软件测试领域&#xff0c;知识管理是团队效率与质量保障的核心支柱。测试团队每日产生海量数据——从缺陷报告、测试用例到经验总结——但传统手动管理方式面临诸多挑战&#xff1a;知识碎片化导致重复劳动&#xff0c;隐性经验难以传承&…

作者头像 李华