一、数据库

/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2000                    */
/*==============================================================*/
 
 
if exists (select 1
            from sysobjects
           where id = object_id('newsContent')
            and   type = 'U')
   drop table newsContent
go
 
 
/*==============================================================*/
/* Table: newsContent                                            */
/*==============================================================*/
create table newsContent (
   ID           int              identity(1,1)   primary key,
   Title          nvarchar(50)     not null,
   Content       ntext            not null,
   AddDate      datetime         not null,
  CategoryID    int              not null
)
go

二、项目文件架构

实现步骤为:4-3-6-5-2-1

ID

项目

描述

用途

项目引用关系

实例所需文件

相关方法

1

Web

表现层

Web页和控件

引用BLL

WebUI.aspx

WebUI.aspx.cs

GetContent()

2

BLL

业务逻辑层

业务逻辑组件

引用 IDAL,Model,使用DALFactory创建实例

Content.cs

ContentInfo GetContentInfo(int id)

3

IDAL

数据访问层接口定义

每个DAL实现都要实现的一组接口

引用 Model

IContent.cs

ContentInfo GetContentInfo(int id)

4

Model

业务实体

传递各种数据的容器

无引用

ContentInfo.cs

5

DALFactory

数据层的抽象工厂

创建反射,用来确定加载哪一个数据库访问程序集的类

引用IDAL,通过读取web.config里设置的程序集,加载类的实例,返回给BLL使用。

Content.cs

IDAL.Icontent create()

6

SQLServerDAL

SQLServer数据访问层

Microsoft SQL Server特定的Pet Shop DAL实现,使用了IDAL接口

引用 Model和IDAL,被DALFactory加载的程序集,实现接口里的方法。

SqlHelper.cs

Content.cs

SqlDataReader ExecuteReader()

PrepareCommand()

ContentInfo GetContentInfo(int id)

OracleDAL

Oracle数据访问层

7

DBUtility

数据库访问组件基础类

GetSqlServerConnectionString得到数据库连接字符串,也可省去该项目,在SQLServerDAL.SqlHelper中用static readonly string SqlConnectionString代替。

无引用

实现步骤过程

1、创建Model,实现业务实体。

2、创建IDAL,实现接口。

3、创建SQLServerDAL,实现接口里的方法。

4、增加web.config里的配置信息,为SQLServerDAL的程序集。

5、创建DALFactory,返回程序集的指定类的实例。

6、创建BLL,调用DALFactory,得到程序集指定类的实例,完成数据操作方法。

7、创建WEB,调用BLL里的数据操作方法。

注意:

1、web.config里的程序集名称必须与SQLServerDAL里的输出程序集名称一致。

2、DALFactory里只需要一个DataAccess类,可以完成创建所有的程序集实例。

3、项目创建后,注意修改各项目的默认命名空间和程序集名称。

4、注意修改解决方案里的项目依赖。

5、注意在解决方案里增加各项目引用。

三、各层间的访问过程

1、传入值,将值进行类型转换(为整型)。

2、创建BLL层的content.cs对象c,通过对象c访问BLL层的方法GetContentInfo(ID)调用BLL层。

3、BLL层方法GetContentInfo(ID)中取得数据访问层SQLServerDAL的实例,实例化IDAL层的接口对象dal,这个对象是由工厂层DALFactory创建的,然后返回IDAL层传入值所查找的内容的方法dal.GetContentInfo(id)。

4、数据工厂通过web.config配置文件中给定的webdal字串访问SQLServerDAL层,返回一个完整的调用SQLServerDAL层的路径给 BLL层。

5、到此要调用SQLServerDAL层,SQLServerDAL层完成赋值Model层的对象值为空,给定一个参数,调用SQLServerDAL层的SqlHelper的ExecuteReader方法,读出每个字段的数据赋值给以定义为空的Model层的对象。

6、SqlHelper执行sql命令,返回一个指定连接的数据库记录集,在这里需要引用参数类型,提供为打开连接命令执行做好准备PrepareCommand。

7、返回Model层把查询得到的一行记录值赋值给SQLServerDAL层的引入的Model层的对象ci,然后把这个对象返回给BLL。

8、回到Web层的BLL层的方法调用,把得到的对象值赋值给Lable标签,在前台显示给界面

四、项目中的文件清单

1、DBUtility项目

(1)connectionInfo.cs

