实验题目
开发一个采用XML格式存储数据的学生通讯录myContracts,具备学生信息添加,编辑、删除、查找等功能。学生通讯录myContracts是一个多窗体桌面程序,不同窗体上面分别布置了工具条、按钮、数据列表等控件,便于操作学生信息。
实验方案
![](https://img.haomeiwen.com/i17996437/c4d0a0bc6478e022.png)
界面设计
![](https://img.haomeiwen.com/i17996437/9dc9c583c6808062.png)
![](https://img.haomeiwen.com/i17996437/fe292e3a9dcf869b.png)
![](https://img.haomeiwen.com/i17996437/5e8d6511e2b562ff.png)
![](https://img.haomeiwen.com/i17996437/fa27dcb877418b3c.png)
功能展示
![](https://img.haomeiwen.com/i17996437/263df118042d5b9e.png)
![](https://img.haomeiwen.com/i17996437/88e03e039a7bae22.png)
![](https://img.haomeiwen.com/i17996437/79e2cb31e1973579.png)
拓展功能
分类功能
![](https://img.haomeiwen.com/i17996437/bd0e73f1a9224240.png)
数据备份和恢复功能
![](https://img.haomeiwen.com/i17996437/ccac774dfd52c7c0.png)
实验总结
本次实验整体代码难度不高,主要运用了调用XML文件的功能,主要实现运用C#对XML文件进行操作,其中包括对文件的添加,编辑,删除,查找等操作,还有就是对VS中的各种插件的熟练运用,对其功能的熟悉是实现这个实验的关键。
最后就是对于扩展的功能也是插件的使用,在插件的函数中插入查找的功能便能对其专业信息进行分类输出,备份和恢复的功能可以直接调用XML文件的文件拷贝函数就可以进行备份和恢复。
一些其他功能比如可以对学号进行排序,这个也是可以实现的,只是还没有在程序中进行实现,这个得加以改进。
源码展示
Form_main.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 System.IO;
namespace 学生通讯录
{
public partial class Form_main : Form
{
public Form_main()
{
InitializeComponent();
initContracts();
}
void initContracts()
{
if (File.Exists(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Students.xml"))
{
dataGridView1.DataSource = StudentInfoBLL.GetAllStudentInfo();
}
else
{
StudentInfoBLL.CreateStudentXml();
dataGridView1.DataSource = StudentInfoBLL.GetAllStudentInfo();
}
dataGridView1.Columns[0].HeaderText = "学生编号";
dataGridView1.Columns[1].HeaderText = "学生姓名";
dataGridView1.Columns[2].HeaderText = "学生性别";
dataGridView1.Columns[3].HeaderText = "学生年龄";
dataGridView1.Columns[4].HeaderText = "出生日期";
dataGridView1.Columns[5].HeaderText = "手机号码";
dataGridView1.Columns[6].HeaderText = "家庭住址";
dataGridView1.Columns[7].HeaderText = "电子邮箱";
dataGridView1.Columns[8].HeaderText = "专 业";
}
void initContracts2()
{
dataGridView1.Columns[0].HeaderText = "学生编号";
dataGridView1.Columns[1].HeaderText = "学生姓名";
dataGridView1.Columns[2].HeaderText = "学生性别";
dataGridView1.Columns[3].HeaderText = "学生年龄";
dataGridView1.Columns[4].HeaderText = "出生日期";
dataGridView1.Columns[5].HeaderText = "手机号码";
dataGridView1.Columns[6].HeaderText = "家庭地址";
dataGridView1.Columns[7].HeaderText = "电子邮箱";
dataGridView1.Columns[8].HeaderText = "专 业";
}
private void Form_main_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void toolStrip_add_Click(object sender, EventArgs e)
{
Form_Add formadd = new Form_Add();
formadd.ShowDialog();
initContracts();
//调用initContracts()函数,功能是如果存在Students.xml,查询所有学生信息,如果不存在,
//则创建该文件后再查询
}
private void toolStrip_edit_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)//如果选中其中某一个学生信息后,则打开编辑学生信息窗体
{
int selectrow = Int32.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString());
Form_edit formedit = new Form_edit();
formedit.studentid_edit = selectrow;
formedit.ShowDialog();
initContracts();
}
else
MessageBox.Show("请选中一行再点击编辑");
}
private void toolStrip_delete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
if (MessageBox.Show("确定要删除此学生信息?", "确认信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
int selectrow = Int32.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString());
if (StudentInfoBLL.DeleteStudentInfo(selectrow))
MessageBox.Show("删除学生信息成功!");
else
MessageBox.Show("删除学生信息失败,请检查是否选中学生信息!");
initContracts();
}
}
else
MessageBox.Show("请选中一行后再点击删除按钮!");
}
private void toolStrip_search_Click(object sender, EventArgs e)
{
Form_Search formsearch = new Form_Search();
formsearch.ShowDialog();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{//备份
StreamWriter myStream;
saveFileDialog1.Filter = "All files (*.xml)|*.xml";//设置文件格式
saveFileDialog1.FilterIndex = 2;//设置系统样式
saveFileDialog1.RestoreDirectory = true;//关闭当前对话框
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
initContracts();
string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Students.xml";//读取原文件的内容
string Path = saveFileDialog1.FileName.ToString();
myStream = new StreamWriter(saveFileDialog1.FileName);
myStream.Write(Path); //写入
myStream.Close();//关闭流
System.IO.File.Copy(_basePath, Path, true);
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{//恢复
OpenFileDialog fileName = new OpenFileDialog();
fileName.InitialDirectory = Application.StartupPath;
fileName.Filter = "All files (*.xml)|*.xml";
fileName.FilterIndex = 2;
fileName.RestoreDirectory = true;
if (fileName.ShowDialog() == DialogResult.OK)
{
string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Students.xml";
string Path = fileName.FileName.ToString();
initContracts();
System.IO.File.Copy(Path, _basePath, true);//把选中的内容恢复到原文件里
string Name = Path.Substring(Path.LastIndexOf("\\") + 1);
dataGridView1.DataSource = StudentInfoBLL.GetAllStudentInfo();
dataGridView1.Columns[0].HeaderText = "学生编号";
dataGridView1.Columns[1].HeaderText = "学生姓名";
dataGridView1.Columns[2].HeaderText = "学生性别";
dataGridView1.Columns[3].HeaderText = "学生年龄";
dataGridView1.Columns[4].HeaderText = "出生日期";
dataGridView1.Columns[5].HeaderText = "手机号码";
dataGridView1.Columns[6].HeaderText = "家庭地址";
dataGridView1.Columns[7].HeaderText = "电子邮箱";
dataGridView1.Columns[8].HeaderText = "专 业";
}
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)//学生专业的层次分类
{
if(treeView1.SelectedNode.Text=="全部")
{
dataGridView1.DataSource = StudentInfoBLL.GetAllStudentInfo();
initContracts2();
}
if (treeView1.SelectedNode.Text == "计算机应用")
{
StudentInfo studentsearch = new StudentInfo();
studentsearch.Profession = treeView1.SelectedNode.Text;
dataGridView1.DataSource = StudentInfoBLL.GetStudentProfession(studentsearch);
initContracts2();
}
if (treeView1.SelectedNode.Text == "信息安全")
{
StudentInfo studentsearch = new StudentInfo();
studentsearch.Profession = treeView1.SelectedNode.Text;
dataGridView1.DataSource = StudentInfoBLL.GetStudentProfession(studentsearch);
initContracts2();
}
if (treeView1.SelectedNode.Text == "电子技术")
{
StudentInfo studentsearch = new StudentInfo();
studentsearch.Profession = treeView1.SelectedNode.Text;
dataGridView1.DataSource = StudentInfoBLL.GetStudentProfession(studentsearch);
initContracts2();
}
if (treeView1.SelectedNode.Text == "网络技术")
{
StudentInfo studentsearch = new StudentInfo();
studentsearch.Profession = treeView1.SelectedNode.Text;
dataGridView1.DataSource = StudentInfoBLL.GetStudentProfession(studentsearch);
initContracts2();
}
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
Form_add.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 System.Xml.Linq;
namespace 学生通讯录
{
public partial class Form_Add : Form
{
private static string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/xml/Students.xml";
public static bool IfStudentInfo(int studentid)
{
StudentInfo studentinfo = new StudentInfo();
XElement xml = XElement.Load(_basePath);
studentinfo = (from student in xml.Descendants("student")
where student.Attribute("studentid").Value == studentid.ToString()
select new StudentInfo
{
StudentId = Int32.Parse(student.Attribute("studentid").Value),
Name = student.Element("name").Value,
Age = Int32.Parse(student.Element("age").Value),
Sex = student.Element("sex").Value,
BirthDate = DateTime.Parse(student.Element("birthdate").Value),
Phone = student.Element("phone").Value,
HomeAddress = student.Element("homeaddress").Value,
Email = student.Element("email").Value,
Profession = student.Element("profession").Value
}).SingleOrDefault();
if (studentinfo == null)
return false;
else
return true;
}
public Form_Add()
{
InitializeComponent();
}
private void label6_Click(object sender, EventArgs e)
{
}
private void btn_add_Click(object sender, EventArgs e)
{
StudentInfo studentinfo = new StudentInfo();
studentinfo.StudentId = Int32.Parse(txt_studegid.Text);
studentinfo.Name = txt_name.Text;
if (radioButton1.Checked)
studentinfo.Sex = "男";
else if (radioButton2.Checked)
studentinfo.Sex = "女";
studentinfo.Age = Int32.Parse(txt_age.Text);
studentinfo.BirthDate = DateTime.Parse(dateTimePicker1.Text);
studentinfo.Phone = txt_phone.Text;
studentinfo.Email = txt_email.Text;
studentinfo.HomeAddress = txt_homeaddress.Text;
studentinfo.Profession = txt_profession.Text;
if (IfStudentInfo(studentinfo.StudentId) == false)
{
if (StudentInfoBLL.AddStudentInfo(studentinfo))
{
MessageBox.Show("添加学生信息成功!");
}
}
else
{
MessageBox.Show("该学号已存在,请重新输入");
}
}
private void bt_close_Click(object sender, EventArgs e)
{
this.Close();
}
private void label7_Click(object sender, EventArgs e)
{
}
private void Form_Add_Load(object sender, EventArgs e)
{
}
}
}
Form_Search.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;
namespace 学生通讯录
{
public partial class Form_Search : Form
{
void InitHeadTitle()
//用于初始化显示要查找到的学生信息的表头
{
//初始化数据表格dataGridView1
dataGridView1.Columns[0].HeaderText = "学生编号";
dataGridView1.Columns[1].HeaderText = "学生姓名";
dataGridView1.Columns[2].HeaderText = "学生性别";
dataGridView1.Columns[3].HeaderText = "学生年龄";
dataGridView1.Columns[4].HeaderText = "出生日期";
dataGridView1.Columns[5].HeaderText = "手机号码";
dataGridView1.Columns[6].HeaderText = "家庭住址";
dataGridView1.Columns[7].HeaderText = "电子邮箱";
dataGridView1.Columns[8].HeaderText = "专 业";
}
public Form_Search()
{
InitializeComponent();
}
private void Form_Search_Load(object sender, EventArgs e)
{
}
private void bt_search_Click(object sender, EventArgs e)
{
//依据选择的查询项和输入查询值进行查询,如果二者都为空,则查询所有信息
if(cb_searchitem .Text ==string.Empty )
{
dataGridView1.DataSource = StudentInfoBLL.GetAllStudentInfo();
InitHeadTitle();
}
else
{
if (txt_searchtext.Text != string.Empty)
{
StudentInfo studentsearch = new StudentInfo();
switch (cb_searchitem.SelectedIndex)
{
case 0: studentsearch.StudentId = Int32.Parse(txt_searchtext.Text); break;
case 1: studentsearch.Name = txt_searchtext.Text; break;
}
dataGridView1.DataSource = StudentInfoBLL.GetStudentInfoList(studentsearch);
InitHeadTitle();
}
else
MessageBox.Show("请输入要查询的" + cb_searchitem.Text);
}
}
private void bt_close_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
Form_edit.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;
namespace 学生通讯录
{
public partial class Form_edit : Form
{
public int studentid_edit = 0;//要编辑的学生编号,用于在主窗体和编辑窗体间传递
public Form_edit()
{
InitializeComponent();
initControl();
}
public void initControl()
{
//查询要编辑的学生信息并把编辑前的信息显示出来
StudentInfo studentinfo = StudentInfoBLL.GetStudentInfo(studentid_edit);
if(studentinfo!=null )
{
txt_studengid.Text = studentinfo.StudentId.ToString();
txt_name.Text = studentinfo.Name;
if(studentinfo.Sex=="男")
{
rB_男.Checked = true;
rB_女.Checked = false;
}
else
{
rB_女.Checked = true;
rB_男.Checked = false;
}
txt_age.Text = studentinfo.Age.ToString();
dtbt_birthdate.Text = studentinfo.BirthDate.ToString();
txt_phone.Text = studentinfo.Phone;
txt_email.Text = studentinfo.Email;
txt_homeaddress.Text = studentinfo.HomeAddress;
txt_profession.Text = studentinfo.Profession;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void Form_edit_Load(object sender, EventArgs e)
{
initControl();
}
private void btn_update_Click(object sender, EventArgs e)
{
//将编辑后的学生信息更新到XML文件中
StudentInfo studentinfo = StudentInfoBLL.GetStudentInfo(studentid_edit);
studentinfo.StudentId = Int32.Parse(txt_studengid.Text);
studentinfo.Name = txt_name.Text;
if (rB_男.Checked)
studentinfo.Sex = "男";
else if (rB_女.Checked)
studentinfo.Sex = "女";
studentinfo.Age = Int32.Parse(txt_age.Text);
studentinfo.BirthDate = DateTime.Parse(dtbt_birthdate.Text);
studentinfo.Phone = txt_phone.Text;
studentinfo.Email = txt_email.Text;
studentinfo.HomeAddress = txt_homeaddress.Text;
studentinfo.Profession = txt_profession.Text;
if (StudentInfoBLL.UpdateStudentInfo(studentinfo))
MessageBox.Show("修改学生信息成功!");
}
private void btn_close_Click(object sender, EventArgs e)
{
this.Close();//关闭窗体
}
private void txt_studengid_TextChanged(object sender, EventArgs e)
{
}
}
}
网友评论