管理员部分
学生信息,课程信息,选课信息,后台信息,退出登录几个功能在上次已经实现。个人信息功能目前还没做好。
教师信息管理:
界面:
功能展示:
1.新增教师信息
2.修改教师信息
3.查询教师信息
名字处提供模糊查询:
4.删除教师信息
技能GET:
1.单选按钮RadioButton分组:
在一个GroupBox中的RadioButton为一组,一组中只能选择一个按钮。如果不放GroupBox,则整个页面中的所有单选按钮只能选择一个。
2.tabControl组件可以在一个窗体内实现多个页面的切换
用户信息管理
用于管理员直接在后台录入用户和删除用户。
界面:
功能展示:
1.新增用户
2.删除用户
新增用户处和注册界面一致。
学生界面部分
界面:
只有输入学号,才可以选择以下的功能:
我的课表
输入学号201215121为例
点击我的课表功能,直接显示所选课的课表:
该部分代码实现:
private void FormMycourse_stu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet1.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.schoolDataSet1.Course);
String Strsno = label2.Text.Trim();
try
{
con.Open();
String select_by_sno = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + Strsno + "'";
SqlCommand cmd = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = cmd.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}
我的成绩
功能1:仅显示及格成绩
功能2:仅显示不及格成绩
功能3:显示所有成绩(用于点击前两个按钮之后恢复原状)
功能4:成绩排序(规定降序)
当界面仅显示及格成绩时,只对及格成绩排序
排序后:
当界面仅显示不及格成绩时,只对不及格成绩排序
当界面显示所有时,对所有成绩排序
排序前:
排序后:
代码实现:
private void FormGrade_stu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet3.STU_Grade”中。您可以根据需要移动或删除它。
this.sTU_GradeTableAdapter.Fill(this.schoolDataSet3.STU_Grade);
String Strsno = label2.Text.Trim();
String select_by_sno = "select * from STU_Grade where Sno = '" + Strsno + "'";
try
{
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}
int flag = 0;//flag表示按下的哪个按钮,确定当前显示 0 为显示所有成绩,1为显示pass成绩,2为显示no pass成绩
private void button_all_Click(object sender, EventArgs e)
{
if (flag != 0)//当前不是显示所有的时候处理,否则不处理
{
flag = 0;
String Strsno = label2.Text.Trim();
String select_by_sno = "select * from STU_Grade where Sno = '" + Strsno + "'";
try
{
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}
}
private void button_pass_Click(object sender, EventArgs e)
{
if (flag != 1)
{
flag = 1;
String Strsno = label2.Text.Trim();
String select_by_sno = "select * from STU_Grade where Grade >= 60 and Sno = '" + Strsno + "'";
try
{
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}
}
private void button_nopass_Click(object sender, EventArgs e)
{
if (flag != 2)
{
flag = 2;
String Strsno = label2.Text.Trim();
String select_by_sno = "select * from STU_Grade where Grade < 60 and Sno = '" + Strsno + "'";
try
{
con.Open();
SqlCommand sqlCommand = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}
}
private void button_grade_order_Click(object sender, EventArgs e)
{
String Strsno = label2.Text.Trim();
String order_by = "";
if (flag == 0)//对所有成绩排序
{
order_by = "select* from STU_Grade where Sno = '" + Strsno + "' order by Grade DESC";
}//成绩降序排列
else if(flag == 1)//仅对pass成绩排序
{
order_by = "select * from STU_Grade where Grade >= 60 and Sno = '" + Strsno + "' order by Grade DESC";
}
else//仅对no pass成绩排序
{
order_by = "select * from STU_Grade where Grade < 60 and Sno = '" + Strsno + "' order by Grade DESC";
}
try
{
con.Open();
SqlCommand sqlCommand = new SqlCommand(order_by, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}
}
选课/退课
界面:
功能:
显示所有——用于查询课程后恢复对所有课程的查看
文本框——输入课程号
输入课程号,查询课程:
选课:
选课成功:
退课:
代码实现:
private void Formselectcourse_stu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“schoolDataSet.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.schoolDataSet.Course);
String Strsno = label2.Text.Trim();
try
{
con.Open();
String select_by_sno = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + Strsno + "'";
SqlCommand cmd = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = cmd.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}//窗口加载事件
private void button_select_Click(object sender, EventArgs e)
{
if(textBox_select.Text.Trim() == "")
{
MessageBox.Show("请输入你要的选择的课程号!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
String Strselect = textBox_select.Text.Trim();
String strsno = label2.Text.Trim();
try
{
if (flag == 1)//课程存在
{
con.Open();
DialogResult result = MessageBox.Show("确定选择该课程么?", "Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if(result == DialogResult.OK)
{
String insertstr = "Insert into SC VALUES('" + strsno + "','" + Strselect + "',NULL)";
SqlCommand cmd1 = new SqlCommand(insertstr, con);
cmd1.ExecuteNonQuery();
String select_by_sno = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + strsno + "'";
SqlCommand cmd = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = cmd.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
}
}
}
catch
{
flag = 1;
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
if(flag == 0)
{
MessageBox.Show("选课成功!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}//选课
private void button_question_Click(object sender, EventArgs e)
{
try
{
String Strselect = textBox_select.Text.Trim();
con.Open();
String Coursestr = "Select * from Course where Cno = '" + Strselect + "'";
SqlCommand cmd2 = new SqlCommand(Coursestr, con);
SqlDataReader sqlDataReader1 = cmd2.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader1;
dataGridView1.DataSource = bindingSource;
if (sqlDataReader1.HasRows)
{
flag = 1;//课程存在
}
}
catch
{
MessageBox.Show("ERROR!");
}
finally
{
con.Close();
}
}//查询课程
private void button_quit_Click(object sender, EventArgs e)
{
int flag = 0;
String strsno = label2.Text.Trim();
try
{
con.Open();
string select_cno = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值
string delete_by = "delete from SC where Cno=" + "'" + select_cno + "' and Sno = '" + strsno + "'";//sql删除语句,根据学号删除
SqlCommand cmd = new SqlCommand(delete_by, con);
cmd.ExecuteNonQuery(); //执行命令
String select_by_sno = "Select Course.Cno,Cname,Cpno,Ccredit from Course,SC where SC.Cno = Course.Cno AND SC.Sno = '" + strsno + "'";
SqlCommand cmd2 = new SqlCommand(select_by_sno, con);
SqlDataReader sqlDataReader = cmd2.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView2.DataSource = bindingSource;
}
catch
{
flag = 1;
MessageBox.Show("请正确选择行!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
if (flag == 0)
{
MessageBox.Show("成功退课!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
con.Open();
String select_by_id = "select * from Course";
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
}//显示所有
}
退出登录的部分与管理员部分相同。
学生界面部分实现心得:
1.在这几个功能实现中,主要运用了窗体加载事件(Load)
目的是使窗口一打开就显示已经筛选后的结果。
例如:“我的课表”中,需要只显示该学生对应学号所选的课。(SQL连接查询,而后设置dataGridView的数据源为该查询结果)
2.【实现窗体之间的传值】在学生主界面中要求输入Sno,而后要将输入的Sno传输到各个功能打开后的界面。
使用标签label显示,将该label在desigener.cs文件中设置为public属性,然后在创建窗体的时候赋值。
例:
Formselectcourse_stu formselectcourse_Stu = new Formselectcourse_stu();
formselectcourse_Stu.label2.Text = textBox_sno.Text.Trim();
formselectcourse_Stu.ShowDialog();
3.在退课/选课的时候不仅要在课表中显示出,最主要的是SC表中的增删(虽然看不到)。
4.MessageBox显示按钮确定和取消,通过DialogResult对象接收其值,然后通过该对象判断按下的是哪个按钮,进而再做相应处理。
例:
DialogResult result = MessageBox.Show("确定选择该课程么?", "Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if(result == DialogResult.OK)
{...}
else
{...}
5.在“我的成绩”部分,为了使显示课程名和成绩等信息,需要做SC和Course表的连接,但是dataGridView只能设置显示一张表。如果选择SC表做数据源,做连接后显示,也无法显示Cname。而如果不设置数据源或数据源设置为空,直接显示查询结果会出错。(这个我也没咋弄懂)
解决办法是:在SC表和Course表上生成STU_Grade视图,在视图上做相应查询。
【15周心得】
感觉距离胜利越来越近了,虽然这个系统表面上看起来花里胡哨的,但它也并非华而不实。这次很是顺利,遇到的问题没有耽误很长时间,顺利解决。脑子果然是个好东西~