using System;
using System.Configuration;
 
namespace Utility
{
       /// <summary>
       /// ConnectionInfo 的摘要说明。
       /// </summary>
       public class ConnectionInfo
       {
              public static string GetSqlServerConnectionString()
              {
                     return ConfigurationSettings.AppSettings["SQLConnString"];
              }
       }
}

2、SQLServerDAL项目

(1)SqlHelper.cs抽象类

using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
 
namespace SQLServerDAL
{
       /// <summary>
       /// SqlHelper 的摘要说明。
       /// </summary>
       public abstract class SqlHelper
       {
              public static readonly string CONN_STR = ConnectionInfo.GetSqlServerConnectionString();
 
              /// <summary>
              /// 用提供的函数,执行SQL命令,返回一个从指定连接的数据库记录集
              /// </summary>
              /// <remarks>
              /// 例如:
              /// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
              /// </remarks>
              /// <param name="connectionString">SqlConnection有效的SQL连接字符串</param>
              /// <param name="commandType">CommandType:CommandType.Text、CommandType.StoredProcedure</param>
              /// <param name="commandText">SQL语句或存储过程</param>
              /// <param name="commandParameters">SqlParameter[]参数数组</param>
              /// <returns>SqlDataReader:执行结果的记录集</returns>
              public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
              {
                     SqlCommand cmd = new SqlCommand();
                     SqlConnection conn = new SqlConnection(connString);
 
                     // 我们在这里用 try/catch 是因为如果这个方法抛出异常,我们目的是关闭数据库连接,再抛出异常,
                     // 因为这时不会有DataReader存在,此后commandBehaviour.CloseConnection将不会工作。
                     try
                     {
                            PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                            cmd.Parameters.Clear();
                            return rdr;
                     }
                     catch
                     {
                            conn.Close();
                            throw;
                     }
              }
 
 
              /// <summary>
              /// 为执行命令做好准备:打开数据库连接,命令语句,设置命令类型(SQL语句或存储过程),函数语取。
              /// </summary>
              /// <param name="cmd">SqlCommand 组件</param>
              /// <param name="conn">SqlConnection 组件</param>
              /// <param name="trans">SqlTransaction 组件,可以为null</param>
              /// <param name="cmdType">语句类型:CommandType.Text、CommandType.StoredProcedure</param>
              /// <param name="cmdText">SQL语句,可以为存储过程</param>
              /// <param name="cmdParms">SQL参数数组</param>
              private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
              {
 
                     if (conn.State != ConnectionState.Open)
                            conn.Open();
 
                     cmd.Connection = conn;
                     cmd.CommandText = cmdText;
 
                     if (trans != null)
                            cmd.Transaction = trans;
 
                     cmd.CommandType = cmdType;
 
                     if (cmdParms != null)
                     {
                            foreach (SqlParameter parm in cmdParms)
                                   cmd.Parameters.Add(parm);
                     }
              }
       }
}

(2)Content.cs类

using System;
using System.Data;
using System.Data.SqlClient;
using Model;
using IDAL;
 
namespace SQLServerDAL
{
       /// <summary>
       /// Content 的摘要说明。
       /// </summary>
       public class Content:IContent 
       {
 
              private const string PARM_ID = "@ID";
              private const string SQL_SELECT_CONTENT = "Select ID, Title, Content, AddDate, CategoryID From newsContent Where ID = @ID";
 
 
              public ContentInfo GetContentInfo(int id)
              {
                     //创意文章内容类
                     ContentInfo ci = null;
 
                     //创建一个参数
                     SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.BigInt, 8);
                     //赋上ID值
                     parm.Value = id;
 
                     using(SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STR, CommandType.Text, SQL_SELECT_CONTENT, parm))
                     {
                            if(sdr.Read())
                            { 
                                   ci = new ContentInfo(sdr.GetInt32(0),sdr.GetString(1), sdr.GetString(2),
                                          sdr.GetDateTime(3), sdr.GetInt32(4), sdr.GetInt32(5), sdr.GetString(6));
                            }
                     }
                     return ci;
              }
       }
}

3、Model项目

(1)contentInfo.cs

using System;
 
namespace Model
{
       /// <summary>
       /// Class1 的摘要说明。
       /// </summary>
       public class ContentInfo
       {
              private int _ID;
              private string _Content;
              private string _Title;
              private string _From;
              private DateTime _AddDate;
              private int _clsID;
              private int _tmpID;
 
