asp三层架构.docx

上传人:b****6 文档编号:15665755 上传时间:2023-07-06 格式:DOCX 页数:22 大小:23.46KB
下载 相关 举报
asp三层架构.docx_第1页
第1页 / 共22页
asp三层架构.docx_第2页
第2页 / 共22页
asp三层架构.docx_第3页
第3页 / 共22页
asp三层架构.docx_第4页
第4页 / 共22页
asp三层架构.docx_第5页
第5页 / 共22页
asp三层架构.docx_第6页
第6页 / 共22页
asp三层架构.docx_第7页
第7页 / 共22页
asp三层架构.docx_第8页
第8页 / 共22页
asp三层架构.docx_第9页
第9页 / 共22页
asp三层架构.docx_第10页
第10页 / 共22页
asp三层架构.docx_第11页
第11页 / 共22页
asp三层架构.docx_第12页
第12页 / 共22页
asp三层架构.docx_第13页
第13页 / 共22页
asp三层架构.docx_第14页
第14页 / 共22页
asp三层架构.docx_第15页
第15页 / 共22页
asp三层架构.docx_第16页
第16页 / 共22页
asp三层架构.docx_第17页
第17页 / 共22页
asp三层架构.docx_第18页
第18页 / 共22页
asp三层架构.docx_第19页
第19页 / 共22页
asp三层架构.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

asp三层架构.docx

《asp三层架构.docx》由会员分享,可在线阅读,更多相关《asp三层架构.docx(22页珍藏版)》请在冰点文库上搜索。

asp三层架构.docx

asp三层架构

实例讲解三层架构的应用以及搭建

一、数据库/*==============================================================*//*DBMSname:

MicrosoftSQLServer2000*//*==============================================================*/ifexists(select1fromsysobjectswhereid=object_id

一、数据库

/*==============================================================*/

/* 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

 

I

 项目

 描述

 用途

 项目引用关系

 实例所需文件

 相关方法

 

1

 Web

 表现层

 Web页和控件

 引用BLL

 WebUI.aspxWebUI.aspx.cs

  

GetContent()

 

2

 BLL

 业务逻辑层

 业务逻辑组件

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

 Content.cs

 ContentInfoGetContentInfo(intid)

 

3

 IDAL

 数据访问层接口定义

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

 引用Model

 IContent.cs

 ContentInfoGetContentInfo(intid)

 

4

 Model

 业务实体

 传递各种数据的容器

 无引用

 ContentInfo.cs

 

 

 

5

 DALFactory

 数据层的抽象工厂

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

 引用IDAL,

 Content.cs

 IDAL.Icontentcreate()

 

6

 SQLServerDAL

 SQLServer数据访问层

 MicrosoftSQLServer特定的PetShopDAL实现,使用了IDAL接口

 引用ModelIDAL,DALFactory加载的程序集,实现接口里的方法。

 SqlHelper.cs

 

 

 Content.cs

SqlDataReaderExecuteReader()PrepareCommand()

ContentInfoGetContentInfo(intid)

 OracleDAL

 Oracle数据访问层

 

7

 DBUtility

 数据库访问组件基础类

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

 无引用

 

  

 

实现步骤过程

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类,

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

{

       /// 

       /// ConnectionInfo 的摘要说明。

       /// 

       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

{

       /// 

       /// SqlHelper 的摘要说明。

       /// 

       public abstract class SqlHelper

       {

              public static readonly string CONN_STR = ConnectionInfo.GetSqlServerConnectionString();

 

              /// 

              /// 用提供的函数,执行SQL命令,返回一个从指定连接的数据库记录集

              /// 

              /// 

              /// 例如:

              /// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

              /// 

              /// SqlConnection有效的SQL连接字符串

              /// CommandType:

CommandType.Text、CommandType.StoredProcedure

              /// SQL语句或存储过程

              /// SqlParameter[]参数数组

              /// SqlDataReader:

执行结果的记录集

              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;

                     }

              }

 

 

              /// 

///为执行命令做好准备:

打开数据库连接,命令语句,设置命令类型(SQL语句或存储过程),函数语取。

////summary///paramname=cmdSqlCommand组件/param///p

              /// 为执行命令做好准备:

打开数据库连接,命令语句,设置命令类型(SQL语句或存储过程),函数语取。

              /// 

              /// SqlCommand 组件

              /// SqlConnection 组件

              /// SqlTransaction 组件,可以为null

              /// 语句类型:

CommandType.Text、CommandType.StoredProcedure

              /// SQL语句,可以为存储过程

              /// SQL参数数组

              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

{

       /// 

       /// Content 的摘要说明。

       /// 

       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

{

       /// 

       /// Class1 的摘要说明。

       /// 

       public class ContentInfo

       {

              private int _ID;

              private string _Content;

privatestring_Title;privatestring_From;privateDateTime_AddDate;privateint_clsID;privateint_tmpID;///summary///文章内容构造函数////summary///paramname=id文章流水号ID

private string _Title;

              private string _From;

              private DateTime _AddDate;

              private int _clsID;

              private int _tmpID;

 

              /// 

              /// 文章内容构造函数

              /// 

              /// 文章流水号ID

              /// 文章内容

              /// 文章标题

              /// 文章来源

              /// 文章的分类属性ID

              /// 文章的模板属性ID

              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._

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2