C#.Net学生管理系统设计
提示:此系统设计制作参考B站Up面朝星海我心澎湃。希望对初学C#和.Net的同学有参考性的帮助。
文章目录
前言
提示:这篇文章已展示主要的界面和代码,需要源码的可以私信我。
提示:以下是本篇文章正文内容,下面案例可供参考
一、C#是什么?
C# 语言是微软推出的一款面向对象的编程语言,凭借其通用的语法和便捷的使用方法受到了很多企业和开发人员的青睐。
C# 语言具备了面向对象语言的特征,即封装、继承、多态,并且添加了事件和委托,增强了编程的灵活性。
认识 C#
二、使用步骤
提示:大部分界面设计和代码块与Up主的相同,部分界面和代码有改动,在原有的基础上增添了一些新的功能。
文章结尾会提到我仍然存在的问题和Bug。
1. Login界面设计
Login界面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 学生管理系统
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (pictureBox1.Location.X < 200)
{
pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);
button4.Visible = true;
}
else
{
if (comboBox1.Text == "学生")
{
timer1.Stop();
string sql = $"select*from T_student where id= '{textBox1.Text}' and Password='{textBox2.Text}'";
Dao dao = new Dao();
IDataReader dc = dao.read(sql);
dc.Read();
string sid = dc["id"].ToString();
Student1_2 a = new Student1_2(sid);
a.Show();
}
else
{
if (comboBox1.Text == "老师")
{
timer1.Stop();
Student a = new Student();
a.Show();
}
else
{
if(comboBox1.Text == "管理员")
{
timer1.Stop();
admin1 a = new admin1();
a.Show();
}
}
}
}
}
//登录事件
private void button1_Click(object sender, EventArgs e)
{
if (Denglu())
{
timer1.Start();//启动计时器事件,图片开始移动
textBox1.Visible = false;
textBox2.Visible = false;
comboBox1.Visible = false;
button1.Visible = false;
button2.Visible = false;
button3.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
checkBox1.Visible = false;
}
}
private bool Denglu()
{
if (textBox1.Text == "" && textBox2.Text == "" && comboBox1.Text == "")
{
MessageBox.Show("输入不完整,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false; }
if (comboBox1.Text == "学生")
{
Dao dao = new Dao();
string sql = $"select*from T_student where id= '{textBox1.Text}' and Password='{textBox2.Text}'";
IDataReader dc = dao.read(sql);
if (dc.Read())
{
Data.UName = dc["name"].ToString();
label6.Text = $"欢迎{Data.UName}同学登录";
MessageBox.Show("登录成功");
//Student a = new Student();
//this.Hide();
//a.ShowDialog();
//this.Show();
return true;
}
else
{
MessageBox.Show("登录失败");
return false;
}
}
if (comboBox1.Text == "老师")
{
Dao dao = new Dao();
string sql = $"select*from T_teacher where id='{textBox1.Text}'and PassWord= '{textBox2.Text}'";
IDataReader dc = dao.read(sql);
if (dc.Read())
{
Data.UID = dc["id"].ToString();
Data.UName = dc["name"].ToString();
label6.Text = $"欢迎{Data.UName}老师登录";
MessageBox.Show("登录成功");
//admin1_2 a = new admin1_2();
//this.Hide();
//a.ShowDialog();
//this.Show();
return true;
}
else
{
MessageBox.Show("登录失败");
return false;
}
}
if (comboBox1.Text == "管理员")
{
Dao dao = new Dao();
string sql = $"select*from T_admin where id='{textBox1.Text}'and Pwd= '{textBox2.Text}'";
IDataReader dc = dao.read(sql);
if (dc.Read())
{
Data.UName = dc["name"].ToString();
label6.Text = $"欢迎{Data.UName}管理员登录";
MessageBox.Show("登录成功");
//admin1_2 a = new admin1_2();
//this.Hide();
//a.ShowDialog();
//this.Show();
return true;
}
else
{
MessageBox.Show("登录失败");
return false;
}
}
return false;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = null;
textBox2.Text = null;
this.Close();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void 欢迎使用学生管理系统_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Login_Load(object sender, EventArgs e)
{
this.AcceptButton = button1; //默认回车键
button4.Visible = false;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//复选框被勾选,明文显示
textBox2.PasswordChar = new char();
}
else
{
//复选框被取消勾选,密文显示
textBox2.PasswordChar = '*';
}
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
2. 数据库类代码
Dao.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 学生管理系统
{
class Dao
{
SqlConnection sc;
public SqlConnection connect()
{
string str = "Data Source = localhost;Initial Catalog=student;Integrated Security=true";//数据库连接字符串
sc = new SqlConnection(str);//创建数据库连接对象
sc.Open();//打开数据库
return sc;//返回数据库连接对象
}
public SqlCommand command(string sql)
{
SqlCommand cmd = new SqlCommand(sql, connect());
return cmd;
}
//用于delete update insert 返回受影响的行数
public int Excute(string sql)
{
return command(sql).ExecuteNonQuery();
}
//用于select,返回SqldataReader对象,包含select到的数据
public SqlDataReader read(string sql)
{
return command(sql).ExecuteReader();
}
public void Daoclose()
{
sc.Close();//关闭数据库连接
}
internal static IDataReader read(object sql)
{
throw new NotImplementedException();
}
internal void Close()
{
throw new NotImplementedException();
}
internal int Execute(string sql)
{
throw new NotImplementedException();
}
}
}
Data.cs
namespace 学生管理系统
{
class Data
{
public static string UID = "", UName = ""; //登录用户id和姓名
}
}
3. 数据库样式
T_admin表
T_course表
T_CourseRecord表
T_student表
T_teacher表
4. 老师登录后界面设计
老师登录后界面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 学生管理系统
{
public partial class Student : Form
{
public Student()
{
InitializeComponent();
Table();
}
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
private void toolStripStatusLabel3_Click(object sender, EventArgs e)
{
}
private void Student_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
timer1.Interval = 1000;
timer1.Start();
toolStripStatusLabel1.Text = $"欢迎{Data.UName}老师使用学生管理系统";
textBox1.Text = "输入学生姓名,学号查询";
comboBox1.Text = "学号";
}
private void 添加学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Student1_1 h = new Student1_1();
h.ShowDialog();
Table();
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void 删除学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult r = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel);
if (r == DialogResult.OK)
{
string id, name;
id = dataGridView1.SelectedCells[0].Value.ToString();
name = dataGridView1.SelectedCells[1].Value.ToString();
string sql = $"delete from T_student where id='{id}'and Name = '{name}' ";
//MessageBox.Show(sql);
Dao dao = new Dao();
dao.Excute(sql);
Table();
}
//try
//{
// string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取账号
// DialogResult dr = MessageBox.Show("确认删除吗?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
// if (dr == DialogResult.OK)
// {
// string sql = $"delete from T_student where id='{id}'";
// Dao dao = new Dao();
// if (dao.Excute(sql) > 0)
// {
// MessageBox.Show("删除成功");
// Table();
// }
// else
// {
// MessageBox.Show("删除失败" + sql);
// }
// dao.Daoclose();
// }
//}
//catch
//{
// MessageBox.Show("请先在表格选中要删除的图书记录!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
//string id, name;
//id = dataGridView1.SelectedCells[0].Value.ToString();
//name = dataGridView1.SelectedCells[1].Value.ToString();
//string sql = $"delete from T_student where id ='{id} 'and name='{name}'";
//MessageBox.Show(sql);
//Dao dao = new Dao();
//dao.Excute(sql);
//Table();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Student1_1 h = new Student1_1();
h.ShowDialog();
Table();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
string[] str = { dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[2].Value.ToString(), dataGridView1.SelectedCells[3].Value.ToString(), dataGridView1.SelectedCells[4].Value.ToString() };
//MessageBox.Show(str[0]+str[4]);
Student1_1 h = new Student1_1();
h.ShowDialog();
}
private void 修改学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
string[] str = { dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[2].Value.ToString(), dataGridView1.SelectedCells[3].Value.ToString(), dataGridView1.SelectedCells[4].Value.ToString() };
//MessageBox.Show(str[0]+str[4]);
Student1_1 d = new Student1_1(str);
d.ShowDialog();
Table();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
Table();
}
public void Table()
{
dataGridView1.Rows.Clear();
string sql = $"select * from T_student";
Dao dao = new Dao();
IDataReader dr = dao.read(sql);
while (dr.Read())
{
string a, b, c, d, e;
a = dr["id"].ToString();
b = dr["Name"].ToString();
c = dr["Class"].ToString();
d = dr["Birthday"].ToString();
e = dr["JG"].ToString();
string[] str = { a, b, c, d, e };
dataGridView1.Rows.Add(str);
}
dr.Close();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//public void Table()
//{
// dataGridView1.Rows.Clear();//清空旧数据
// Dao dao = new Dao();
// string sql = $"select*from T_student";
// IDataReader dc = dao.read(sql);
// while (dc.Read())
// {
// dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
// }
// dc.Close();
// dao.Daoclose();
//}
public void TableID()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"select * from T_student where id like '%{textBox1.Text}%' ";
IDataReader dc = dao.read(sql);
while (dc.Read())
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.Daoclose();
}
public void TableName()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"select * from T_student where Name like '%{textBox1.Text}%'";
IDataReader dc = dao.read(sql);
while (dc.Read())
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.Daoclose();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "学号")
{
TableID();
}
else
{
TableName();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void 添加课程ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 添加课程ToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
}
}
5.学生信息管理界面设计
学生信息管理界面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 学生管理系统
{
public partial class Student1_1 : Form
{
string[] str = new string[5];
public Student1_1()
{
InitializeComponent();
} public Student1_1(string[] a)
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
str[i] = a[i];
}
textBox1.Text = str[0];
textBox2.Text = str[1];
textBox3.Text = str[2];
textBox4.Text = str[3];
textBox5.Text = str[4];
button1.Visible = false;//修改时隐藏插入保存按钮
}
private void button1_Click(object sender, EventArgs e)
{
//if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
//{
// string sql = $"insert into T_student values('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}','{textBox5.Text}','1234556')";
// MessageBox.Show(sql);
// Dao dao = new Dao();
// int x = dao.Execute(sql);
// if (x > 0)
// {
// MessageBox.Show("添加成功", "恭喜!");
// }
// else
// {
// MessageBox.Show("添加失败", "警告!");
// }
// textBox1.Text = "";
// textBox2.Text = "";
// textBox3.Text = "";
// textBox4.Text = "";
// textBox5.Text = "";
//}
//else
//{
// MessageBox.Show("输入不允许有空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//}
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
{
MessageBox.Show("输入不完整,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
string sql = $"insert into T_student values('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}','{textBox5.Text}','123456')";
//MessageBox.Show(sql);
Dao dao = new Dao();
int i = dao.Excute(sql);
if (i > 0)
{
MessageBox.Show("添加成功", "恭喜!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
else
{
MessageBox.Show("添加失败", "警告!");
}
//textBox1.Text = "";
//textBox2.Text = "";
//textBox3.Text = "";
//textBox4.Text = "";
//textBox5.Text = "";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void Student1_1_Load(object sender, EventArgs e)
{
this.AcceptButton = button1; //默认回车键
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
{
MessageBox.Show("输入不完整,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (textBox1.Text != str[0])
{
string sql = $"update T_student set id = '{textBox1.Text}'where id='{str[0]}'and Name='{str[1]}'";
Dao dao = new Dao();
dao.Excute(sql);
str[0] = textBox1.Text;
}if (textBox2.Text != str[1])
{
string sql = $"update T_student set Name = '{textBox2.Text}'where id='{str[0]}'and Name='{str[1]}'";
Dao dao = new Dao();
dao.Excute(sql);
str[1] = textBox2.Text;
}if (textBox3.Text != str[2])
{
string sql = $"update T_student set Class = '{textBox3.Text}'where id='{str[0]}'and Name='{str[1]}'";
Dao dao = new Dao();
dao.Excute(sql);
str[2] = textBox3.Text;
}if (textBox4.Text != str[3])
{
string sql = $"update T_student set Birthday = '{textBox4.Text}'where id='{str[0]}'and Name='{str[1]}'";
Dao dao = new Dao();
dao.Excute(sql);
str[3] = textBox4.Text;
}if (textBox5.Text != str[4])
{
string sql = $"update T_student set JG = '{textBox5.Text}'where id='{str[0]}'and Name='{str[1]}'";
Dao dao = new Dao();
dao.Excute(sql);
str[4] = textBox5.Text;
}
MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
textBox5.Text = null;
}
}
}
6.密码修改
密码修改界面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 学生管理系统
{
public partial class Password : Form
{
string SID;
public Password()
{
InitializeComponent();
}
public Password(string sid)
{
InitializeComponent();
SID = sid;
string sql = $"select*from T_student where id='{SID}'";
Dao dao = new Dao();
IDataReader dr = dao.read(sql);
dr.Read();
textBox1.Text = dr["Password"].ToString();
dr.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Password_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox3.TextLength < 6 || textBox3.TextLength > 16)
{
MessageBox.Show("密码不符合标准!", "提示");
return;
}
else
{
if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("二次密码不一致,请重新输入!", "提示");
textBox3.Text = "";
return;
}
else if (textBox2.Text == textBox3.Text)
{
//执行sql向数据库插入新的注册信息。
string sql = $"Update T_student set Password='{textBox2.Text}' where id='{SID}'";
Dao dao = new Dao();
int i = dao.Excute(sql);
if (i > 0)
{
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//复选框被勾选,明文显示
textBox2.PasswordChar = new char();
}
else
{
//复选框被取消勾选,密文显示
textBox2.PasswordChar = '*';
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
//复选框被勾选,明文显示
textBox3.PasswordChar = new char();
}
else
{
//复选框被取消勾选,密文显示
textBox3.PasswordChar = '*';
}
}
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//复选框被勾选,明文显示
textBox2.PasswordChar = new char();
}
else
{
//复选框被取消勾选,密文显示
textBox2.PasswordChar = '*';
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
//复选框被勾选,明文显示
textBox3.PasswordChar = new char();
}
else
{
//复选框被取消勾选,密文显示
textBox3.PasswordChar = '*';
}
}
}
}
总结
这个学生管理系统的注册功能和其他功能下期更新。
学生选课界面是存在部分问题的,后期将记录问题。
需要工程文件的,有问题的可以私聊我
B站up的主页链接:https://space.bilibili.com/124790846?spm_id_from=333.788.b_765f7570696e666f.1(建议先看up图书管理系统,这个up设计的界面和代码都比较规范)
学习任何一门语言都不是容易的,都需要我们从底层做起,很多东西都是从无到有,从不会到会的,现在信息越来越发达,这就要求我们的掌握的知识和能力越来越强。我们如今用着很多现成的东西,然而,当有一天,你发现你自己,或者你在的团队做也可以独立的做出来,那种成就感是十分巨大的,加油,各位!