分层开发
No.1实现登陆功能,验证用户名和密码。从数据库里进行匹配,看是否有符合要求的数据。
在DAL层编写代码,返回值为布尔类型。方法参数为(student实体类对象),使用参数化SqlParameter类型防止SQL注入。
1 public bool IsLogin(Student stu) 2 { 3 bool flag = false; 4 SqlConnection con = new SqlConnection(SqlHelper.str); 5 6 string sql = "select count(1) from student where studentNo=@StudentName and LoginPwd=@LoginPwd"; 7 //02 将sql语句交给SQL服务器执行 8 SqlParameter para1 = new SqlParameter("@StudentName", stu.StudentNo); 9 SqlParameter para2 = new SqlParameter("@LoginPwd", stu.LoginPwd); 10 SqlCommand cmd = new SqlCommand(sql,con); 11 cmd.Parameters.Add(para1); 12 cmd.Parameters.Add(para2); 13 try 14 { 15 con.Open(); 16 int count = Convert.ToInt32(cmd.ExecuteScalar()); 17 if (count > 0) 18 { 19 flag = true; 20 } 21 } 22 catch (Exception ex) 23 { 24 25 throw ex; 26 } 27 finally 28 { 29 con.Close(); 30 } 31 return flag; 32 }
在BLL层创建DAL层的对象,调用DAL层的login方法,返回值和参数和DAL层login方法一致,return dal.login(stu)
1 public bool IsLogin(Student stu) 2 { 3 return dal.IsLogin(stu); 4 }
在UI层调用BLL层的方法,并在UI层创建实体类对象,给实体类对象的属性赋值后将对象放入调用的BLL层的方法中。判断登陆是否成功!
1 StudentBLL bll = new StudentBLL(); 2 Student stu = new Student(); 3 stu.StudentNo = Convert.ToInt32(txtName.Text); 4 stu.LoginPwd = txtPwd.Text; 5 bool flag = bll.IsLogin(stu); 6 if (flag) 7 { 8 MessageBox.Show("登陆成功"); 9 frmlist list = new frmlist(); 10 list.Show(); 11 } 12 else 13 { 14 MessageBox.Show("登陆失败"); 15 }
No.2展示学生信息
在DAL层编写方法返回值是泛型集合,无方法参数,使用工具类(SqlHelper类)将DataTable类型转换为泛型
1 public List<Student> Select() 2 { 3 SqlConnection con = new SqlConnection(SqlHelper.str); 4 string sql = "select * from student"; 5 DataTable dt = SqlHelper.ExecuteDataTable(sql); 6 My_Tool tool = new My_Tool(); 7 List<Student> list = tool.DataTableToList<Student>(dt); 8 return list; 9 10 }
在BLL层编写方法与DAL层同理
1 public List<Student> Select() 2 { 3 return dal.Select(); 4 }
在UI层的初始化Select方法()将数据绑定到datagridView中!
1 private void frmlist_Load(object sender, EventArgs e) 2 { 3 Initial(); 4 } 5 public List<Student> list = new List<Student>(); 6 public void Initial() 7 { 8 StudentBLL bll = new StudentBLL(); 9 list= bll.Select(); 10 this.dgvlist.DataSource = list; 11 12 }
No.3实现添加学生信息
Insert语句插入数据的参数较多,所以我们可以声明一个SqlParaMeters类型的数组,节约代码,方法返回值依然是bool类型,方法参数是Student类型的对象。注意!在给Command对象添加参数的时候
要使用AddRange()方法!
1 public bool Insert(Student stu) 2 { 3 bool flag = false; 4 SqlConnection con = new SqlConnection(SqlHelper.str); 5 string sql = "insert into student values(@LoginPwd,@StudentName,@Gender,@GradeId,@Phone,@Address,@Birthday,@Email,@MyTT)"; 6 SqlParameter[] para = 7 { 8 new SqlParameter("@LoginPwd", stu.LoginPwd), 9 new SqlParameter("@StudentName", stu.StudentName), 10 new SqlParameter("@Gender", stu.Gender), 11 new SqlParameter("@GradeId", stu.GradeId), 12 new SqlParameter("@Phone", stu.Phone), 13 new SqlParameter("@Address", stu.Address), 14 new SqlParameter("@Birthday", stu.Birthday), 15 new SqlParameter("@Email", stu.Email), 16 new SqlParameter("@MyTT", stu.MyTT) 17 }; 18 SqlCommand cmd = new SqlCommand(sql,con); 19 cmd.Parameters.AddRange(para); 20 21 try 22 { 23 con.Open(); 24 int count = Convert.ToInt32(cmd.ExecuteScalar()); 25 if (count > 0) 26 { 27 flag = true; 28 } 29 30 } 31 catch (Exception ex) 32 { 33 34 throw ex; 35 } 36 finally 37 { 38 con.Close(); 39 } 40 return flag; 41 }
在BLL层同样实现了方法调用!
1 public bool Insert(Student stu) 2 { 3 return dal.Insert(stu); 4 }
在UI层调用BLL层的方法,给Student类的属性赋值,判断是否添加学生信息成功!
1 StudentBLL bll = new StudentBLL(); 2 Student stu = new Student(); 3 4 stu.LoginPwd = txtPwd.Text; 5 stu.StudentName = txtName.Text; 6 if (rbtnFemale.Checked) 7 { 8 stu.Gender = "0"; 9 } 10 else if (rbtnMan.Checked) 11 { 12 stu.Gender = "1"; 13 } 14 if (cboGrade.SelectedItem.ToString() == "S1") 15 { 16 stu.GradeId = 0; 17 } 18 else if (cboGrade.SelectedItem.ToString() == "S2") 19 { 20 stu.GradeId = 1; 21 } 22 stu.Phone = txtPhone.Text; 23 stu.Address = txtAddress.Text; 24 stu.Birthday = dpBirthday.Value; 25 stu.Email = txtEmail.Text; 26 this.listnew.Add(stu); 27 bool flag= bll.Insert(stu); 28 frm.dgvlist.DataSource = new BindingList<Student>(this.listnew); 29 this.Close();
No.4实现comboBox的两级联动,当选中下拉框中的一项时,另一个下拉框会自动匹配,并且以筛选条件来查询!
在GradeDAL层编写读取所有年级下拉框的方法,返回值为泛型集合,无方法参数
1 public List<Grade> GetGradenew() 2 { 3 List<Grade> list = new List<Grade>(); 4 using (SqlConnection con = new SqlConnection(SqlHelper.str)) 5 { 6 string sql = "select * from Grade"; 7 SqlCommand cmd = new SqlCommand(sql,con); 8 con.Open(); 9 using (SqlDataReader dr = cmd.ExecuteReader()) 10 { 11 while (dr.Read()) 12 { 13 Grade ga = new Grade(); 14 ga.GradeId = Convert.ToInt32(dr["GradeId"]); 15 ga.GradeName = dr["GradeName"].ToString(); 16 list.Add(ga); 17 18 } 19 } 20 21 } 22 return list; 23 }
在BLL层调用该方法!
1 public List<Grade> GetGradenew() 2 { 3 return dal.GetGradenew(); 4 }
在UI 层中调用此方法,comboBox的disPalyMember属性为“gradename”,valuemember属性设置为“gradeid”,将comboBox的DropDownHeight属性设置为106,避免下拉框冗余
1 this.comboBox2.DropDownHeight = 106; 2 this.comboBox1.DataSource = bll.GetGradenew(); 3 this.comboBox1.ValueMember = "GradeId"; 4 this.comboBox1.DisplayMember = "GradeName";
在DAL层编写两个方法,返回值都是泛型,一个方法有返回值,另一个没有,一个方法是为了查询所有科目,另一个是根据年级编号查询科目
public List<Subject> GetSubjectName() { List<Subject> list = new List<Subject>(); using (SqlConnection con = new SqlConnection(SqlHelper.str)) { string sql = "select * from Subject"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Subject sub = new Subject(); sub.SubjectId = Convert.ToInt32(dr["SubjectId"]); sub.SubjectName = dr["SubjectName"].ToString(); sub.GradeId = Convert.ToInt32(dr["GradeId"]); sub.ClassHour = Convert.ToInt32(dr["ClassHour"]); list.Add(sub); } } } return list; } public List<Subject> GetSubjectByGrade(int gradeid) { List<Subject> list = new List<Subject>(); using (SqlConnection con = new SqlConnection(SqlHelper.str)) { string sql = "select * from Subject where gradeId='" + gradeid + "'"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { Subject sub = new Subject(); sub.SubjectId = Convert.ToInt32(dr["SubjectId"]); sub.SubjectName = dr["SubjectName"].ToString(); sub.GradeId = Convert.ToInt32(dr["GradeId"]); sub.ClassHour = Convert.ToInt32(dr["ClassHour"]); list.Add(sub); } } } return list; }
在BLL层调用方法
1 SubjectDAL dal = new SubjectDAL(); 2 public List<Subject> GetSubjectName() 3 { 4 return dal.GetSubjectName(); 5 } 6 public List<Subject> GetSubjectByGrade(int gradeid) 7 { 8 return dal.GetSubjectByGrade(gradeid); 9 } 10 public List<Subject> GetSubjectById(int subjectid) 11 { 12 return dal.GetSubjectById(subjectid); 13 }
在UI层编写代码,注意如果其他事件触发的顺序在窗体load事件之前,使用bool类型变量来调整先后顺序,执行完load事件后flag的值为ture,如果为True,执行其他的事件,在科目下拉框再加入一个请选择,用泛型的Insert方法将其加到下标为0的位置上!
1 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 if (flag) 4 { 5 int num = Convert.ToInt32(this.comboBox1.SelectedValue); 6 7 List < Subject > list= sub.GetSubjectByGrade(num); 8 Subject ject = new Subject(); 9 ject.SubjectId = -1; 10 ject.SubjectName = "请选择"; 11 list.Insert(0, ject); 12 this.comboBox2.ValueMember = "SubjectId"; 13 this.comboBox2.DisplayMember = "SubjectName"; 14 this.comboBox2.DataSource = list; 15 } 16 }
在DAL层编写方法,作为条件查询学生成绩的方法,根据查询条件来设置方法参数和SQL语句,实现多种条件下的模糊查询
1 public DataTable GetList(string name, int id) 2 { 3 using (SqlConnection con = new SqlConnection(SqlHelper.str)) 4 { 5 string sql = "select StudentName,subjectName,studentresult,examdate from student,result,Subject,grade where subject.gradeid=grade.GradeId and result.studentNo=student.studentNo and result.subjectid=subject.subjectid"; 6 if (id != -1) 7 { 8 sql += " and Subject.Subjectid='" + id + "'"; 9 } 10 if (!string.IsNullOrEmpty(name)) 11 { 12 sql += " and StudentName='" + name + "'"; 13 } 14 SqlDataAdapter da = new SqlDataAdapter(sql, con); 15 DataSet ds = new DataSet(); 16 da.Fill(ds, "ss"); 17 return ds.Tables["ss"]; 18 } 19 }
在BLL层调用该方法!
1 public DataTable GetList(string name, int id) 2 { 3 return dal.GetList(name, id); 4 }
在UI层调用该方法,给方法参数赋值,实体类的属性赋值
1 private void button1_Click(object sender, EventArgs e) 2 { 3 string name = textBox1.Text; 4 int subjectid = Convert.ToInt32(comboBox2.SelectedValue); 5 DataTable dt = re.GetList(name, subjectid); 6 this.dataGridView1.DataSource = dt; 7 }