使用三层架构实现Student管理系统,分为Studrnt.Model层,Student.DAL层,Student.BLL层和Student.UI层
步骤分析:
1.在Student.Model添加StudentModel类,每一个类对应数据库表中的字段
2.在每一层添加引用,DAL层引用Model层,BLL层引用DAL和Model层,UI层引用BLL层和Model层
3.在Student.DAL层建一个StudentDAL类
在StudentDAL类中添加一个DataTable类型的方法,进行所有学生信息查询
public DataTable Select() { string str = "Data Source=.;Initial catalog=StudentDB;uid=sa"; using (SqlConnection con = new SqlConnection(str)) { string sql = "select * from Student"; SqlDataAdapter da = new SqlDataAdapter(sql,con); DataSet ds = new DataSet(); da.Fill(ds, "Student"); return ds.Tables["Student"]; } }
在BLL层添加StudentBLL类,在StudentBLL层中添加与StudentDAL层中方法重名的Select()类
调用StudentDAL的Select()方法,返回dal.Select()
public DataTable Select() { //调用StudentDAL层Select()方法 return dal.Select(); }
在Load事件中对StudentBLL类中的Select()方法进行调用,再将数据动态绑定到DataGridView(dgvList)控件上
private void FrmStudent_Load(object sender, EventArgs e) { //dgvList加载数据 DataTable table = bll.Select(); dgvList.DataSource = table; }
运行结果如下:
4.在StudentDAL类中添加一个DataTable类型的方法,进行根据学生姓名查询学生信息的方法
public DataTable SelectName(string name) { string str = "Data Source=.;Initial catalog=StudentDB;uid=sa"; //con释放资源 using (SqlConnection con = new SqlConnection(str)) { //模糊查询语句 string sql = "select * from Student where stu_name like'%"+name+"%'"; using (SqlDataAdapter da = new SqlDataAdapter(sql, con)) { DataSet ds = new DataSet(); da.Fill(ds, "StudentName"); return ds.Tables["StudentName"]; } } }
在StudentBLL层中添加与StudentDAL层中方法重名的SelectName()类
调用StudentDAL的SelectName()方法,返回dal.SelectName()
public DataTable SelectName(string name) { return dal.SelectName(name); }
在窗体中的查询按钮中写入代码,定义一个count接收txtSname中输入的值,调用BLL层中SelectName()方法进行模糊查询,将查询的数据动态绑定到dgvlist上
private void btnSelect_Click(object sender, EventArgs e) { string count = txtSname.Text; dgvList.DataSource = bll.SelectName(count); }
运行结果如下:
5.在StudentDAL类中添加一个Bool类型的AddStudent()类,默认返回False
public bool AddStudent(StudentModel model) { bool result = false; string str = "Data Source=.;Initial catalog=StudentDB;uid=sa"; using (SqlConnection con = new SqlConnection(str)) { string sql = "Insert into Student(stu_name,stu_age,stu_sex,stu_email) values(@name,@age,@gender,@email)"; SqlParameter[] para = { new SqlParameter("@name",model.Name), new SqlParameter("@age",model.Age), new SqlParameter("@gender",model.Gender), new SqlParameter("@email",model.Email) }; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddRange(para); try { con.Open(); int count = cmd.ExecuteNonQuery(); ) { result = true; } } catch (Exception) { throw; } finally { } } return result; }
在StudentBLL类中添加对StudentDAL类中对AddStudent方法的调用
public bool AddStudent(StudentModel model) { //调用StudentDAL层AddStudent()方法 return dal.AddStudent(model); }
在窗体中的录入数据按钮中写入代码
private void btnAdd_Click(object sender, EventArgs e) { //当姓名不为空时,进行添加 if(txtName.Text!="") { //给StudentModel中属性赋值 model.Name = txtName.Text; model.Age = Convert.ToInt32(txtAge.Text); model.Gender = cmbGender.SelectedItem.ToString(); model.Email = txtEmail.Text; bool result = bll.AddStudent(model); if (result) { MessageBox.Show("添加成功!"); //即时刷新 dgvList.DataSource = bll.Select(); } else { MessageBox.Show("添加失败!"); } } else { //否则提示 MessageBox.Show("姓名不能为空!"); } }
6.项目完成