小試了一下asp.net中的三層結構體系

來源:互聯網
上載者:User
顯示層/邏輯層/資料層
顯示層通常由.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 Form設計器產生的程式碼override protected void OnInit(EventArgs e){//// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。//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);}#endregionprivate 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;}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.