news 2026/3/2 9:03:25

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

作者头像

张小明

前端开发工程师

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/2/28 3:46:28

图像金字塔与直方图

在计算机视觉领域&#xff0c;图像金字塔与直方图是两大基础且实用的技术。图像金字塔用于处理不同分辨率的图像&#xff0c;广泛应用于图像融合、超分辨率重建等场景&#xff1b;直方图则用于描述图像像素分布&#xff0c;是图像增强、阈值分割的核心工具。本文将结合完整代码…

作者头像 李华
网站建设 2026/2/28 14:41:09

ABB机器人ABB选项添加:三种方法全解析

ABB选项添加&#xff0c;ABB机器人三种方法全套&#xff0c;真实、虚拟、密钥三种方法&#xff0c;含教程、含软件、含密钥&#xff0c;想怎么加就怎么加&#xff0c;想用真实密钥就用真实密钥&#xff0c;想用虚拟密钥就用虚拟密钥&#xff0c;随意加。在ABB机器人的使用过程中…

作者头像 李华
网站建设 2026/3/1 12:06:14

超实用的白光焊台控制板开发资料分享

白光jbc245/t12/936一A1321/A1322 oled1.3寸焊台控制板资料 &#xff0c;四合1资料。 全套带C语言程序&#xff0c;STC芯片方案&#xff0c;原理图pdf&#xff0c;pcb可直接打板&#xff0c;程序无缺无漏。 照片拍的都有 注意是开发资料&#xff01;嘿&#xff0c;各位电子爱好…

作者头像 李华
网站建设 2026/3/2 3:33:00

博图SCL+LAD之原创程序分享

博图SCLLAD之原创程序。 硬件配置S7-1214?3个CM1241 RS232?1个CB1241 RS485。 以下功能只是一部分&#xff0c;占总程序25%&#xff0c;请注意。 以下用SCL功能实现&#xff1a; 1&#xff1a;预设五组配方&#xff0c;包含条形码编码、光源亮度、板件厚度等信息&#xff0c;…

作者头像 李华
网站建设 2026/2/27 12:42:43

知网AIGC检测不通过?毕业生必看的3步自救攻略

知网AIGC检测不通过&#xff1f;毕业生必看的3步自救攻略 TL;DR&#xff1a;知网AIGC检测不通过别慌&#xff0c;按「定位问题段落 → 工具批量处理 → 人工精修」三步走&#xff0c;可以把AI率从80%降到10%以下。推荐用嘎嘎降AI处理&#xff08;达标率99.26%&#xff09;&…

作者头像 李华