使用三层架构实现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.项目完成

Student管理系统的更多相关文章

  1. MVC丶 (未完待续······)

         希望你看了此小随 可以实现自己的MVC框架     也祝所有的程序员身体健康一切安好                                                     ...

  2. 学生信息管理系统总结——student数据库中表关系分析

    说到关系,那就不得不提两个东西: 1.E-R图,也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型 2.关系模 ...

  3. Student学生管理系统

    1.定义各个层 2.添加各个层之间的引用 DAL 层调用Model BLL层调用DAL和Model UI层调用BLL和Model层 Model层供各个层调用 3.根据数据库建立实体类,每张表对应一个实 ...

  4. 【IOS开发笔记02】学生管理系统

    端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...

  5. 数据结构(c语言)之学生信息管理系统

    程序思维导图 代码表示(代码参考:长春大学-牛言涛老师) 如有错误请指出欢迎交流 #include<stdio.h> #include<malloc.h>//动态存储分配函数头 ...

  6. C程序范例(2)——学生管理系统”链表“实现

    1.对于学生管理系统,能够实现的方法有许多,但是今天我们用链表的方法来实现.虽然初学者很可能看不懂,但是不要紧,这是要在整体的系统的学习完C语言之后,我才编写出的程序.所以大家不必要担心.在这里与大家 ...

  7. Python开发程序:学员管理系统(mysql)

    主题:学员管理系统 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节 ...

  8. Java项目:学生成绩管理系统(二)

    学生成绩管理系统(二):项目介绍 一.设计要求: 1.1 简单的图形界面登录功能. 1.2 对数据库的的信息的查询功能. 1.3 对数据库的的信息的修改功能. 1.4 对数据库的的信息的删除功能. 1 ...

  9. 基于数据库MySQL的简易学生信息管理系统

    通过这几天学习Mysql数据库,对其也有了基本的了解,为了加深印象,于是就写了一个最简易的学生信息管理系统. 一:基本要求 1.通过已知用户名和密码进行登录: 2.可以显示菜单: 3.可以随时插入学生 ...

随机推荐

  1. .net MVC 连接数据本地数据库三种方法

    <appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add ...

  2. 理解React中es6方法创建组件的this

    首发于:https://mingjiezhang.github.io/(转载请说明此出处). 在JavaScript中,this对象是运行时基于函数的执行环境(也就是上下文)绑定的. 从react中的 ...

  3. Objective-C instancetype关键字

     instancetype是clang 3.5开始,clang提供的一个关键字 表示某个方法返回的未知类型的Objective-C对象 instancetype会告诉编译器当前的类型,这点和NSObj ...

  4. 自定义JSP标签库及Properties使用

    自定义JSP标签库及Properties使用 自定义JSP标签 自定义JSP标签技术是在JSP 1.1版本中才出现的,它支持用户在JSP文件中自定义标签,这样可以使JSP代码更加简洁. 这些可重用的标 ...

  5. Android UI线程和非UI线程

    Android UI线程和非UI线程 UI线程及Android的单线程模型原则 当应用启动,系统会创建一个主线程(main thread). 这个主线程负责向UI组件分发事件(包括绘制事件),也是在这 ...

  6. 转 String,StringBuffer与StringBuilder的区别??

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  7. 【代码笔记】iOS-电影上的花絮,自动滚动

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  8. OC中的私有变量和description

    .OC中的私有变量 在类的实现即.m @implementation中也可以声明成员变量,但是因为在其他文件中通常都只 是包含头文件而不会包含实现文件,所以在.m文件中声明的成员变量是@private ...

  9. zend studio 9.0.4 破解、汉化和字体颜色及快捷键相关设置

    转载:http://www.penglig.com/post-45.html 下载:http://www.geekso.com/component/zendstudio-downloads/ 破解:h ...

  10. 详解 Spotlight on MySQL监控MySQL服务器

    前一章详解了Spotlight on Unix 监控Linux服务器 ,今天再来看看Spotlight on MySQL怎么监控MySQL服务器. 注:http://www.cnblogs.com/J ...