Student

Posted Leizi-go

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Student相关的知识,希望对你有一定的参考价值。

package test2;

import java.io.Serializable;

public class Student implements Serializable

//private static final long serialVersionUID = 1L;

private String name;
private int age;

@Override
public String toString()
return "Student" +
"name=\'" + name + \'\\\'\' +
", age=" + age +
\'\';


public String getName()
return name;


public void setName(String name)
this.name = name;


public int getAge()
return age;


public void setAge(int age)
this.age = age;


public Student(String name, int age)
this.name = name;
this.age = age;

Student管理系统

 

使用三层架构实现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的主要内容,如果未能解决你的问题,请参考以下文章

student表中创建触发器,实现student表和student _course表的级联删除

为啥在 Student(Student s) 中可以使用实例变量? [复制]

Student管理系统

Models.Student'不包含'Score'的定义

将dict转换为数据框? [复制]

现在有两张表student和score,对每科的的成绩进行排名,取前三名学生的信息和排名