关闭

我理解中的ASP三层

221人阅读 评论(0) 收藏 举报
  
ASP。NET三层实例解析(人事管理系统)
1. Model层的构建
Model层是一个规范层,在这里所做的事情就是将数据库中的每个表做成一个类,而表中的字段则做成类中的属性。在封装成的每个类中,对于每个属性均赋予两个简单的操作,即存和取,分别用get和set来实现。现给出一个例子
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
    public class person_info
    {
        private string m_PeopleId;//人员编号
        private string m_PeopleName;//人员姓名
        private string m_Sex;//性别
        private string m_Department;//部门
        private string m_Job;//职位
        private string m_Station;//岗位
        private string m_InOrNot;//是否在职
        private string m_SchoolAge;//学历
        private string m_HomeTown;//籍贯
 
 
 
        public string PeopleID
        {
            get
            {
                return m_PeopleId;
            }
            set
            {
                m_PeopleId = value;
 
            }
        }
        public string PeopleName
        {
            get
            {
                return m_PeopleName;
 
            }
            set
            {
                m_PeopleName = value;
 
            }
 
        }
        public string Sex
        {
            get
            {
                return m_Sex;
            }
            set
            {
                m_Sex = value;
 
            }
 
        }
        public string Department
        {
            get
            {
                return m_Department;
            }
            set
            {
                m_Department = value;
            }
        }
        public string Job
        {
            get
            {
                return m_Job;
            }
            set
            {
                m_Job = value;
            }
        }
        public string Station
        {
            get
            {
                return m_Station;
            }
            set
            {
                m_Station = value;
            }
        }
        public string InOrNot
        {
            get
            {
                return m_InOrNot;
            }
            set
            {
                m_InOrNot = value;
            }
        }
        public string SchoolAge
        {
            get
            {
                return m_SchoolAge;
            }
            set
            {
                m_SchoolAge = value;
            }
        }
        public string HomeTown
        {
            get
            {
                return m_HomeTown;
            }
            set
            {
                m_HomeTown = value;
            }
 
        }
    }
}
2. DAL层的构建
DAL层是数据操作层,在这里实现了与业务无关的基本数据操作。一般包括:增,删,改,存几个步骤,在这个例子里是直接用SQL语句进行操作。在完成这一切操作之前要先完成两个工作。1)编译成生Model文件,并将其引用添加到DAL层中。2)进行数据库的连接。数据库的连接,可以用读取web.config文件来实现。
Public String con_PeopleManager = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
然后才开始实现
然后,就实现SQL的增删改,在这里给出了一个增加功能的例子
public void InsertPersonInfo(Model.person_info person)
        {
            SqlConnection Sqlcon = new SqlConnection(con_PeopleManager);
            string insertString = "insert into person_info(PeopleId,PeopleName,Sex,Department,Job,Station,InOrNot,SchoolAge,HomeTown) " + "Values(@PeopleId,@PeopleName,@Sex,@Department,@Job,@Station,@InOrNot,@SchoolAge,@HomeTown) ";
            SqlCommand cmd_InsertMessage = new SqlCommand(insertString, Sqlcon);
            cmd_InsertMessage.Parameters.AddWithValue("@PeopleId", person.PeopleID);
            cmd_InsertMessage.Parameters.AddWithValue("@PeopleName", person.PeopleName);
            cmd_InsertMessage.Parameters.AddWithValue("@Sex", person.Sex);
            cmd_InsertMessage.Parameters.AddWithValue("@Department", person.Department);
            cmd_InsertMessage.Parameters.AddWithValue("@Job", person.Job);
            cmd_InsertMessage.Parameters.AddWithValue("@Station", person.Station);
            cmd_InsertMessage.Parameters.AddWithValue("@InOrNot", person.InOrNot);
            cmd_InsertMessage.Parameters.AddWithValue("@SchoolAge", person.SchoolAge);
            cmd_InsertMessage.Parameters.AddWithValue("@HomeTown", person.HomeTown);
            Sqlcon.Open();
            cmd_InsertMessage.ExecuteNonQuery();
            Sqlcon.Close();
        }
3.BLL层的创建:
BLL层是业务逻辑层,它负责对与业务相关的数据进行处理。BLL是双向的,它既负责从界面层接收控件值,并将其传入底层的数据库。也负责从DAL读取数据,显示到界面层的控件上去.下面给出了两个例子,分别诠释了这两种情况.
1:
  public void evaluation(string PeopleId, string PeopleName, string Department, string Topic, string Content, string Result, string EvalutePeople, string EvaluteTime, string Instruction, bool mark)
        {
            Model.evaluation evaluation = new Model.evaluation();
            evaluation.PeopleId = PeopleId;
            evaluation.PeopleName = PeopleName;
            evaluation.Department = Department;
            evaluation.Topic = Topic;
            evaluation.Content = Content;
            evaluation.Result = Result;
            evaluation.EvalutePeople = EvalutePeople;
            evaluation.EvaluteTime = EvaluteTime;
            evaluation.Instruction = Instruction;
            if (mark == true)
                (new DAL.DAL()).InsertEvaluation(evaluation);
            else
                (new DAL.DAL()).UpdateEvaluation(evaluation);
        }
2:
public Model.person_info personinfo_search()
        {
            Model.person_info person_info = new Model.person_info();
            SqlDataReader da_user = (new DAL.DAL()).GetPeopleInfo();
            while (da_user.Read())
            {
                person_info.PeopleID = da_user["PeopleID"].ToString().Trim();
                person_info.PeopleName = da_user["PeoPleName"].ToString().Trim();
                person_info.SchoolAge = da_user["SchoolAge"].ToString().Trim();
                person_info.Sex = da_user["Sex"].ToString().Trim();
                person_info.Station = da_user["Station"].ToString().Trim();
                person_info.HomeTown = da_user["HomeTown"].ToString().Trim();
                person_info.InOrNot = da_user["InOrNot"].ToString().Trim();
                person_info.Job = da_user["Job"].ToString().Trim();
                person_info.Department = da_user["Department"].ToString().Trim();
 
            }
 
            return person_info;
 
        }
至此,就完成了三层模的简单构建.
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1748次
    • 积分:42
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档