news 2026/6/25 13:37:06

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

作者头像

张小明

前端开发工程师

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/6/13 2:16:13

交变磁场下含感应材料沥青路面温度:奇妙的物理与技术融合

交变磁场下含感应材料沥青路面温度在道路工程领域&#xff0c;沥青路面是我们再熟悉不过的存在。然而&#xff0c;你是否想过&#xff0c;通过交变磁场与含感应材料的结合&#xff0c;能让沥青路面的温度产生神奇的变化&#xff1f;这背后蕴含着有趣的物理原理和潜在的应用价值…

作者头像 李华
网站建设 2026/6/17 22:31:42

昆仑通态直接控制变频器程序及通讯那些事儿

昆仑通态直接控制变频器程序及通讯。在工业自动化领域&#xff0c;昆仑通态人机界面与变频器的协同工作是实现精准控制与高效生产的关键环节。今天就来唠唠昆仑通态如何直接控制变频器程序及通讯。 一、通讯基础准备 昆仑通态与变频器通讯&#xff0c;首先要明确通讯协议。常见…

作者头像 李华
网站建设 2026/6/12 3:39:09

django基于数据挖掘技术的台风灾害预测系统-计算机毕业设计源码+无LW文档

Django基于大数据旅游数据分析与推荐系统的设计与实现 摘要 本文阐述了一个基于Django框架&#xff0c;结合大数据技术的旅游数据分析与推荐系统的设计与实现过程。该系统旨在利用大数据分析技术深入挖掘旅游数据&#xff0c;为游客提供精准的旅游推荐和全面的旅游资讯&#xf…

作者头像 李华
网站建设 2026/6/24 19:06:11

永磁同步电机(PMSM)矢量控制之旅:从理论到MATLAB仿真

永磁同步电机(PMSM)矢量控制&#xff0c;坐标变换到d-q轴后&#xff0c;采用SVPWM调制算法&#xff0c;进行速度电流双闭环控制&#xff0c;控制算法为PID&#xff0c;仿真结果如图所示。 (默认发MATLAB R2018b版本)永磁同步电机&#xff08;PMSM&#xff09;因其高效、功率密度…

作者头像 李华
网站建设 2026/6/10 9:24:14

英文论文降AI工具横评:留学生亲测这3款最靠谱

英文论文降AI工具横评&#xff1a;留学生亲测这3款最靠谱 TL;DR&#xff1a;留学生英文论文降AI首选AIGCleaner&#xff08;达标率95%&#xff0c;支持Turnitin/GPTZero&#xff09;&#xff0c;其次是HumText&#xff08;专注学术英文&#xff0c;6.8元起&#xff09;&#xf…

作者头像 李华
网站建设 2026/6/10 9:19:01

Google Cloud与AWS大数据AI服务对比(2026)

Google Cloud 与 AWS 的大数据 AI 服务&#xff0c;核心差异在于 “GCP 强 AI 原生与数据密集型任务性价比&#xff0c;AWS 胜在生态完整与企业级灵活可控”。选 GCP 优先用于深度学习、实时分析、生成式 AI 与数据仓库&#xff1b;选 AWS 优先用于企业级 MLOps、混合云、多元数…

作者头像 李华