写项目之前呢,先来缕缕思路,既然要写学生管理系统,那肯定上不了从数据库中读取信息,然而想要从数据库中拿到你想要的东西,就要先登录,得到他的权限才行,所以我们第一步就要先搞出一个登录页面并且能连接到数据库的.So 就有了第一张图,至于他怎么来的,咱继续往下看!嘻嘻~
想登陆成功需要在登录里加些代码进去,(双击登录进入代码编辑将下面的代码写进去)
1 //验证用户名密码是否和数据库中匹配 2 string str = "Data source=.;initial catalog=MySchool;uid=sa"; 3 SqlConnection con = new SqlConnection(str); 4 string sql = "select count(1) from student where studentname='" + txtName.Text + "' and loginpwd='" + txtPwd.Text + "'"; 5 6 SqlCommand cmd = new SqlCommand(sql, con); 7 try 8 { 9 con.Open(); 10 int count = Convert.ToInt32(cmd.ExecuteScalar()); 11 if (count > 0) 12 { 13 this.Hide(); 14 //定位到Main界面 15 FrmMain frm=new FrmMain(); 16 Tool.pwd= txtPwd.Text; 17 frm.Show(); 18 19 } 20 } 21 catch (Exception) 22 { 23 MessageBox.Show("网络出错"); 24 } 25 finally 26 { 27 con.Close(); 28 29 } 30 31 } 32 //Load窗体在被用户肉眼看到前就被执行了 33 private void FrmLogin_Load(object sender, EventArgs e) 34 { 35 36 } 37 }
登陆成功后就能进入查询学生系统,所以,我们要写一个前台
生活中的前台是咨询的地方,所以我们的前台也会模仿他们一样,有固定的功能,For example 菜单→修改密码或退出;新建学生信息;查询学生信息;按年级查询学生信息......
下面小博继续为大家讲解.
二、编写前台
菜单栏(menuStrip1);工具栏(toolStrip1);右键(contextMenuStrip1);
下面进行真正的写项目,写具体"功能"了.
三、新建学生用户
为了让用户点击保存用户信息直接添加到数据库中,需要在"保存"里面加入下面的代码
1 private void btnEdit_Click(object sender, EventArgs e) 2 { 3 //根据学号是否为0,判定该按钮用来执行“添加”还是“修改” 4 if (stuno!=0) 5 { 6 //修改 7 //update 8 } 9 else 10 { 11 //添加 12 AddStudent(); 13 } 14 } 15 16 //添加学生 17 public void AddStudent() 18 { 19 //添加学生 20 string pwd = txtPwd.Text;//密码 21 string stuname = txtName.Text;//姓名 22 //获取性别 23 string stugender = string.Empty; 24 if (rbtnMale.Checked) 25 { 26 stugender = "1"; 27 } 28 else 29 { 30 stugender = "0"; 31 } 32 //如何给下拉框绑定数据 33 int gid = GetIdByName(); 34 //电话 35 string stuPhone = txtPhone.Text; 36 37 //地址 38 string stuAddress = txtAddress.Text; 39 //日期 40 DateTime dt = dpBirthday.Value; 41 string stuEmail = txtEmail.Text; 42 //StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Email, MyTT 43 string sql = "insert into student values('" + pwd + "','" + stuname + "'," + stugender + "," + gid + ",'" + stuPhone + "','" + stuAddress + "','" + dt + "','" + stuEmail + "')"; 44 string str = "Data source=.;initial catalog=MySchool;uid=sa"; 45 SqlConnection con = new SqlConnection(str); 46 SqlCommand cmd = new SqlCommand(sql, con); 47 con.Open(); 48 int count = cmd.ExecuteNonQuery(); 49 if (count > 0) 50 { 51 MessageBox.Show("成功!"); 52 } 53 con.Close(); 54 } 55 56 //该方法将年级名称转换成年级编号 57 public int GetIdByName() 58 { 59 string str = "Data source=.;initial catalog=MySchool;uid=sa"; 60 SqlConnection con = new SqlConnection(str); 61 string sql = "select gradeid from grade where gradename='"+cboGrade.Text+"'"; 62 SqlCommand cmd = new SqlCommand(sql, con); 63 int gid = 0; 64 try 65 { 66 con.Open(); 67 gid =Convert.ToInt32(cmd.ExecuteScalar()); 68 } 69 catch (Exception) 70 { 71 72 MessageBox.Show("网络异常"); 73 } 74 finally 75 { 76 con.Close(); 77 } 78 return gid; 79 }
四、从数据库中拿到想要的内容
和上面的方式相同
1 private void btnSearch_Click(object sender, EventArgs e) 2 { 3 4 5 //查询数据前,清空LIstView中的数据 6 lvStuList.Items.Clear(); 7 string sql = @"select Studentno,Studentname,Gender,gradename 8 from student,grade 9 where student.gradeid=grade.gradeid and studentname like '%"+txtStudentName.Text+"%' "; 10 LoadDataFromDBToListView(sql); 11 }
以上就是小博今天为大家"传授"的小步骤,希望能多评价即便小博下次改正~如果有高手高手高高手,请联系小博,小博会悉心请教~谢谢.
No.QQ:1327805794