• 学生管理系统-----分层


    首先建立三层:DAL(窗体)、BLL(类库)、UI(类库)和实体类Model

    他们的关系是:UI引用BLL和Model,BLL引用DAL和Model,DAL引用Model,Model谁都不引用

          

    登录功能:

       DAL层:(为了防止sql注入需要使用SqlParameter)

           //登录
           public bool LoginStudent(Student stu) 
           {
               bool flag = false;
               string sql = "select count(1) from Student where StudentName=@name and LoginPwd=@pwd";
               SqlParameter[] para = 
               {
               new SqlParameter("@name",stu.StudentName),
               new SqlParameter("@pwd",stu.LoginPwd)
               };
               int count=Convert.ToInt32(SQLHelper.ExecuteScalar(sql,para));
               if (count > 0)
               {
                   flag = true;
               }
               return flag;
           }

       BLL层:(只做数据传递的载体)

     StudentDAL dal = new StudentDAL();
           //登录
           public bool IsLogin(Student stu) 
           {
               bool result = dal.LoginStudent(stu);
               return result;
           }

       UI层:

     private void btnLogin_Click(object sender, EventArgs e)
            {
                StudentBLL bll = new StudentBLL();
                Student stu = new Student();
                stu.StudentName = txtName.Text;
                stu.LoginPwd = txtPwd.Text;
                bool result=bll.IsLogin(stu);
                if (result)
                {
                    MessageBox.Show("登录成功!");
                    FrmMain frm = new FrmMain();
                    this.Hide();
                    frm.Show();
                }
                else
                {
                    MessageBox.Show("哎哟!失败啦!");
                }
            }

    效果:

                 

    登录成功后进入主界面

    添加学生:

      DAL层:

     //添加学生
           public bool AddStudent(Student stu)
           {
               bool flag = false;
               string sql = "insert into Student values('"+stu.StudentNo+"','"+stu.LoginPwd+"','"+stu.StudentName+"','"+stu.Sex+"','"+stu.GradeId+"','"+stu.Phone+"','"+stu.Address+"','"+stu.BornDate+"','"+stu.Email+"')";
               int count = SQLHelper.ExecuteNonQuery(sql);
               if (count > 0) 
               {
                   flag = true;
               }
               return flag;
           }

      BLL层:

          //添加
           public bool Add(Student stu) 
           {
               bool result = dal.AddStudent(stu);
               return result;
           }

      UI层:

     private void btnEdit_Click(object sender, EventArgs e)
            {
                stu.StudentNo = txtStudentNo.Text;
                stu.LoginPwd = txtPwd.Text;
                stu.StudentName = txtName.Text;
                stu.Sex = rbtnFemale.Checked ? "0" : "1";
                stu.GradeId = Convert.ToInt32(cboGrade.SelectedValue);
                stu.Phone = txtPhone.Text;
                stu.Address = txtAddress.Text;
                stu.BornDate = dpBirthday.Value;
                stu.Email = txtEmail.Text;      
    
                if (txtPwd.Text.Trim() != txtRePwd.Text.Trim())
                {
                    MessageBox.Show("密码请输入一致!");
                    return;
                }
                
                    bool re = bll.Add(stu);
                    if (re)
                    {
                        MessageBox.Show("添加成功!");
                    }
                    else
                    {
                        MessageBox.Show("添加失败!");
                    }          
             }

      效果:

    删除学生:

      DAL层:

     //删除学生
           public bool DeleteStudent(string StuNo) 
           {
               bool flag = false;
               string sql = "delete from Student where StudentNo=@stuno";
               SqlParameter para = new SqlParameter("stuno",StuNo);
               int count = SQLHelper.ExecuteNonQuery(sql,para);
               if (count > 0) 
               {
                   flag = true;
               }
               return flag;
           }

      BLL层:

     //删除
           public bool DeleteStudent(string StuNo) 
           {
               return dal.DeleteStudent(StuNo);
           }

      UI层:

     private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
                string stuno = dgvListName.SelectedRows[0].Cells["studentno"].Value.ToString();
    
                //获取当前选中行的索引值
                int index = dgvListName.CurrentRow.Index;
    
                bool result = bll.DeleteStudent(stuno);
                if (result)
                {
                    MessageBox.Show("删除成功!");
                    List<Student> list = bll.Gets();
                    dgvListName.DataSource = list;
                   
                    //光标停留
                    dgvListName[0, index].Selected = true;
                    dgvListName.CurrentCell = dgvListName[0, index];
                }
            }

    效果:

    修改:

      DAL层:

     //修改
           public bool UpdateStudent(Student stu) 
           {
               bool flag = false;
               string sql = "update student set studentno=@studentno,studentname=@studentname,sex=@sex,gradeid=@gradeid where studentno=@studentno";
               SqlParameter[] paras = 
               {
               new SqlParameter("@studentname",stu.StudentName),
               new SqlParameter("@studentno",stu.StudentNo),
               new SqlParameter("@sex",stu.Sex),
               new SqlParameter("@gradeid",stu.GradeId)
               };
               int count = SQLHelper.ExecuteNonQuery(sql,paras);
               if (count > 0) 
               {
                   flag = true;
               }
               return flag;
           }

      BLL层:

    //修改
           public bool UpdateStudent(Student stu)
           {
               return dal.UpdateStudent(stu);
           }

      UI层:

    修改需要新建一个窗体

            StudentBLL bll = new StudentBLL();
            Student stu = new Student();
            public DataGridView dgvListName = null;
            private void UpdateStudent_Load(object sender, EventArgs e)
            {
                //绑定值
                stu.StudentNo = dgvListName.SelectedRows[0].Cells["studentno"].Value.ToString();
                stu.StudentName = dgvListName.SelectedRows[0].Cells["studentname"].Value.ToString();
                stu.Sex = dgvListName.SelectedRows[0].Cells["sex"].Value.ToString();
                stu.GradeId = Convert.ToInt32(dgvListName.SelectedRows[0].Cells["gradeid"].Value);
                this.txtNo.Text = stu.StudentNo;
                this.txtName.Text = stu.StudentName;
                this.txtSex.Text = stu.Sex;
                this.txtGrade.Text = stu.GradeId.ToString();
            }
    
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                int index = dgvListName.CurrentRow.Index;
                stu.StudentName = txtName.Text;
                stu.Sex = txtSex.Text;
                stu.StudentNo = txtNo.Text;
                bool flag = bll.UpdateStudent(stu);
                if (flag) 
                {
                    MessageBox.Show("修改成功!");
                    List<Student> list = bll.Gets();
                    dgvListName.DataSource = list;
    
                    dgvListName[0, index].Selected = true;
                    dgvListName.CurrentCell = dgvListName[0, index];
                }
            }
         private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
            {         
                UpdateStudent up = new UpdateStudent();
                up.dgvListName = dgvListName;
                up.Show();
            }

    效果:

  • 相关阅读:
    L1-046. 整除光棍
    L2-014. 列车调度
    L2-009. 抢红包
    L2-005. 集合相似度
    L2-021. 点赞狂魔
    L1-033. 出生年
    设计模式之生成器模式
    设计模式之抽象工厂模式
    设计模式之工厂方法模式
    设计模式之简单工厂模式
  • 原文地址:https://www.cnblogs.com/hr1997/p/5516228.html
Copyright © 2020-2023  润新知