我学习的第一个ASP.NET三层架构

         以前一直听朋友说三层架构 ,自己从来不知道什么东西,今天学习了哈,练习了个小例,放在这里和大家交流.技术在交流中进步嘛!

所谓的三层就是把所要开发的网站或软件进行分层,尽量做到UI层和数据层,以及业务逻辑层分开,这样的做的好处是以后进行维护是方便简单.

UI层:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<b   <form id="form1" runat="server">
    <div>
    <center>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="223px"
            Width="760px" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <Columns>
                <asp:BoundField DataField="NewsRealID" HeaderText="新闻编号" />
                <asp:BoundField DataField="NewsTitle" HeaderText="新闻标题" />
                <asp:HyperLinkField DataTextField="NewsBody" HeaderText="新闻内容" />
                <asp:BoundField DataField="NewsCreateDate" HeaderText="发布日期" />
                <asp:BoundField DataField="NewsSiteName" HeaderText="来源网站" />
            </Columns>
            <RowStyle BackColor="White" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        </asp:GridView>
        </center>
       </div>
    </form>
</body>
</html>ody>
 

数据库层:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace My3WebDataComm
{
    public class DataComm
    {
       
        public DataSet GetData()
        {
           
                 SqlConnection   conn = new SqlConnection(My3WebDataConn.DataConn.DataConnString);
                 conn.Open();
                 SqlCommand cmd = new SqlCommand(My3WebDataConn.DataConn.SelectData, conn);
                 SqlDataAdapter   dap = new SqlDataAdapter(cmd);
                 DataSet  ds = new DataSet();
                 dap.Fill(ds, My3WebDataConn.DataConn.DataTable);
                 return ds;
                     
        }

 

       
    }
}

应用配置层:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;


namespace My3WebDataConn
{
    public class DataConn
    {
        public const string DataConnString = "server=SSITGHJ;database=qw_News;uid=OA;password=123456";

        public const string SelectData = "select*from News";

        public const string DataTable = "DataTable";
    }
}

  然后在UI层来进行调用:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using My3WebDataComm;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
      
    }

    public void BindData()
    {
    
        DataComm dc = new DataComm();
        this.GridView1.DataSource = dc.GetData();
        this.GridView1.DataBind();

   
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
    }
  
}

总结:这样做很方便,同时也把两个层进行了编译,可以保护代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值