精美学生管理系统
半个学期即将完美结束,精美的学生管理系统也终于亮相啦,哈哈!
话不多说,切入正题。
首先,要了解完成学生管理系统的步骤:
总共有几个模块,我就逐一讲解:
完成登录页面模块:
判断“登录”是否成功的代码:
1 private void btnLogin_Click(object sender, EventArgs e) 2 { 3 string str = DBHelper.str; 4 SqlConnection con = new SqlConnection(str); 5 string sql = "select count(1) from student where studentname='" + txtnamea.Text + "' and loginpwd='" + txtpwdtu.Text + "' "; 6 SqlCommand cmd = new SqlCommand(sql,con); 7 con.Open(); 8 int count = Convert.ToInt32(cmd.ExecuteScalar()); 9 if (count > 0) 10 { 11 //MessageBox.Show("登陆成功"); 12 this.Hide(); 13 //Welcome frm = new Welcome(); 14 15 //frm.name= txtnamea.Text; 16 //frm.Show(); 17 } 18 con.Close(); 19 //用户名,密码和登录类型都不能为空 20 if (CheckInput()) 21 { 22 //显示系统管理员主窗体 23 //隐藏登录窗体 24 this.Hide(); 25 } 26 FrmAdminMain frm = new FrmAdminMain(); 27 //方法1: 28 //frm.pwd=txtpwdtu.Text; 29 //方法2: 30 Tool.pwd = txtpwdtu.Text; 31 frm.Show(); 32 }
1 if (this.txtnamea.Text.Trim().Equals(string.Empty)) 2 { 3 MessageBox.Show("请输入用户名", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); 4 this.txtnamea.Focus(); 5 return false; 6 } 7 else if (this.txtpwdtu.Text.Trim().Equals(string.Empty)) 8 { 9 MessageBox.Show("请输入密码", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); 10 this.txtpwdtu.Focus(); 11 return false; 12 } 13 else if (this.cboLoginType.Text.Trim().Equals(string.Empty)) 14 { 15 MessageBox.Show("请输入登录类型", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); 16 this.cboLoginType.Focus(); 17 return false; 18 } 19 else 20 { 21 return true; 22 }
修改密码的窗体:
修改密码的代码:
1 private void FrmChangePwd_Load(object sender, EventArgs e) 2 { 3 //颜色变化 4 this.BackColor = Color.Red; 5 //显示旧密码到就文本框里面 6 txtoldpwd.Text = Tool.pwd; 7 } 8 9 private void btnqueren_Click(object sender, EventArgs e) 10 { 11 string num = txtnewpwd.Text; 12 string str = DBHelper.str; 13 14 string sql = "update student set loginpwd='" + num + "' where loginpwd='" + txtoldpwd.Text + "'"; 15 SqlConnection con = new SqlConnection(str); 16 SqlCommand cmd = new SqlCommand(sql, con); 17 con.Open(); 18 if (txtconfrimpwd.Text.Equals(num)) 19 { 20 int com = Convert.ToInt32(cmd.ExecuteScalar()); 21 } 22 23 DialogResult result = MessageBox.Show("确定要修改密码吗?","温馨提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question); 24 if (result==DialogResult.Yes) 25 { 26 this.Close(); 27 } 28 con.Close();
制作管理员窗体:
怎样做才使“窗体跳转”的代码:
//选择"新增学生"时打开"编辑学生信息"窗体 private void 编辑学生信息ToolStripMenuItem_Click(object sender, EventArgs e) { FrmEditStudent frm = new FrmEditStudent(); frm.MdiParent = this; frm.Show(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("确认要退出吗?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { this.Close(); } } //选择“窗口”中的查询学生时显示的“窗体” private void 查询学生信息ToolStripMenuItem_Click(object sender, EventArgs e) { FrmSearchByName frm = new FrmSearchByName(); frm.MdiParent = this.MdiParent; frm.Show(); } //选择“帮助”中的”关于“时显示的窗体 private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { FrmAbout frm = new FrmAbout(); frm.MdiParent = this.MdiParent; frm.Show(); } private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e) { FrmChangePwd frm = new FrmChangePwd(); frm.MdiParent = this.MdiParent; //让修改密码前,给其pwd赋值 //frm.pwd = pwd; frm.Show(); } private void toolStripDropDownButton1_Click_2(object sender, EventArgs e) { FrmEditStudent frm = new FrmEditStudent(); frm.MdiParent = this.MdiParent; frm.Show(); } private void toolStripDropDownButton2_Click(object sender, EventArgs e) { FrmSearchByName frm = new FrmSearchByName(); frm.MdiParent = this.MdiParent; frm.Show();
编辑学生窗体:
怎么样才能得到所有“年级名称”的代码:
1 //加载年级到下拉框信息 2 public void LoadGrade() { 3 4 //拿到所有年级名称 5 string str = DBHelper.str; 6 SqlConnection con = new SqlConnection(str); 7 string sql = "select gradename from grade"; 8 SqlCommand cmd = new SqlCommand(sql, con); 9 10 con.Open(); 11 SqlDataReader dr = cmd.ExecuteReader(); 12 13 if (dr != null) 14 { 15 if (dr.HasRows) 16 { 17 while (dr.Read()) 18 { 19 string gname = dr["gradename"].ToString(); 20 //将年级放到下拉框中 21 cbograde.Items.Add(gname); 22 } 23 } 24 }
拿到所有“学生信息”的代码:
1 //通过学生学号更改信息 2 public void LoadStudentbyStudentno() { 3 string str = DBHelper.str; 4 SqlConnection con = new SqlConnection(str); 5 6 string sql = "select *from student where studentno=" + studentNo + ""; 7 SqlCommand cmd = new SqlCommand(sql,con); 8 9 try 10 { 11 con.Open(); 12 SqlDataReader dr = cmd.ExecuteReader(); 13 //一列拆解,复制到当前的文本框 14 while (dr.Read()) 15 { 16 //获取名字 17 string stuname = dr["studentname"].ToString(); 18 txtname.Text = stuname; 19 //获取电话 20 string stuphone = dr["phone"].ToString(); 21 txtphone.Text = stuphone; 22 //获取地址 23 string stuadress = dr["address"].ToString(); 24 txtadress.Text = stuadress; 25 //获取邮件 26 string stuemail= dr["email"].ToString(); 27 txtemail.Text = stuemail; 28 ////获取密码 29 //string strpwd=dr["LoginPwd"].ToString(); 30 //txtpwd.Text = strpwd; 31 ////确认密码 32 //txttruepwd.Text = strpwd; 33 //获取出生日期 34 DateTime day =Convert.ToDateTime( dr["birthday"]); 35 cbobirthdatetime.Value = day; 36 //获取年级名称 37 int getid = Convert.ToInt32(dr["gradeid"]); 38 string gradename = GetidName(getid); 39 cbograde.Text = gradename; 40 //获取性别 41 string genders=dr["gender"].ToString(); 42 if (genders.Equals("0")) 43 { 44 rbtnFemale.Checked = true; 45 } 46 if(genders.Equals("1")) { 47 48 rbtnMale.Checked = true; 49 } 50
将“年级编号”转化为“年级名称”的代码:
1 //将年级编号转换为年级名称 2 public string GetidName(int getid) 3 { 4 string str = DBHelper.str; 5 SqlConnection con = new SqlConnection(str); 6 7 string sql = "select gradename from grade where gradeid=" + getid + ""; 8 SqlCommand cmd = new SqlCommand(sql, con); 9 string getname = ""; 10 try 11 { 12 con.Open(); 13 getname = cmd.ExecuteScalar().ToString(); 14 } 15 catch (Exception) 16 { 17 18 } 19 finally 20 { 21 con.Close(); 22 } 23 return getname;
当你点击“修改”的时候,”编辑窗体“变为“修改信息的窗体”的代码:
1 private void FrmEditStudent_Load(object sender, EventArgs e) 2 { 3 LoadGrade(); 4 //当点修改按钮时,变成修改窗体 5 if (studentNo != 0) 6 { 7 this.Text = "修改学生信息"; 8 btnsave.Text = "修改"; 9 } 10 else { 11 this.Text = "编辑学生信息"; 12 btnsave.Text = "保存"; 13 } 14 15 txtnano.Text = studentNo.ToString(); 16 LoadStudentbyStudentno(); 17 }
修改“学生信息”的代码:
1 public void updateInfo() { 2 //修改密码 3 string pwd = txtpwd.Text; 4 //修改姓名 5 string studentname = txtname.Text; 6 //修改性别 7 string studentgender = string.Empty; 8 if (rbtnFemale.Checked)//女 9 { 10 studentgender = "0"; 11 } 12 else if (rbtnMale.Checked)//男 13 { 14 studentgender = "1"; 15 } 16 //如何给下拉框绑定数据 17 int getid = GetIdByName(); 18 //修改电话 19 string phone = txtphone.Text; 20 //修改地址 21 string adrers = txtadress.Text; 22 //修改日期 23 DateTime birthdate = this.cbobirthdatetime.Value; 24 //修改邮件 25 string emailt = txtemail.Text; 26 string sql =string.Format("update student set loginpwd='{0}',studentname='{1}',gender='{2}',gradeid='{3}',phone='{4}',address='{5}',birthday='{6}',email='{7}' where studentno={8}", pwd, studentname, studentgender, getid, phone, adrers, birthdate, emailt,studentNo); 27 string str = DBHelper.str; 28 SqlConnection con = new SqlConnection(str); 29 SqlCommand cmd = new SqlCommand(sql, con); 30 con.Open(); 31 int count = cmd.ExecuteNonQuery(); 32 if (count>0) 33 { 34 //调用查询窗口的一个DataSelect()方法 35 frmselectInfo.DataSelect(); 36 MessageBox.Show("修改成功"); 37 } 38 else 39 { 40 MessageBox.Show("失败"); 41 }
1 //根据学号是否为0,判定该按钮用来执行“添加”还是“修改” 2 if (studentNo != 0) 3 { 4 //修改 5 updateInfo(); 6 } 7 else 8 { 9 //添加 10 AddStudent(); 11 }
将“年级名称”变为“年级名称”的代码:
1 //该方法将年级名称转换成年级编号 2 public int GetIdByName() 3 { 4 string str = DBHelper.str; 5 SqlConnection con = new SqlConnection(str); 6 string sql = "select gradeid from grade where gradename='" + cbograde.Text + "'"; 7 SqlCommand cmd = new SqlCommand(sql, con); 8 int getid = 0; 9 try 10 { 11 con.Open(); 12 getid = Convert.ToInt32(cmd.ExecuteScalar()); 13 }
按姓名查询学生信息的窗体:
查询和在Load事件中提前显示的代码:
1 private void FrmSearchByName_Load(object sender, EventArgs e) 2 { 3 //在Load事件中编写代码提前显示 4 //查询 5 string sql = @"select Studentno,Studentname,Gender,gradename 6 from student,grade 7 where student.gradeid=grade.gradeid "; 8 LoadFrmListView(sql); 9 } 10 //查询数据 11 public void DataSelect() 12 { 13 lvlistview.Items.Clear(); 14 string sql = @"select studentno,studentname,Gender,gradename from student,grade 15 where student.gradeid=grade.gradeid and studentname like '%" + txtsearchname.Text + "%'"; 16 LoadFrmListView(sql); 17 }
获取学号的代码:
1 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 4 if (this.lvlistview.SelectedItems.Count > 0) 5 { 6 FrmEditStudent frm = new FrmEditStudent(); 7 //将选中的学号传递到"编辑学生信息窗体" 8 //frm.studentNo = Convert.ToInt32(this.lvlistview.SelectedItems[0].Text);也可以 9 frm.studentNo = Convert.ToInt32(this.lvlistview.SelectedItems[0].SubItems[0].Text); 10 frm.MdiParent = this.MdiParent; 11 frm.frmselectInfo = this; 12 frm.Show();
获取学生信息的代码::
1 //获取学号 2 int stuNo = Convert.ToInt32(dr["studentno"]); 3 //第二列获取姓名 stuname 4 string stuName = Convert.ToString(dr["studentname"]); 5 //第三列 获取 性别 6 string stugender = Convert.ToString(dr["gender"]); 7 //获取年级名称 8 string gname = Convert.ToString(dr["gradename"]); 9 // 10 ListViewItem lvItem = new ListViewItem(stuNo.ToString()); 11 lvItem.SubItems.Add(stuName);//接收学生名字 12 lvItem.SubItems.Add(stugender);//接收学生性别 13 lvItem.SubItems.Add(gname);//接收学生年级名称 14 //让lvItem和ListView关联 15 lvlistview.Items.Add(lvItem); 16 }
关于我们的窗体:
计时器的Tick事件处理方法,定时变换图片框中的图片代码:
1 private void timetime_Tick(object sender, EventArgs e) 2 { 3 //如果当前显示的图片索引没有到最大值就继续增加 4 if (index < imglist.Images.Count - 1) 5 { 6 index++; 7 } 8 else { 9 index = 0; 10 } 11 //设置图片框显示的图片 12 this.pictureBox1.Image = imglist.Images[index];
按年级查询学生信息的的窗体:
加载年级的代码:
1 //加载年级信息 2 public void LoadGradeInfo() { 3 4 //创建数据库连接 5 string str = DBHelper.str; 6 SqlConnection con = new SqlConnection(str); 7 //string sql = "select studentno,studentname,gender,birthday from student"; 8 string sql = "select gradeid,gradename from Grade"; 9 SqlDataAdapter da = new SqlDataAdapter(sql, con); 10 DataSet ds = new DataSet(); 11 da.Fill(ds,"Grade"); 12 DataRow row =ds.Tables["Grade"].NewRow(); 13 row[0]=-1; 14 row[1] = "全部"; 15 ds.Tables["Grade"].Rows.InsertAt(row,0); 16 17 cbograde.DataSource = ds.Tables["Grade"]; 18 //ValueMember下拉框显示的隐藏值 19 cbograde.ValueMember = "gradeid"; 20 //DisplayMember下拉框显示的真实值 21 cbograde.DisplayMember = "Gradename"; 22 cbograde.SelectedIndex = 0; 23 }
修改的代码:
1 //保存修改 2 private void btnSAve_Click(object sender, EventArgs e) 3 { 4 DialogResult result = MessageBox.Show("确定要修改吗?",caption,MessageBoxButtons.YesNo,MessageBoxIcon.Question); 5 if (result==DialogResult.Yes)//确认保存修改 6 { 7 //使用SqlCommandBuilder构建增删改的command操作 8 SqlCommandBuilder sq = new SqlCommandBuilder(da); 9 //将数据集中student表的数据交给数据库更新 10 da.Update(ds, "student"); 11 }
删除信息的窗体:
获取信息代码:
1 public void FillTables(){ 2 string str = DBHelper.str; 3 //取消英文列自动生成,就是没写列名称时,不能用英文列代替 4 dgstudenlist.AutoGenerateColumns = false; 5 SqlConnection con = new SqlConnection(str); 6 string sql = "select studentno,studentname,gender,gradename,phone from student,grade where student.gradeid=grade.gradeid"; 7 SqlDataAdapter da = new SqlDataAdapter(sql, con); 8 DataSet ds = new DataSet(); 9 da.Fill(ds, "student"); 10 this.dgstudenlist.DataSource = ds.Tables["student"]; 11 } 12 private void FrmStudentList_Load(object sender, EventArgs e) 13 { 14 FillTables(); 15 }
删除操作的代码:
1 //确认删除操作 2 DialogResult result = MessageBox.Show("确认删除改学生吗?同时会删除该学生的成绩!!!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 3 if (result == DialogResult.Yes) 4 { 5 if (dgstudenlist.SelectedRows.Count > 0) 6 { 7 //获得选中的学生的学号 8 int studentno = Convert.ToInt32(dgstudenlist.SelectedRows[0].Cells["studentno"].Value); 9 } 10 string str = DBHelper.str; 11 SqlConnection con = new SqlConnection(str); 12 string sql = string.Format("delete from result where studentno={0}", StudentNO); 13 con.Open(); 14 SqlCommand cmd = new SqlCommand(sql, con); 15 int count = cmd.ExecuteNonQuery(); 16 //删除学生信息 17 sql = string.Format("delete from student where studentno={0}", StudentNO); 18 cmd.CommandText = sql; 19 int result1 = cmd.ExecuteNonQuery(); 20 if (result1 == 1) 21 { 22 MessageBox.Show("删除成公!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 23 //重新查询数据,绑定数据源,重新帅选 24 FillTables(); 25 } 26 con.Close();
其中一些重要代码:
1.向数据集年级表的第一行添加数据“全部”或者“请选择”的代码:
1 1 DataRow row = ds.Tables["Grade"].NewRow();//形成行对象 2 2 row["gradeid"] = -1;//给该行的列赋值 3 3 row["gradename"] = "全部"; 4 4 ds.Tables["Grade"].Rows.InsertAt(row, 0);//行对象和Table产生关系 5 5 6 6 7 7 或者 8 8 9 9 //01.形成行对象 table.NewRow() 10 10 DataTable table = ds.Tables[0]; 11 11 DataRow dr=table.NewRow(); 12 12 //02.给该行的列赋值 dr[0]=-1; 13 13 dr[0] = -1; 14 14 dr[1] = "请选择"; 15 15 //03.让行对象和Table产生关系table.Rows.InsertAt() 16 16 table.Rows.InsertAt(dr,0);
2.向下拉框赋值,即年级名称,隐藏年级编号:
1 SqlDataAdapter da = new SqlDataAdapter(sql, con); 2 DataSet ds = new DataSet(); 3 da.Fill(ds, "Grade"); 4 cbograde.DataSource = ds.Tables["Grade"]; 5 //ValueMember下拉框显示的隐藏值gradeid 6 cbograde.ValueMember = "gradeid"; 7 //DisplayMember下拉框显示的真实值Gradename 8 cbograde.DisplayMember = "Gradename"; 9 cbograde.SelectedIndex = 0; //索引为0 10 con.Close();
有不足之处,望大家多多指教哈