ASP.NET-三层架构-2

在ASP.NET-三层架构-1的基础上继续完成三层项目的其他功能,如学生列表展示,添加学生信息,修改学生信息等功能。

1.在Model层添加学生表实体类(列表显示功能)

在model层添加“Student.cs”类文件,在文件中创建一个与Student数据表相对应的实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{//创建与Student数据表相对应的实体类
    public class Student
    {//int?表示可空的值类型
        public int ID { get; set; }//主键类
        public string StuNum { get; set; }
        public string StuName { get; set; }
        public string StuClass { get; set; }
        public string Stuject { get; set; }
        public int? StuAge { get; set; }
        public string StuPhone { get; set; }
        public string StuGender { get; set; }
    }
}

int?表示可空的值类型

2.在数据访问层查询数据(列表显示功能)

在DAL层中添加一个“StudentDal.cs”类文件,该类中封装所有对Student数据表操作的代码。该类中定义一个GetAllStudent()方法,用于查询Student表中的所有数据

using Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class StudentDal
    {
        public List<Student> GetAllStudent()
        {
            //查询数据的sql语句
            string sql = "select*from Student";
            //创建Student类型集合
            List<Student> studentList = new List<Student>();
            //调用ExecuteReader()方法查询数据
            using (SqlDataReader reader = SqlHelper.ExcuteReader(sql))
            {
                //判断是否获取数据
                if (reader.HasRows)
                {
                    //循环读取数据
                    while(reader.Read())
                    {
                        //创建对象用于存储数据
                        Student stu = new Student();
                        stu.ID = reader.GetInt32(0);
                        stu.StuNum = reader.GetString(1);
                        stu.StuName = reader.GetString(2);
                        stu.StuClass = reader.GetString(3);
                        stu.Stuject = reader.GetString(4);
                        //判断该列数据是否有值
                        stu.StuAge = Convert.IsDBNull(reader[5]) ? null : (int?)reader.GetInt32(5);
                        stu.StuPhone = reader.GetString(6);
                        stu.StuGender = reader.GetString(7);
                        //将对象添加到集合中
                        studentList.Add(stu);

                    
                    }
                }
            }
            //返回集合对象
            return studentList;

        }
    }
}

GetAllStudent()方法调用SqlHelper的ExecuteReader()方法查询数据。在查询数据时取出数据表中可为空的int类型列的数据赋给对象属性时,需要先通过Convert.IsDBNull()方法判断再赋值。

3.在业务逻辑层调用数据层(列表显示功能)

在BLL层中添加"StudentBll.cs"类文件,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;

namespace BLL
{
    public class StudentBll
    {
        private StudentDal dal = new StudentDal();
        //调用GetAllStudent()方法判断集合元素个数
        public List<Student> GetAllStudent()
        {
            return dal.GetAllStudent().Count > 0 ? dal.GetAllStudent() : null;
        }

    }
}

通过调用dal对象的GetAllStudent()获取数据对象集合,并将获取到的数据集合返回。

4.在表现层调用业务逻辑层(列表显示功能)

在UI层的StudentList.aspx.cs文件的Page_Load()事件中编写代码,该事件在页面加载时触发,触发时将所有数据加载到页面中

using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class StudentList : System.Web.UI.Page
    {
        private StudentBll bll = new StudentBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            //判断用户是否已登录
            if (Session["UserName"] == null)
            {
                //未登录则跳转到登录页面
                Response.Redirect("/Login.aspx");
            }
            else
            {
                //获取Student表中所有数据
                List<Student> studentList=bll.GetAllStudent();
                //创建用于拼接表格StringBuilder对象
                StringBuilder sb = new StringBuilder();
                int count = 1;//表格中的编号
                //拼接表头
                sb.Append("<div style='position:absolution; top:25%; left:15%; background-color:#F0F0F0;'>" +
                    "<table border='1px;solid'><tr><th>编号</th><th>学号</th><th>姓名</th><th>班级</th><th>学科</th><th>年龄</th><th>电话</th><th>性别</th><th>操作</th>");
                //循环遍历对象集合将值拼接到表格
                foreach(var item in studentList)
                {
                    sb.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td><a href='DeleteStudent.aspx?ID={8}'>删除</a>&nbsp;&nbsp;&nbsp;<a href='UpdateStudent.aspx?ID={8}'>修改</a></td></tr>", count++, item.StuNum, item.StuName, item.StuClass, item.Stuject, item.StuAge,item.StuPhone,item.StuGender,item.ID));
                }
                sb.Append("<tr><td colspan='9'><a href='AddStudent.aspx'>添加用户</a></td></tr></table>");
                //将表格字符串输出到页面
                Response.Write(sb.ToString());
            }
        }
    }
}

在Page_Load()事件中,首先获取登录时存入到Session对象中的用户名并进行判断,判断已登录后,通过调用bll的GetAllStudent()方法获取数据集合,遍历集合并将值拼接成表格输出到页面。

5.添加页面导航栏

在StudentList.aspx页面的body标签内添加页面导航栏功能的布局代码,具体包括显示登录的用户名,网站主页链接,修改密码链接以及用户退出链接

<body>
        <div class="header">
        <div class="title">您好:<%=Session["UserName"].ToString() %>,欢迎使用学生管理系统</div>
            <div class="h_nav"><ul class="nav">
                <li class="active"><a href="StudentList.aspx">网站主页</a></li>
                <li><a href="updatePassWord.aspx">修改密码</a></li>
                <li><a href="Logoff.ashx">注销退出</a></li>
                </ul>
                </div>
        </div> 
    <form id="form1" runat="server"><div></div>
    </form>
</body>

页面样式需添加CSS引用,“CSS/tableStyle.css”是表格样式,"CSS/NavigateCSS.css"是导航样式。

"<%=%>"用于读取Session中的用户名并展示在页面上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值