界面效果

练习重点

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的更多相关文章

  1. C++ 实现简单命令行学生管理系统

    C++ 实现简单命令行学生管理系统 预览: 编译环境是macOS.system("clear") 在windows下请换成 system("cls") #inc ...

  2. Winform 学生管理系统增删改查

    数据库: create database adonet go use adonet go create table xue ( code ), name ), sex bit, birth datet ...

  3. 【IOS开发笔记02】学生管理系统

    端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...

  4. 【IOS开发笔记01】学生管理系统(上)

    端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...

  5. Java案例——学生管理系统

    简单完整的学生管理系统 学生类 public class Student { private String id; private String age; private String name; p ...

  6. Java写一个简单学生管理系统

    其实作为一名Java的程序猿,无论你是初学也好,大神也罢,学生管理系统一直都是一个非常好的例子,初学者主要是用数组.List等等来写出一个简易的学生管理系统,二.牛逼一点的大神则用数据库+swing来 ...

  7. php实现简单的学生管理系统

    php实现学生管理系统 一.效果 二.代码框架 functions文件夹里面是封装的mysqli的数据库操作函数和一个跳转的函数 student文件夹里面就是学生管理系统的主界面 applicatio ...

  8. <每日一题>题目7:简单的学生管理系统V1.0

    ''' # 学生管理系统v1.0 # 添加学生的信息 # 删除学生的信息 # 修改学生的信息 # 查看学生的信息 #遍历学生的信息 #退出系统 ''' import json #1 显示操作功能 de ...

  9. Java实现功能简单的学生管理系统(附带源代码)

    这几天Java学了点新的知识,打算要用这些知识做一个比较简单的管理系统,实战一下子,代码中的功能简洁,可能不多,但是作为一个练手来了解一个项目是怎么样一点一点思考的还是不错的 一.代码中要实现的功能 ...

  10. jdbc简单学生管理系统

    这个是java连接mysql数据库的一个简单学生系统,通过jdbc连接数据库. 工具类 JDBCuntils. package Student; import java.io.IOException; ...

随机推荐

  1. Java核心知识体系8:Java如何保证线程安全性

    Java核心知识体系1:泛型机制详解 Java核心知识体系2:注解机制详解 Java核心知识体系3:异常机制详解 Java核心知识体系4:AOP原理和切面应用 Java核心知识体系5:反射机制详解 J ...

  2. Tensorflow2.0:使用Keras自定义网络实战

    tensorflow2.0建议使用tf.keras作为构建神经网络的高级API 接下来我就使用tensorflow实现VGG16去训练数据 背景介绍: 2012年 AlexNet 在 ImageNet ...

  3. 算法那么多,AI量化交易策略如何选择最佳算法?

    常见算法优劣比较 算法没有最好,只有更好. 这个问题的答案取决于许多因素,例如股票市场的条件,数据集的质量和特征工程的有效等.接下来,我们来看看这些算法的优势和劣势: 神经网络:适用于复杂的非线性问题 ...

  4. ElasticSearch之Close index API

    关闭指定的索引. 索引关闭之后: 停止对读.写操作的响应. 停止检索操作的响应. 在索引关闭前,允许执行的操作,关闭之后均不允许执行. ElasticSearch取消对索引的相关维护操作,包含内存中的 ...

  5. TDD、BDD、ATDD都是什么、有什么区别?(下)

    在<TDD.BDD.ATDD都是什么.有什么区别?(下)>一文中,探讨了TDD.BDD和ATDD的概念.虽然TDD.BDD和ATDD都是软件开发中使用的测试方法,但它们在方法和重点上有所不 ...

  6. 【wing】一款轻量快捷的团队开发工具

    导航 开源地址:[Github] & [Gitee] 新手使用 更多命令 开发指南 说明 wing是一个代码同步管理工具类似repo,具有以下特性: 支持Winddows .Linux .Ma ...

  7. PowerDotNet平台化软件架构设计与实现系列(17):PCRM个人用户管理平台

    个人用户管理是业务系统中非常基础且重要的一个公共服务系统,我们写的绝大多数应用都和个人用户或会员有关,用户(会员)数据安全无小事,必须有一个完备的用户管理平台系统. 因为不同公司的主业务不同,个人用户 ...

  8. Kubernetes常见错误总结

    1.屏幕持续打印Pod日志报error: unexpected EOF错误 Kubernetes: requesting flag for "kubectl logs" to av ...

  9. 宝塔面板如何用一IP不同端口创建不同的网站(“您添加的站点已存在”)

    问题描述 玩宝塔面板的时候,一开始没有云服务器,需要在本地虚拟机里搭建各种网站,想在本地服务器下搭建多个站点,但是总会遇到"您添加的站点已存在"这个现象. 问题原因及解决办法 出现 ...

  10. 云小课 | ModelArts Pro 自然语言处理套件:高效构建行业高精度文本处理模型

    摘要:ModelArts Pro提供了自然语言处理套件,为客户提供自然语言处理的自定制工具,旨在帮助客户高效地构建行业领域的高精度文本处理模型,可应用于政府.金融.法律等行业. 本文分享自华为云社区& ...