              /// <summary>
              /// 文章内容构造函数
              /// </summary>
              /// <param name="id">文章流水号ID</param>
              /// <param name="content">文章内容</param>
              /// <param name="title">文章标题</param>
              /// <param name="from">文章来源</param>
              /// <param name="clsid">文章的分类属性ID</param>
              /// <param name="tmpid">文章的模板属性ID</param>
              public ContentInfo(int id,string title,string content,string from,DateTime addDate,int clsid,int tmpid )
              {
                     this._ID = id;
                     this._Content = content;
                     this._Title = title;
                     this._From = from;
                     this._AddDate = addDate;
                     this._clsID = clsid;
                     this._tmpID = tmpid;
              }
 
 
              //属性
              public int ID
              {
                     get   { return _ID; }
              }
              public string Content
              {
                     get   { return _Content; }
              }
              public string Title
              {
                     get   { return _Title; }
              }
              public string From
              {
                     get   { return _From; }
              }
              public DateTime AddDate
              {
                     get   { return _AddDate; }
              }
              public int ClsID
              {
                     get   { return _clsID; }
              }
              public int TmpID
              {
                     get   { return _tmpID; }
              }
 
 
 
       }
}

4、IDAL项目

(1)Icontent.cs

using System;
using Model;
 
namespace IDAL
{
       /// <summary>
       /// 文章内容操作接口
       /// </summary>
       public interface IContent
       {
              /// <summary>
              /// 取得文章的内容。
              /// </summary>
              /// <param name="id">文章的ID</param>
              /// <returns></returns>
              ContentInfo GetContentInfo(int id);
       }
}

5、DALFactory项目

(1)Content.cs

using System;
using System.Reflection;
using System.Configuration;
using IDAL;
 
namespace DALFactory
{
       /// <summary>
       /// 工产模式实现文章接口。
       /// </summary>
       public class Content
       {
              public static IDAL.IContent Create()
              {
                     // 这里可以查看 DAL 接口类。
                     string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"].ToString();
                     string className = path+".Content";
                    
                     // 用配置文件指定的类组合
                     return (IDAL.IContent)Assembly.Load(path).CreateInstance(className);
              }
       }
}

6、BLL项目

(1)Content.cs

using System;
 
using Model;
using IDAL;
 
namespace BLL
{
       /// <summary>
       /// Content 的摘要说明。
       /// </summary>
       public class Content
       {
 
              public ContentInfo GetContentInfo(int id)
              {
 
                     // 取得从数据访问层取得一个文章内容实例
                     IContent dal = DALFactory.Content.Create();
 
                     // 用DAL查找文章内容
                     return dal.GetContentInfo(id);
              }
       }
}

7、Web项目

1、Web.config:

<appSettings>  

<add key="SQLConnString" value="Data Source=localhost;Persist Security info=True;Initial Catalog=newsDB;User ID=sa;Password= " />
    <add key="WebDAL" value="SQLServerDAL" />   
 </appSettings>

2、WebUI.aspx

<%@ Page language="c#" Codebehind="WebUI.aspx.cs" AutoEventWireup="false" Inherits="Web.WebUI" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
       <HEAD>
              <title>WebUI</title>
              <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
              <meta name="CODE_LANGUAGE" Content="C#">
              <meta name="vs_defaultClientScript" content="JavaScript">
              <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
       </HEAD>
       <body MS_POSITIONING="GridLayout">
              <form id="Form1" method="post" runat="server">
                     <FONT">宋体"></FONT>
                     <table width="600" border="1">
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;
                                          <asp:Label id="lblTitle" runat="server"></asp:Label></td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px; HEIGHT: 22px">&nbsp;</td>
                                   <td style="HEIGHT: 22px">&nbsp;
                                          <asp:Label id="lblDataTime" runat="server"></asp:Label></td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;
                                          <asp:Label id="lblContent" runat="server"></asp:Label></td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;</td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px; HEIGHT: 23px">&nbsp;</td>
                                   <td style="HEIGHT: 23px">&nbsp;</td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;</td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;</td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;</td>
                            </tr>
                            <tr>
                                   <td style="WIDTH: 173px">&nbsp;</td>
                                   <td>&nbsp;
                                          <asp:Label id="lblMsg" runat="server">Label</asp:Label></td>
                            </tr>
                     </table>
              </form>
       </body>
