简单学生信息管理系统(附源码),原生无边框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表,包含Id、Name和Age字段。
核心功能实现
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(注释都有)只是学习用