码迷,mamicode.com
首页 > 其他好文 > 详细

Student管理系统

时间:2016-05-22 10:49:54      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

 

使用三层架构实现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();
                    if(count>0)
                    {
                        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.项目完成

 

Student管理系统

标签:

原文地址:http://www.cnblogs.com/S2223/p/5516206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!