关闭

asp.net中的三层结构体系显示层/逻辑层/数据层

807人阅读 评论(0) 收藏 举报

显示层通常由.aspx/.aspx.cs文件构成,用来显示数据页面,按三层体系的分的话,页面层应该是直接调用逻辑层中的方法
逻辑层是一个中间层,用来连接页面层和数据层,它要给页面层提供接口同时也要从数据层中调用方法供页面层使用
数据层是最底层了,一般作数据方面的操作
呵,暂时这样解释这三层体系吧,小试了一吧.
页面层[default.aspx]:只有一个DataGrid控件和一个Label控件
页面的最终执行结果

default.aspx.cs文件


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using LemongTree.BL;
namespace services
{
        /// <summary>
        /// _default 的摘要说明。
        /// </summary>
        public class _default : System.Web.UI.Page
        {
                protected System.Web.UI.WebControls.Label Label1;
                protected System.Web.UI.WebControls.DataGrid DataGrid1;
       
                private void Page_Load(object sender, System.EventArgs e)
                {
                        // 在此处放置用户代码以初始化页面
                        if(!Page.IsPostBack)
                        {
                        setup("select * from favor",DataGrid1);
                        }
                }
                private void setup(string sql,DataGrid dg)
                {
                BLayer bl = new BLayer("server=lemongtree;uid=sa;pwd=sa;database=bbs");
                        bl.showDataGrid(sql,dg);
                        Label1.Text=bl.error;
                }

                #region Web 窗体设计器生成的代码
                override protected void OnInit(EventArgs e)
                {
                        //
                        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                        //
                        InitializeComponent();
                        base.OnInit(e);
                }
               
                /// <summary>
                /// 设计器支持所需的方法 - 不要使用代码编辑器修改
                /// 此方法的内容。
                /// </summary>
                private void InitializeComponent()
                {   
                        this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
                        this.Load += new System.EventHandler(this.Page_Load);

                }
                #endregion

                private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
                {
               
                }
        }
}

逻辑层[BL.cs]


using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using LemongTree.DB;
using System.Web.UI.WebControls;
namespace LemongTree.BL
{
        /// <summary>
        /// BL 的摘要说明。
        /// </summary>
        public class BLayer
        {
                private string strconn;
                private SqlConnection objConn;
                private DBMaster dbm;
                private string ErrMsg;
                public BLayer(string strconn)
                {
                        //
                        // TODO: 在此处添加构造函数逻辑
                        //
                        this.strconn=strconn;
                        this.objConn=new SqlConnection(strconn);
                        dbm=new DBMaster(strconn);
                }
                public string error
                {
                        get
                        {
                        return ErrMsg;
                        }
                }
                public void showDataGrid(string strsql,DataGrid dgrd)
                {
                       
                        dgrd.DataSource=dbm.dataset(strsql);
                        ErrMsg=dbm.Error;
                        dgrd.DataBind();
                }
        }
}

数据层[DB.cs]


using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
namespace LemongTree.DB
{
        /// <summary>
        /// DB 的摘要说明。
        /// </summary>
        public class DBMaster
        {
                private string strconn;
                private string msg;
                private SqlConnection objConn;
                public DBMaster(string strConn)
                {
                        //
                        // TODO: 在此处添加构造函数逻辑
                        //
                        this.strconn=strConn;
                        this.objConn=new SqlConnection(strConn);
                }
                public string Error
                {
                        get
                        {
                        return msg;
                        }
                }
                public DataSet dataset(string strSql)
                {
                        DataSet dst = new DataSet();
                        try
                        {
                                dst = SqlHelper.ExecuteDataset(strconn,CommandType.Text,strSql);
                                msg="记录总数:<b>"+dst.Tables[0].Rows.Count.ToString()+"</b>";
                                return dst;
                        }
                        catch(SqlException e)
                        {
                                msg=e.Message;
                        }
                        finally
                        {
                                dst.Dispose();
                        }
                        return null;
                }
        }
}

0
0

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