简单的winform学生管理系统Demo
界面效果



练习重点
1. 关系表的创建
2. 增删改查的操作,及sqlhelper的封装
3. 跨页面数据传递,编辑页数据提交后数据局步刷新到列表数据
项目源码
FrmStuddentList
public partial class FrmStudentList : Form
{
private Action reload = null;
public FrmStudentList()
{
InitializeComponent();
}
private static FrmStudentList frmStudentList = null;
public static FrmStudentList CreateInstance()
{
if(frmStudentList is null || frmStudentList.IsDisposed)
{
frmStudentList = new FrmStudentList();
}
return frmStudentList;
}
private void FrmStudentList_Load(object sender, EventArgs e)
{
LoadClasse();//加载班级列表
LoadAllStudentList();//加载所有学生信息
} private void LoadAllStudentList()
{
string sql = "select StuId,StuName,c.ClassName,GradeName,Sex,Phone from StudentInfo s " +
"inner join ClassInfo c on c.ClassId=s.ClassId " +
"inner join GradeInfo g on g.GradeId=c.GradeId";
//加载数据
DataTable dtStudents = SqlHelper.GetDataTable(sql);
//组装
if (dtStudents.Rows.Count > 0)
{
foreach (DataRow dr in dtStudents.Rows)
{
string className = dr["ClassName"].ToString();
string gradeName = dr["GradeName"].ToString();
dr["ClassName"] = className + "--" + gradeName;
}
}
//我只想显示固定的列
dgvStudentList .AutoGenerateColumns = false;
// dtStudents.Columns.Remove(dtStudents.Columns[3]);
//绑定数据
dgvStudentList.DataSource = dtStudents;
} private void LoadClasse()
{
//获取数据 ---- 查询 ---写sql
string sql = "select ClassId,ClassName,GradeName from ClassInfo c,GradeInfo g where c.GradeId=g.GradeId"; DataTable dtClasse = SqlHelper.GetDataTable(sql);
//组合班级列表显示项的过程
if (dtClasse.Rows.Count > 0)
{
foreach (DataRow dr in dtClasse.Rows)
{
string className = dr["ClassName"].ToString();
string gradeName = dr["GradeName"].ToString();
dr["ClassName"] = className + "--" + gradeName;
} }
//添加默认选择项
DataRow drNew = dtClasse.NewRow();
drNew["ClassId"] = 0;
drNew["ClassName"] = "请选择"; dtClasse.Rows.InsertAt(drNew, 0); //指定数据源
cmbClassName.DataSource = dtClasse;
cmbClassName.DisplayMember = "ClassName";
cmbClassName.ValueMember = "ClassId";
} private void textBox1_TextChanged(object sender, EventArgs e)
{ } private void btnSearch_Click(object sender, EventArgs e)
{
//接收条件设置信息
int classId = (int)cmbClassName .SelectedValue;
string stuName = txtStuName.Text.Trim(); //查询sql
string sql = "select StuId,StuName,c.ClassName,GradeName,Sex,Phone from StudentInfo s " +
"inner join ClassInfo c on c.ClassId=s.ClassId " +
"inner join GradeInfo g on g.GradeId=c.GradeId";
sql += " where 1=1 ";
if (classId > 0)
{
sql += " and s.ClassId=@ClassId";
}
if (!string.IsNullOrEmpty(stuName))
{
sql += " and StuName like @StuName";
}
sql += " order by StuId"; SqlParameter[] paras =
{
new SqlParameter("@ClassId",classId),
new SqlParameter("@StuName","%"+stuName+"%")
};
//加载数据
DataTable dtStudents = SqlHelper.GetDataTable(sql, paras);
//组装
if (dtStudents.Rows.Count > 0)
{
foreach (DataRow dr in dtStudents.Rows)
{
string className = dr["ClassName"].ToString();
string gradeName = dr["GradeName"].ToString();
dr["ClassName"] = className + "--" + gradeName;
}
}
//我只想显示固定的列
dgvStudentList.AutoGenerateColumns = false; //绑定数据
dgvStudentList .DataSource = dtStudents;
} private void dgvStudentList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex !=-1)
{
DataGridViewCell cell= dgvStudentList.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell is DataGridViewLinkCell && cell.FormattedValue .ToString ()=="修改")
{
reload = LoadAllStudentList ; DataRow dr = (dgvStudentList.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
int stuId = int.Parse(dr["StuId"].ToString());
FrmEditStudent frmEdit = new FrmEditStudent();
//传值
frmEdit.Tag = new TagObject() { EditId = stuId, Reload = reload };
frmEdit.MdiParent = this.MdiParent;
frmEdit.Show(); }
else if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "删除")
{
if(MessageBox .Show ("您确定要删除该学生信息吗?","删除学生提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
DataRow dr = (dgvStudentList.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
int stuId = int.Parse(dr["StuId"].ToString());
string sqlDel = "delete StudentInfo where StuId=@StuId";
SqlParameter para = new SqlParameter("@StuId", stuId);
int count = SqlHelper.ExecuteNonQuery(sqlDel, para);
if (count > 0)
{
MessageBox.Show("该学生信息删除成功!", "删除学习提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
DataTable dtStudent = (DataTable)dgvStudentList.DataSource;
dtStudent.Rows.Remove(dr);
dgvStudentList.DataSource = dtStudent;
}
else
{
MessageBox.Show("该学生信息删除失败!", "删除学习提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
}
} private void btnDel_Click(object sender, EventArgs e)
{
List<int> listIds = new List<int>();
int count = 0;
for (int i = 0; i < dgvStudentList .Rows .Count ; i++)
{ DataGridViewCheckBoxCell cell = dgvStudentList.Rows[i].Cells["colCheck"] as DataGridViewCheckBoxCell;
bool chk = Convert.ToBoolean(cell.Value);
if (chk)
{
DataRow dr = (dgvStudentList.Rows[i].DataBoundItem as DataRowView).Row;
int stuId = int.Parse(dr["StuId"].ToString());
listIds.Add(stuId);
} }
if(listIds .Count == 0)
{
MessageBox.Show("请选择要删除的数据!", "删除学生提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if(listIds .Count > 0)
{
if (MessageBox.Show("您确定要删除该学生信息吗?", "删除学生提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ using (SqlConnection conn=new SqlConnection(SqlHelper.connString))
{ conn.Open();
SqlTransaction trans = conn.BeginTransaction();
SqlCommand cmd = conn.CreateCommand();
cmd.Transaction = trans;
try
{ foreach (int id in listIds)
{
cmd.CommandText = "delete from StudentInfo where StuId=@StuId";
SqlParameter para = new SqlParameter("@StuId", id);
cmd.Parameters.Clear();
cmd.Parameters.Add(para);
count += cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception)
{
trans.Rollback();
MessageBox.Show("删除学生出现了异常!", "删除学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} }
if(count==listIds.Count)
{
MessageBox.Show("这些学生信息删除成功!", "删除学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//手动刷新
DataTable dtStudents = (DataTable)dgvStudentList.DataSource;
string idStr = string.Join(",", listIds);
DataRow[] rows= dtStudents.Select("StuId in (" + idStr + ")");
foreach (DataRow dr in rows)
{
dtStudents.Rows.Remove(dr);
}
dgvStudentList.DataSource = dtStudents;
}
}
} }
FrmAddStudent
public partial class FrmAddStudent : Form
{
public FrmAddStudent()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//1)获取页面信息输入
string stuName = txtStuName.Text.Trim();
int classId = (int)cmbClassName .SelectedValue;
string sex = rdoMan .Checked ? rdoMan .Text.Trim() : rdoWoman.Text.Trim();
string phone = txtPhone.Text.Trim();
//2)判空处理 姓名不可以为空 电话不可以为空
if (string.IsNullOrEmpty(stuName))
{
MessageBox.Show("姓名不能为空!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(phone))
{
MessageBox.Show("电话不能为空!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//3)判断 姓名+电话 是否在数据库里已存在 姓名+电话
string sql = "select count(1) from StudentInfo where StuName=@StuName and Phone=@phone";
SqlParameter[] paras =
{
new SqlParameter("@StuName",stuName),
new SqlParameter("@phone",phone)
};
object o = SqlHelper.ExecuteScalar(sql, paras);
if (o != null && o != DBNull.Value && ((int)o) > 0)
{
MessageBox.Show("该学生已存在!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//4)添加入库 sql 参数 执行 完成返回受影响行数
string sqlAdd = "insert into StudentInfo(StuName,ClassId,Sex,Phone) values(@StuName,@ClassId,@Sex,@Phone)";
SqlParameter[] parasAdd =
{
new SqlParameter("@StuName",stuName),
new SqlParameter("@ClassId",classId),
new SqlParameter("@Sex",sex),
new SqlParameter("@phone",phone)
};
int count = SqlHelper.ExecuteNonQuery(sqlAdd, parasAdd);
if (count > 0)
{
MessageBox.Show($"学生:{stuName} 添加成功!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("该学生添加失败,请检查!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} private void FrmAddStudent_Load(object sender, EventArgs e)
{
InitClasse();//加载班级列表
rdoMan .Checked = true;
}
private void InitClasse()
{
//获取数据 ---- 查询 ---写sql
string sql = "select ClassId,ClassName,GradeName from ClassInfo c,GradeInfo g where c.GradeId=g.GradeId"; DataTable dtClasses = SqlHelper.GetDataTable(sql);
//组合班级列表显示项的过程
if (dtClasses.Rows.Count > 0)
{
foreach (DataRow dr in dtClasses.Rows)
{
string className = dr["ClassName"].ToString();
string gradeName = dr["GradeName"].ToString();
dr["ClassName"] = className + "--" + gradeName;
} } //指定数据源
cmbClassName .DataSource = dtClasses;
cmbClassName.DisplayMember = "ClassName";
cmbClassName.ValueMember = "ClassId";
cmbClassName.SelectedIndex = 0;
} private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
FrmEditStudent
public partial class FrmEditStudent : Form
{
public FrmEditStudent()
{
InitializeComponent();
}
private Action reload = null;
private int stuId;
private void FrmEditStudent_Load(object sender, EventArgs e)
{
IntiClass();//加载班级列表
InitStuInfo();//加载学生信息
} private void IntiClass()
{
//获取数据 ---- 查询 ---写sql
string sql = "select ClassId,ClassName,GradeName from ClassInfo c,GradeInfo g where c.GradeId=g.GradeId"; DataTable dtClasses = SqlHelper.GetDataTable(sql);
//组合班级列表显示项的过程
if (dtClasses.Rows.Count > 0)
{
foreach (DataRow dr in dtClasses.Rows)
{
string className = dr["ClassName"].ToString();
string gradeName = dr["GradeName"].ToString();
dr["ClassName"] = className + "--" + gradeName;
} } //指定数据源
cmbClassName.DataSource = dtClasses;
cmbClassName.DisplayMember = "ClassName";
cmbClassName.ValueMember = "ClassId";
cmbClassName.SelectedIndex = 0;
} private void InitStuInfo()
{
//获取stuid
if(this.Tag!=null)
{
TagObject tagObject = (TagObject)this.Tag;
this.stuId = tagObject.EditId;
this.reload = tagObject.Reload;
}
//查询出来
string sql = @"select StuName,Sex,ClassId,Phone from StudentInfo where StuId=@StuId";
SqlParameter paraId = new SqlParameter("@StuId", stuId);
SqlDataReader dr = SqlHelper.ExecuteReader(sql, paraId);
if (dr.Read())
{
txtStuName.Text = dr["StuName"].ToString();
txtPhone.Text = dr["Phone"].ToString();
string sex = dr["Sex"].ToString();
if (sex == "男")
{
rdoMan.Checked = true;
}
else
{
rdoWoman.Checked = true;
}
int classId = (int)dr["ClassId"];
cmbClassName.SelectedValue = classId;
}
dr.Close();
} private void btnEdit_Click(object sender, EventArgs e)
{
//1)获取页面信息输入
string stuName = txtStuName.Text.Trim();
int classId = (int)cmbClassName.SelectedValue;
string sex = rdoMan.Checked ? rdoMan.Text.Trim() : rdoWoman.Text.Trim();
string phone = txtPhone.Text.Trim();
//2)判空处理 姓名不可以为空 电话不可以为空
if (string.IsNullOrEmpty(stuName))
{
MessageBox.Show("姓名不能为空!", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(phone))
{
MessageBox.Show("电话不能为空!", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//3)判断 姓名+电话 是否在数据库里已存在 姓名+电话
string sql = "select count(1) from StudentInfo where StuName=@StuName and Phone=@phone and StuId<>@StuId";
SqlParameter[] paras =
{
new SqlParameter("@StuName",stuName),
new SqlParameter("@phone",phone),
new SqlParameter ("@StuId",stuId)
};
object o = SqlHelper.ExecuteScalar(sql, paras);
if (o != null && o != DBNull.Value && ((int)o) > 0)
{
MessageBox.Show("该学生已存在,请重新修改!", "修改学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//4)修改入库 sql 参数 执行 完成返回受影响行数
string sqlEdit = " update StudentInfo " +
" set stuName=@StuName, ClassId=@ClassId,Sex=@Sex,Phone=@Phone " +
" where StuId=@StuId ";
SqlParameter[] parasAdd =
{
new SqlParameter("@StuName",stuName),
new SqlParameter("@ClassId",classId),
new SqlParameter("@Sex",sex),
new SqlParameter("@phone",phone),
new SqlParameter ("@StuId",stuId)
};
int count = SqlHelper.ExecuteNonQuery(sqlEdit, parasAdd);
if (count > 0)
{
MessageBox.Show($"学生:{stuName} 修改成功!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.reload.Invoke();
}
else
{
MessageBox.Show("该学生修改失败,请检查!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
有兴趣研究的。可以进下面QQ群,在群在线文档里面进行下载。
简单的winform学生管理系统Demo的更多相关文章
- C++ 实现简单命令行学生管理系统
C++ 实现简单命令行学生管理系统 预览: 编译环境是macOS.system("clear") 在windows下请换成 system("cls") #inc ...
- Winform 学生管理系统增删改查
数据库: create database adonet go use adonet go create table xue ( code ), name ), sex bit, birth datet ...
- 【IOS开发笔记02】学生管理系统
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- 【IOS开发笔记01】学生管理系统(上)
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- Java案例——学生管理系统
简单完整的学生管理系统 学生类 public class Student { private String id; private String age; private String name; p ...
- Java写一个简单学生管理系统
其实作为一名Java的程序猿,无论你是初学也好,大神也罢,学生管理系统一直都是一个非常好的例子,初学者主要是用数组.List等等来写出一个简易的学生管理系统,二.牛逼一点的大神则用数据库+swing来 ...
- php实现简单的学生管理系统
php实现学生管理系统 一.效果 二.代码框架 functions文件夹里面是封装的mysqli的数据库操作函数和一个跳转的函数 student文件夹里面就是学生管理系统的主界面 applicatio ...
- <每日一题>题目7:简单的学生管理系统V1.0
''' # 学生管理系统v1.0 # 添加学生的信息 # 删除学生的信息 # 修改学生的信息 # 查看学生的信息 #遍历学生的信息 #退出系统 ''' import json #1 显示操作功能 de ...
- Java实现功能简单的学生管理系统(附带源代码)
这几天Java学了点新的知识,打算要用这些知识做一个比较简单的管理系统,实战一下子,代码中的功能简洁,可能不多,但是作为一个练手来了解一个项目是怎么样一点一点思考的还是不错的 一.代码中要实现的功能 ...
- jdbc简单学生管理系统
这个是java连接mysql数据库的一个简单学生系统,通过jdbc连接数据库. 工具类 JDBCuntils. package Student; import java.io.IOException; ...
随机推荐
- [ABC262G] LIS with Stack
Problem Statement There is an empty sequence $X$ and an empty stack $S$. Also, you are given an inte ...
- @Async实现异步任务
1.@Async是SpringBoot自带的一个执行步任务注解 @EnableAsync // 开启异步 @SpringBootApplication public class Application ...
- Chrome扩展开发系列开篇
大家好,我是 dom 哥.这是我关于 Chrome 扩展开发的系列文章,感兴趣的可以 点个小星星. 浏览器现状 研究机构 Statcounter 发布了 2023 年 9 月报告,揭示了有关浏览器的最 ...
- Kernel Memory 入门系列:生成并获取文档摘要
Kernel Memory 入门系列:生成并获取文档摘要 前面在RAG和文档预处理的流程中,我们得到一个解决方案,可以让用户直接获取最终的问题答案. 但是实际的业务场景中,仍然存在一些基础的场景,不需 ...
- 《最新出炉》系列初窥篇-Python+Playwright自动化测试-36-处理web页面定位toast-下篇
1.简介 按理说,现在这种一闪而过的toast的已经相当普及或者是见怪不怪了,应该网上的大网站会用到的,偶然的在一次租房中,看到了这种场景,所以宏哥决定将其拿来主义,进行演示实践一下. 2.租房网站 ...
- MySQL|mysql-索引
1.索引是什么 1.1索引简介 索引是表的目录,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,以及快速定位查询数据.对于索引 ...
- CSS3学习笔记-句子排版效果
CSS3提供了丰富的排版效果,可以通过样式属性来控制文本的排列方式.字体样式.行高.字间距等.以下是一些常用的句子排版效果示例: 文本对齐方式: .text-center { text-align: ...
- 面试官:请说一下Mysql事务实现原理
在日常工作中,数据库是我们必须使用的,其中使用最多的也是大部分中小公司的选择是Mysql,跳槽面试中也是必问的,今天我们就说一下Mysql事务 MySQL中的事务实现原理主要涉及以下几个方面: ACI ...
- 打造 VSCode 高效 C++ 开发环境的必备插件
工欲善其事,必先利其器 C++ clangd:代码补全.跳转.clang-tidy 检查,自带 clang-format CodeLLDB:LLVM 的调试器(类比 GDB) CMake CMake ...
- JavaImprove--Lesson03--String的工具类,Math,Runtime,BigDecimal,Date
一String的工具类 String的作为字符串对象,也是使用最多的数据类型对象 所以难免有很多操作,字符串的常见操作包括:字符串拼接,字符串反转,字符串长度,字符串转换等 直接使用String类型来 ...