学生信息查询系统学习总结
来源:互联网 发布:爱淘网 淘宝 编辑:程序博客网 时间:2024/05/17 01:04
程序界面:
具体功能:
1.导入.csv文件中的学生信息,对其进行增,删,改。
2.对选中的学生,展示其详细信息。
3.根据学号,姓名,电话实时查询。
代码实现:
Models类库:
Student.cs://学生类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Models{ /// <summary> /// 实体类:学生信息 /// </summary> public class Student { public string SNO { get; set; } public string Name { get; set; } public string Gender { get; set; } public DateTime Birthday { get; set; } public string Mobile { get; set; } public string Email { get; set; } public string HomeAddress { get; set; } public string PhotoPath { get; set; } }}
DAS类库:
fileOperator.cs://文件操作类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Models;using System.IO;namespace DAS{ public class fileOperator { public List<Student> ReadFile(string filepath)// 把某一个路径的文件装入list并返回 { List<Student> objlist = new List<Student>(); try { string line = string.Empty; StreamReader sr = new StreamReader(filepath, Encoding.Default); line = sr.ReadLine(); while (line != null) { string[] student = line.Split(','); #region////传统写法: //Student temp = new Student(); //temp.SNO = student[0]; //temp.Name = student[1]; //temp.Gender = student[2]; //temp.Birthday =Convert.ToDateTime(student[3]); //temp.Mobile = student[4]; //temp.Email = student[5]; //temp.HomeAddress = student[6]; //temp.PhotoPath = student[7]; //objlist.Add(temp); #endregion //推荐写法:类的初始化器 objlist.Add( new Student { SNO = student[0], Name = student[1], Gender = student[2], Birthday = Convert.ToDateTime(student[3]), Mobile = student[4], Email = student[5], HomeAddress = student[6], PhotoPath = student[7] } ); line = sr.ReadLine(); } sr.Close(); return objlist; } catch (Exception ex) { throw ex;//抛给ui层,所以ui层要接收 } } public void AddStudentToFile(Student stu, List<Student> objstudentlist, string filepath)//向文件追加学生信息 { string studentinfo = "\r\n" + stu.SNO + ',' + stu.Name + ',' + stu.Gender + ',' + stu.Birthday.ToString() + ',' + stu.Mobile + ',' + stu.Email + ',' + stu.HomeAddress + ',' + string.Empty; FileStream fs = null; Encoding encoder = Encoding.GetEncoding("GB2312");//中文编码的换行符:\r\n byte[] bytes = encoder.GetBytes(studentinfo); try { fs = File.OpenWrite(filepath); fs.Position = fs.Length; fs.Write(bytes, 0, bytes.Length); } catch (Exception ex) { throw ex; } finally { fs.Close(); } } public void UpdateStudentToFile(Student stu, string filepath, List<Student> objlist)//更新objstudent { //更改objliststudent数据-->逐行读取-->写入 int index = 0; foreach (Student item in objlist) { if (item.SNO == stu.SNO) { objlist[index] = stu; break; } else { index++; } } } public void loadListofile(List<Student> objlist, string filepath) { FileStream fas = new FileStream(filepath, FileMode.Create, FileAccess.Write); fas.Close(); FileStream fs = null; Encoding encoder = Encoding.GetEncoding("GB2312");//中文编码的换行符:\r\n string studentinfo = string.Empty; try { fs = File.OpenWrite(filepath); foreach (Student item in objlist) { if (item.SNO == "95001") { studentinfo = item.SNO + ',' + item.Name + ',' + item.Gender + ',' + item.Birthday + ',' + item.Mobile + ',' + item.Email + ',' + item.HomeAddress + ',' + item.PhotoPath; } else { studentinfo = "\r\n" + item.SNO + ',' + item.Name + ',' + item.Gender + ',' + item.Birthday + ',' + item.Mobile + ',' + item.Email + ',' + item.HomeAddress + ',' + item.PhotoPath; } byte[] bytes = encoder.GetBytes(studentinfo); fs.Write(bytes, 0, bytes.Length); } } catch (Exception ex) { throw ex; } finally { fs.Close(); } } }}
StudentService.cs://处理学生信息的类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Models;namespace DAS{ public class StudentService { public Student GetStudentBySNO(string SNO, List<Student> objStudentList)//根据学号获取学生信息 { Student objStduent = new Student(); foreach (Student item in objStudentList) { if (item.SNO == SNO) { objStduent = item; return objStduent; } } return objStduent; } public bool WhetherSnoRepeat(string SNO, List<Student> objStudentList)//判断学号是否重复 { bool repeat = false; foreach (Student item in objStudentList) { if (item.SNO == SNO) { repeat = true; } } return repeat; } public List<Student> GetAllStudentBySNO(string SNO, List<Student> objStudentList)//按照学号模糊查询 { List<Student> objListQuery = new List<Student>(); //遍历 foreach (Student item in objStudentList) { if (item.SNO.StartsWith(SNO)) { objListQuery.Add(//一定要实例化! new Student { SNO = item.SNO, Name = item.Name, Email = item.Email, HomeAddress = item.HomeAddress, Birthday = item.Birthday, Gender = item.Gender, PhotoPath = item.PhotoPath, Mobile = item.Mobile, } ); } } return objListQuery; } public List<Student> GetAllStudentByName(string Name, List<Student> objStudentList)//按照姓名模糊查询 { List<Student> objListQuery = new List<Student>(); //遍历 foreach (Student item in objStudentList) { if (item.Name.StartsWith(Name)) { objListQuery.Add(//一定要实例化! new Student { SNO = item.SNO, Name = item.Name, Email = item.Email, HomeAddress = item.HomeAddress, Birthday = item.Birthday, Gender = item.Gender, PhotoPath = item.PhotoPath, Mobile = item.Mobile, } ); } } return objListQuery; } public List<Student> GetAllStudentByMobile(string Mobile, List<Student> objStudentList)//按照电话模糊查询 { List<Student> objListQuery = new List<Student>(); //遍历 foreach (Student item in objStudentList) { if (item.Mobile.StartsWith(Mobile)) { objListQuery.Add(//一定要实例化! new Student { SNO = item.SNO, Name = item.Name, Email = item.Email, HomeAddress = item.HomeAddress, Birthday = item.Birthday, Gender = item.Gender, PhotoPath = item.PhotoPath, Mobile = item.Mobile, } ); } } return objListQuery; } public List<Student> AddStudent(string sno, string name, string gender, string birthday, string mobile, string email, string homeaddress, string photopath, List<Student> objStudentList)//加新学生信息 { objStudentList.Add( new Student { SNO = sno, Name = name, Email = email, Birthday = Convert.ToDateTime(birthday), Mobile = mobile, PhotoPath = photopath, Gender = gender, HomeAddress = homeaddress, } ); return objStudentList; } }}
程序入口:
frmMain.cs:
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;using Models;using DAS;namespace StudentManage{ public partial class frmMain : Form { #region 全局变量 string filename = string.Empty;//打开的文件名 private List<Student> objListStudent = new List<Student>();//定义存储学生信息的list,因为Student类在Models类库中,所以要添加引用 private Student CurrentStudent = new Student();//当前学生 List<Student> TempList = new List<Student>();//查询时用到的templist public int actionflag = 0;//标志添加还是修改 StudentService ss = new StudentService();//实例化 fileOperator objfile = new fileOperator(); #endregion public frmMain() { InitializeComponent(); gboxDetails.Enabled = false; //禁用按钮,只有导入数据可用 ForbidWidget(); btnImport.Enabled = true; } //与用户直接交互的写在UI层,就是这里 //控件事件 private void btnImport_Click(object sender, EventArgs e)//点击导入数据 { //导入数据:选择文件-->读取文件-->liststudent-->展示在datagridview-->展示某一行明细 //打开选择文件的窗体 OpenFileDialog OpenFile = new OpenFileDialog(); OpenFile.Filter = "csv文件(*.csv)|*.csv|TXT文件(*.txt)|*.txt|所有文件(*.*)|*.*"; if (OpenFile.ShowDialog() == DialogResult.OK) { filename = OpenFile.FileName; } //把文件读到list中 try { objListStudent = objfile.ReadFile(filename); } catch (Exception ex) { MessageBox.Show("读取文件出错,具体原因:" + ex.Message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information); } //把list展示在datagridview dgvStudentdata.DataSource = null; dgvStudentdata.AutoGenerateColumns = false;//使datagridview只显示绑定的几列 dgvStudentdata.DataSource = objListStudent; //展示学生明细 CurrentStudent = ss.GetStudentBySNO(dgvStudentdata.CurrentRow.Cells[0].Value.ToString(), objListStudent); LoadDataToDetail(CurrentStudent); //启用按钮,导入数据不可用 AllowWidget(); btnImport.Enabled = false; } private void dgvStudentdata_SelectionChanged(object sender, EventArgs e)//选中行改变,详细信息改变 { //展示学生明细 CurrentStudent = ss.GetStudentBySNO(dgvStudentdata.CurrentRow.Cells[0].Value.ToString(), objListStudent); LoadDataToDetail(CurrentStudent); } private void txtQuerySno_TextChanged(object sender, EventArgs e)//根据学号查询 { TempList = ss.GetAllStudentBySNO(txtQuerySno.Text, objListStudent); dgvStudentdata.DataSource = null; dgvStudentdata.Rows.Clear(); dgvStudentdata.AutoGenerateColumns = false; dgvStudentdata.DataSource = TempList; } private void txtQueryName_TextChanged(object sender, EventArgs e)//根据姓名查询 { TempList = ss.GetAllStudentByName(txtQueryName.Text, objListStudent); dgvStudentdata.DataSource = null; dgvStudentdata.Rows.Clear(); dgvStudentdata.AutoGenerateColumns = false; dgvStudentdata.DataSource = TempList; } private void txtQuerytel_TextChanged(object sender, EventArgs e)//根据电话查询 { TempList = ss.GetAllStudentByMobile(txtQuerytel.Text, objListStudent); dgvStudentdata.DataSource = null; dgvStudentdata.Rows.Clear(); dgvStudentdata.AutoGenerateColumns = false; dgvStudentdata.DataSource = TempList; } private void btnAdd_Click(object sender, EventArgs e)//添加学生(做准备) { //把当前行设置成最后一行,方便查看 dgvStudentdata.CurrentCell = dgvStudentdata.Rows[dgvStudentdata.RowCount - 1].Cells[0]; ForbidWidget(); gboxDetails.Enabled = true; ClearDetail(); actionflag = 1; } private void btnUpdate_Click(object sender, EventArgs e)//修改学生信息(做准备) { ForbidWidget(); gboxDetails.Enabled = true; ClearDetail(); //SNO不能修改 txtDetailSno.Enabled = false; actionflag = 2; } private void btnCancel_Click(object sender, EventArgs e)//删除学生信息(做准备) { actionflag = 2; } private void btnCommit_Click(object sender, EventArgs e)//提交学生信息 { string gender = rBtnBoy.Checked ? "男" : "女"; switch (actionflag) { case 1: //检查输入 if (CheckInformation()) { //添加学生 objListStudent = ss.AddStudent(txtDetailSno.Text, txtDetailName.Text, gender, dtpBirth.Value.ToString(), txtdetailTelphone.Text, txtEmail.Text, txtAddress.Text, string.Empty, objListStudent); dgvStudentdata.DataSource = null; dgvStudentdata.Rows.Clear(); dgvStudentdata.AutoGenerateColumns = false; dgvStudentdata.DataSource = objListStudent; //把添加的学生信息写入文件 Student stu = new Student(); stu = objListStudent[objListStudent.Count - 1]; objfile.AddStudentToFile(stu, objListStudent, @"C:\Users\sisheng\Desktop\学生信息.csv"); //添加完后刷新datagridview dgvStudentdata.DataSource = null; dgvStudentdata.Rows.Clear(); dgvStudentdata.DataSource = objListStudent; //添加后把当前行设置到新添加的学生 dgvStudentdata.CurrentCell = dgvStudentdata.Rows[dgvStudentdata.RowCount - 1].Cells[0]; ClearDetail(); } break; case 2: //根据要修改的学生的学号,查找文件中对应的行-->把这行删掉-->写入相应的行 Student UpdateStudent = new Student() { SNO = txtDetailSno.Text, Name = txtDetailName.Text, Email = txtEmail.Text, Birthday = Convert.ToDateTime(dtpBirth.Text), Mobile = txtdetailTelphone.Text, PhotoPath = string.Empty, Gender = gender, HomeAddress = txtAddress.Text, }; //将文件中的信息更新 objfile.UpdateStudentToFile(UpdateStudent,@"C:\Users\sisheng\Desktop\学生信息.csv",objListStudent); objfile.loadListofile(objListStudent, @"C:\Users\sisheng\Desktop\学生信息.csv"); MessageBox.Show("修改成功!"); //更新datagridview dgvStudentdata.DataSource = null; dgvStudentdata.Rows.Clear(); dgvStudentdata.DataSource = objListStudent; break; default: break; } } private void btnRevoke_Click(object sender, EventArgs e)//取消 { switch (actionflag) { case 1: ClearDetail(); //使标志置零 actionflag = 0; //按钮的恢复启用 AllowWidget(); btnImport.Enabled = false; gboxDetails.Enabled = false; break; case 2: ClearDetail(); //使标志置零 actionflag = 0; //按钮的恢复启用 AllowWidget(); btnImport.Enabled = false; gboxDetails.Enabled = false; break; default: break; } } //自定义方法 private void LoadDataToDetail(Student student)//显示学生详细信息 { txtDetailSno.Text = student.SNO; txtDetailName.Text = student.Name; txtAddress.Text = student.HomeAddress; txtdetailTelphone.Text = student.Mobile; txtEmail.Text = student.Email; //(student.Gender == "男") ? rBtnBoy.Checked = true : rBtnGirl.Checked = true; if (student.Gender == "男") { rBtnBoy.Checked = true; } else if (student.Gender == "女") { rBtnGirl.Checked = true; } dtpBirth.Text = student.Birthday.ToString(); } private void ForbidWidget()//禁止控件 { gboxQuery.Enabled = false; dgvStudentdata.Enabled = false; btnAdd.Enabled = false; btnCancel.Enabled = false; btnImport.Enabled = false; btnUpdate.Enabled = false; } private void AllowWidget()//启用控件 { gboxQuery.Enabled = true; dgvStudentdata.Enabled = true; btnAdd.Enabled = true; btnCancel.Enabled = true; btnImport.Enabled = true; btnUpdate.Enabled = true; } private void ClearDetail()//清空详细信息框 { txtDetailName.Text = string.Empty; txtdetailTelphone.Text = string.Empty; txtEmail.Text = string.Empty; txtAddress.Text = string.Empty; rBtnBoy.Checked = false; rBtnGirl.Checked = false; dtpBirth.Value = DateTime.Now; } private bool CheckInformation()//检查用户输入 { bool b = true; if (string.IsNullOrWhiteSpace(txtDetailSno.Text))//学号为空 { MessageBox.Show("学号不能为空", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information); txtDetailSno.Focus(); b = false; } if (string.IsNullOrWhiteSpace(txtDetailName.Text))//姓名为空 { MessageBox.Show("姓名不能为空", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information); txtDetailName.Focus(); b = false; } if (actionflag == 1) { if (ss.WhetherSnoRepeat(txtDetailSno.Text.Trim(), objListStudent))//null和string.empty是不同的! { MessageBox.Show("该学号已经存在"); txtDetailSno.Focus(); b = false; ; } } return b; } }}
学习收获:
1.C#面向对象的设计方法
2.C#对list的使用
3.C#的文件操作
4.visual studio 的使用技巧
5.datagriview控件使用
限于文章篇幅,这些将分别用五篇文章来总结。
阅读全文
0 0
- 学生信息查询系统学习总结
- 学生管理系统信息查询部分主要功能总结
- 简易学生信息查询系统
- 学生信息查询--JDBC学习
- 学生信息查询系统(有点问题)
- jsp+servlet学生信息查询系统小结
- 学生信息管理系统之查询数据信息
- 学生信息查询器
- 登录查询学生信息
- 学生信息查询
- 做学生信息录入删除查询修改系统的心得
- 数据结构二分排序之学生信息查询简单系统
- 学生信息管理系统之 查询学籍信息流程
- 【学生信息管理系统 4】:有趣的信息查询
- 【Java】StudentsInfoQuery(简单的学生信息查询系统)
- 学生学籍管理系统_学生登陆系统查询与修改信息
- sql实现查询学习了所有课程的学生信息
- 学生信息小系统
- Linux虚拟机和主机三种网络连接方式
- 将list按blockSize大小等分,最后多余的单独一份
- PHP新特性 闭包
- jmap,jhat分析内存
- 数据结构与算法专题之树——二叉树的遍历及应用
- 学生信息查询系统学习总结
- Maze (opentrains)
- Ubuntu使用C调用Shell
- Conscription (Kruskal+优先队列)
- 2.修改/etc/hosts文件 /安装SSH、配置SSH无密码登陆(全程切换到hadoop用户下操作)
- docker部署laravel报错界面只有whoops没有调试信息
- (25) 异常 (下) / 计算机程序的思维逻辑
- 小游戏之飞机躲子弹
- 拍黄片