</HTML>

3、WebUI.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 BLL;
using Model;
 
namespace myWeb
{
       /// <summary>
       /// WebForm1 的摘要说明。
       /// </summary>
       public class WebUI : System.Web.UI.Page
       {
              protected System.Web.UI.WebControls.Label lblTitle;
              protected System.Web.UI.WebControls.Label lblDataTime;
              protected System.Web.UI.WebControls.Label lblContent;
              protected System.Web.UI.WebControls.Label lblMsg;
 
              private ContentInfo ci ;
 
 
              private void Page_Load(object sender, System.EventArgs e)
              {
                     if(!Page.IsPostBack)
                     {
                            GetContent("1");
                     }
              }
 
              private void GetContent(string id)
              {
                     int ID = WebComponents.CleanString.GetInt(id);
             
                     Content c = new Content();
                     ci = c.GetContentInfo(ID);
                     if(ci!=null)
                     {
                            this.lblTitle.Text = ci.Title;
                            this.lblDataTime.Text = ci.AddDate.ToString("yyyy-MM-dd");
                            this.lblContent.Text = ci.Content;
                     }
                     else
                     {
                            this.lblMsg.Text = "没有找到这篇文章";
                     }
              }
 
              #region Web 窗体设计器生成的代码
              override protected void OnInit(EventArgs e)
              {
                     //
                     // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                     //
                     InitializeComponent();
                     base.OnInit(e);
              }
             
              /// <summary>
              /// 设计器支持所需的方法 - 不要使用代码编辑器修改
              /// 此方法的内容。
              /// </summary>
              private void InitializeComponent()
              {   
                     this.Load += new System.EventHandler(this.Page_Load);
 
              }
              #endregion
       }
}

4、WebComponents项目

(1)CleanString.cs

using System;
using System.Text;
 
namespace myWeb.WebComponents
{
       /// <summary>
       /// CleanString 的摘要说明。
       /// </summary>
       public class CleanString
       {
 
              public static int GetInt(string inputString)
              {
                     try
                     {
                            return Convert.ToInt32(inputString);
                     }
                     catch
                     {
                            return 0;
                     }
 
              }
 
 
              public static string InputText(string inputString, int maxLength)
              {
                     StringBuilder retVal = new StringBuilder();
 
                     // check incoming parameters for null or blank string
                     if ((inputString != null) && (inputString != String.Empty))
                     {
                            inputString = inputString.Trim();
 
                            //chop the string incase the client-side max length
                            //fields are bypassed to prevent buffer over-runs
                            if (inputString.Length > maxLength)
                                   inputString = inputString.Substring(0, maxLength);
 
                            //convert some harmful symbols incase the regular
                            //expression validators are changed
                            for (int i = 0; i < inputString.Length; i++)
                            {
                                   switch (inputString[i])
                                   {
                                          case '"':
                                                 retVal.Append("&quot;");
                                                 break;
                                          case '<':
                                                 retVal.Append("&lt;");
                                                 break;
                                          case '>':
                                                 retVal.Append("&gt;");
                                                 break;
                                          default:
                                                 retVal.Append(inputString[i]);
                                                 break;
                                   }
                            }
 
                            // Replace single quotes with white space
                            retVal.Replace("'", " ");
                     }
 
                     return retVal.ToString();
                    
              }
               
       }
}

