==========================================实体层==========================================
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace MySchool.Models 8 { 9 /// <summary> 10 /// 年级信息 11 /// </summary> 12 public class GradeInfo 13 { 14 /// <summary> 15 /// 年级ID 16 /// </summary> 17 public int GradeId { get; set; } 18 /// <summary> 19 /// 年级名称 20 /// </summary> 21 public string GradeName { get; set; } 22 } 23 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace MySchool.Models 8 { 9 /// <summary> 10 /// 登录信息 11 /// </summary> 12 public class LoginInfo 13 { 14 /// <summary> 15 /// 登录Id 16 /// </summary> 17 public string LoginId { get; set; } 18 /// <summary> 19 /// 登录密码 20 /// </summary> 21 public string LoginPwd { get; set; } 22 } 23 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace MySchool.Models 8 { 9 /// <summary> 10 /// 学生信息实体 11 /// </summary> 12 public class StudentInfo 13 { 14 /// <summary> 15 /// 学生姓名 16 /// </summary> 17 public string StudentName { get; set; } 18 /// <summary> 19 /// 学生学号 20 /// </summary> 21 public string StudentNo { get; set; } 22 /// <summary> 23 /// 性别 24 /// </summary> 25 public string Sex { get; set; } 26 /// <summary> 27 /// 年级 28 /// </summary> 29 public string GradeName { get; set; } 30 /// <summary> 31 /// 电话 32 /// </summary> 33 public string Phone { get; set; } 34 /// <summary> 35 /// 密码 36 /// </summary> 37 public string PWD { get; set; } 38 } 39 }
==========================================访问数据层==========================================
1 using MySchool.Models; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace MySchool.DAL 11 { 12 /// <summary> 13 /// 访问数据层 14 /// </summary> 15 public class MySchoolDAL 16 { 17 /// <summary> 18 /// 获取登录账号、密码 19 /// </summary> 20 /// <returns>List<LoginInfo></returns> 21 public List<LoginInfo> GetLoginInfo() 22 { 23 DBHelper db = new DBHelper(); 24 string sql = "select * from Admin "; 25 //登录信息泛型集合 26 List<LoginInfo> loginInfo = new List<LoginInfo>(); 27 28 db.connection.Open(); 29 SqlCommand cmd = new SqlCommand(sql,db.connection); 30 SqlDataReader reader = cmd.ExecuteReader(); 31 //循环读取sql语句读取出来的数据 32 while (reader.Read()) 33 { 34 //将登录Id 密码 赋给对应属性 35 LoginInfo li = new LoginInfo(); 36 li.LoginId = reader["LoginId"].ToString(); 37 li.LoginPwd = reader["LoginPwd"].ToString(); 38 //添加到登录信息泛型集合 39 loginInfo.Add(li); 40 } 41 42 //关闭 43 reader.Close(); 44 db.connection.Close(); 45 //返回登录信息泛型集合 46 return loginInfo; 47 } 48 49 /// <summary> 50 /// 获取登录账号、密码 51 /// </summary> 52 /// <returns>List<LoginInfo></returns> 53 public List<LoginInfo> LoginInfo() 54 { 55 DBHelper db = new DBHelper(); 56 string sql = "select * from Admin "; 57 //登录信息泛型集合 58 List<LoginInfo> loginInfo = new List<LoginInfo>(); 59 DataSet ds = db.Adapter(sql); 60 foreach (DataRow item in ds.Tables[0].Rows) 61 { 62 LoginInfo li = new LoginInfo(); 63 li.LoginId = item["LoginId"].ToString(); 64 li.LoginPwd = item["LoginPwd"].ToString(); 65 loginInfo.Add(li); 66 } 67 return loginInfo; 68 } 69 70 71 /// <summary> 72 /// 获取数据库年级信息 73 /// </summary> 74 /// <returns></returns> 75 public List<GradeInfo> GetGradeInfo() 76 { 77 string sql = "select * from grade "; 78 List<GradeInfo> gradeInfo = new List<GradeInfo>(); 79 DBHelper db = new DBHelper(); 80 DataSet ds = db.Adapter(sql); 81 foreach (DataRow dr in ds.Tables[0].Rows) 82 { 83 GradeInfo gi = new GradeInfo(); 84 gi.GradeId = (int)(dr["GradeId"]); 85 gi.GradeName = dr["GradeName"].ToString(); 86 gradeInfo.Add(gi); 87 } 88 return gradeInfo; 89 } 90 91 /// <summary> 92 /// 获取学生信息 93 /// </summary> 94 public List<StudentInfo> GetStudentInfo() 95 { 96 List<StudentInfo> studentInfo = new List<StudentInfo>(); 97 string sql = "select * from Student inner join Grade on Grade.GradeId = Student.GradeId "; 98 DBHelper db = new DBHelper(); 99 DataSet ds = db.Adapter(sql); 100 foreach (DataRow dr in ds.Tables[0].Rows) 101 { 102 StudentInfo si = new StudentInfo(); 103 si.GradeName = dr["GradeName"].ToString(); 104 si.Phone = dr["Phone"].ToString(); 105 si.PWD = dr["LoginPwd"].ToString(); 106 if (dr["Gender"].ToString().Equals("True")) 107 { 108 si.Sex = "男"; 109 } 110 else 111 { 112 si.Sex = "女"; 113 } 114 si.StudentName = dr["StudentName"].ToString(); 115 si.StudentNo = dr["StudentNo"].ToString(); 116 studentInfo.Add(si); 117 } 118 return studentInfo; 119 } 120 } 121 }
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace MySchool.DAL 10 { 11 public class DBHelper 12 { 13 static string con = "server=.;database=MySchool;user=sa;pwd=123"; 14 public SqlConnection connection = new SqlConnection(con); 15 16 public DataSet Adapter(string sql) 17 { 18 DataSet ds = new DataSet(); 19 SqlDataAdapter sda = new SqlDataAdapter(sql, connection); 20 sda.Fill(ds); 21 return ds; 22 } 23 } 24 }
==========================================业务逻辑层==========================================
1 using MySchool.DAL; 2 using MySchool.Models; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace MySchool.BLL 10 { 11 /// <summary> 12 /// 业务逻辑层 13 /// </summary> 14 public class MySchoolBLL 15 { 16 /// <summary> 17 /// 账号密码是否正确 18 /// </summary> 19 /// <param name="userName">用户名</param> 20 /// <param name="pwd">密码</param> 21 /// <returns>是否正确</returns> 22 public bool isR(string userName,string pwd) 23 { 24 MySchoolDAL dal = new MySchoolDAL(); 25 //获取登录信息泛型集合 26 List<LoginInfo> loginInfo = dal.LoginInfo(); 27 //遍历登录信息泛型集合 判断输入的账号密码是否存在 28 foreach (var item in loginInfo) 29 { 30 if (item.LoginId.Equals(userName) && item.LoginPwd.Equals(pwd)) 31 { 32 return true; 33 } 34 } 35 return false; 36 } 37 38 /// <summary> 39 /// 获取学生信息 40 /// </summary> 41 /// <returns></returns> 42 public List<StudentInfo> GetList() 43 { 44 MySchoolDAL mySchoolDal = new MySchoolDAL(); 45 List<StudentInfo> studentInfo = new List<StudentInfo>(); 46 studentInfo = mySchoolDal.GetStudentInfo(); 47 48 return studentInfo; 49 } 50 51 /// <summary> 52 /// 按照年级获取新学生列表 53 /// </summary> 54 /// <param name="gradeId"></param> 55 /// <returns></returns> 56 public List<StudentInfo> GetNewList(string gradeName) 57 { 58 List<StudentInfo> studentInfo = GetList(); 59 60 List<StudentInfo> newStudentInfo = new List<StudentInfo>(); 61 for (int i = 0; i < studentInfo.Count; i++) 62 { 63 if (studentInfo[i].GradeName.Equals(gradeName)) 64 { 65 newStudentInfo.Add(studentInfo[i]); 66 } 67 } 68 return newStudentInfo; 69 } 70 71 72 /// <summary> 73 /// 获取年级信息 74 /// </summary> 75 /// <returns></returns> 76 public List<GradeInfo> GetGrade() 77 { 78 List<GradeInfo> gradeInfo = new List<GradeInfo>(); 79 MySchoolDAL md = new MySchoolDAL(); 80 gradeInfo = md.GetGradeInfo(); 81 return gradeInfo; 82 } 83 } 84 }
==========================================用户界面层==========================================
----------------------------------------登录界面----------------------------------------
1 using MySchool.BLL; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace SJ3 13 { 14 public partial class frmLogin : Form 15 { 16 public frmLogin() 17 { 18 InitializeComponent(); 19 } 20 21 //单击登录 22 private void btnLogin_Click(object sender, EventArgs e) 23 { 24 MySchoolBLL mb = new MySchoolBLL(); 25 //调用账号密码是否正确方法 26 bool isR = mb.isR(txtUserName.Text,txtPWD.Text); 27 if (isR == true) 28 { 29 MessageBox.Show("登录成功"); 30 frmList fl = new frmList(); 31 fl.ShowDialog(); 32 } 33 else 34 { 35 MessageBox.Show("登录失败,账号或者密码错误"); 36 } 37 } 38 39 //单击退出 40 private void btnCancel_Click(object sender, EventArgs e) 41 { 42 Application.Exit(); 43 } 44 } 45 }
----------------------------------------学生列表界面----------------------------------------
1 using MySchool.BLL; 2 using MySchool.Models; 3 using System; 4 using System.Collections.Generic; 5 using System.ComponentModel; 6 using System.Data; 7 using System.Drawing; 8 using System.Linq; 9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 namespace SJ3 14 { 15 public partial class frmList : Form 16 { 17 public frmList() 18 { 19 InitializeComponent(); 20 } 21 22 //加载 23 private void frmList_Load(object sender, EventArgs e) 24 { 25 BangCmb(); 26 } 27 28 /// <summary> 29 /// 绑定年级组合框 30 /// </summary> 31 public void BangCmb() 32 { 33 MySchoolBLL mb = new MySchoolBLL(); 34 List<GradeInfo> gradeInfo = mb.GetGrade(); 35 cmbGrade.DataSource = gradeInfo; 36 cmbGrade.DisplayMember = "GradeName"; 37 cmbGrade.ValueMember = "GradeId"; 38 } 39 40 /// <summary> 41 /// 按照年级绑定学生列表 42 /// </summary> 43 public void BangList() 44 { 45 MySchoolBLL mb = new MySchoolBLL(); 46 List<StudentInfo> studentInfo = mb.GetNewList(cmbGrade.Text); 47 dataGridView1.DataSource = studentInfo; 48 } 49 50 //更改组合框内容 51 private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e) 52 { 53 BangList(); 54 } 55 } 56 }