asp.net三层架构详解相关推荐

  1. JAVA三层架构详解

    JAVA三层架构详解 三层架构分别是什么 为什么要有三层架构 三层架构思想下的项目目录结构 三层架构分别是什么 通俗说三层架构指的就是: 前端所能看见的界面为第一层,专业术语表示层(UI层) 后端对相 ...

  2. java 三层架构_java三层架构详解

    三层架构模式介绍 三层架构模式: 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:界面层(User Interface layer).业务逻辑层(Bu ...

  3. JavaWeb三层架构详解

    什么是三层架构? 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:界面层(User Interface layer).业务逻辑层(Business L ...

  4. 程序基础之三层架构详解

    我们在编写程序时,最基本最常用的就是三层架构,那么关于三层架构,有些人肯定会有很多问题和不理解的地方,所以我们就根据问题来认识一下三层架构. 什么是三层架构 为什么要用三层架构 三层架构有什么优点缺点 ...

  5. C#_三层(BLL DAL Model)架构详解

    C#_三层架构详解 三层架构: 表现层(UI).业务逻辑层(BLL).数据访问层(DAL)再加上实体类库(Model) 表现层(UI):一般都是窗体的设计或者网页的设计,是可以一眼就可以看到的界面. ...

  6. asp.net三层架构连接Oracle 11g详解

    asp.net三层架构连接Oracle 11g 连接Oracle时使用微软的Oracle连接组件: 一 DAL层 using System; using System.Collections.Gene ...

  7. 高并发高流量网站架构详解

    (推荐)高并发高流量网站架构详解 Web2.0的兴起,掀起了互联网新一轮的网络创业大潮.以用户为导 向的新网站建设概念,细分了网站功能和用户群,不仅成功的造就了一大批新生的网站,也极大的方便了上网的人 ...

  8. java调用webservice_笃学私教:Java开发网站架构演变过程-从单体应用到微服务架构详解...

    原标题:笃学私教:Java开发网站架构演变过程-从单体应用到微服务架构详解 Java开发网站架构演变过程,到目前为止,大致分为5个阶段,分别为单体架构.集群架构.分布式架构.SOA架构和微服务架构.下 ...

  9. 大型分布式架构详解:架构模式+敏捷性+可扩展+案例等

    大型分布式架构详解:架构模式+敏捷性+可扩展+案例等 本篇是大型分布式网站架构的技术总结篇. 主要对大型分布式架构中涉及的架构模式.高性能.高可用.可伸缩.敏捷性.可扩展等技术点进行简要总结,对大型分 ...

最新文章

  1. Redis 高可用篇:你管这叫主从架构数据同步原理?
  2. 求助!妹子一个rm -rf把公司服务器数据删没了,我该怎么办?
  3. 「每周CV论文推荐」 初学深度学习活体与伪造人脸检测必读的文章
  4. 使用c语言标准库中的时间函数
  5. unity角色移动代码_教你3个步骤实现Unity小地图
  6. Android Gradle manifestPlaceholders 占位符详解
  7. OpenCASCADE:建模算法之将触感的形状连接起来
  8. 基于ZYNQ的机器视觉应用实例
  9. 图像色调,饱和度,对比度等相关定义
  10. adf4351使用_ADF:将UI类别与动态表单一起使用
  11. bug9-TypeError: ‘generator‘ object is not subscriptable
  12. matlab低通滤波器库函数代码_【转】Matlab中模拟低通滤波器的函数
  13. ros2 nav2 行为树插件引擎原理和应用
  14. UML用例图之关联关系、包含关系、扩展关系、泛化关系详解
  15. 手机照片删除了还能找回来吗?高手就是高手,精彩
  16. CDbCriteria CArrayDataProvider zii.widgets.grid (2)
  17. 百度飞桨AI抠图+图片合成
  18. 未来已经降临,只是先后有别
  19. 第二章 大教堂与市集
  20. mac和eshow投屏链接不成功问题

热门文章

  1. 【Groovy】闭包 Closure ( 闭包的 delegate 代理策略 | OWNER_FIRST | DELEGATE_FIRST | OWNER_ONLY | DELEGATE_ONLY )
  2. 【C 语言】Windows 下使用 gcc 编译器 ( 常用的编译器 | Qt 中的 gcc 编译器 | 独立安装 MinGW )
  3. 【Flutter】Dart 数据类型 Map 类型 ( 创建 Map 集合 | 初始化 Map 集合 | 遍历 Map 集合 )
  4. 【计算机网络】传输层 : TCP 协议 ( TCP 协议特点 | TCP 报文段首部格式 | TCP 报文段控制位 )
  5. 【Android 性能优化】应用启动优化 ( 安卓应用启动分析 | ActivityThread 主函数分析 | 应用初始化 | 启动优化项目 )
  6. 【Android RTMP】音频数据采集编码 ( FAAC 头文件与静态库拷贝到 AS | CMakeList.txt 配置 FAAC | AudioRecord 音频采样 PCM 格式 )
  7. [Android]动态加载/热部署框架汇总
  8. 把 Eclipse 中的工程 Push 到 Github(适用 Windows 平台)
  9. Step by Step WebMatrix网站开发之二:使用WebMatrix(2)
  10. 关于使浏览器崩溃的代码尝试