ASP.NET MVC三层架构实例

开发 后端
本文以Northwind数据库的表Categories为例,简明的演示了一个简单的ASP.NET MVC三层架构的例子。

前几天收到CodeProject的电邮,asp.net mvc 的E文教程正在编写,一个老外蛮伟大的,免费贡献***章,也有100多页的内容。其中大量应用了LINQ技术(看来得花时间看看了,用统一的方式来面对所有数据源,确实还是蛮吸引人的)。当然,LINQ不是MVC必须的,你可以用很多技术实现,比如NHibernate,甚至原生的ADO.NET。

既然是实例,我直接上代码了,基础理论一搜一大把,但我还是觉得实践才是最重要的:

ASP.NET MVC三层架构实例:首先的数据访问层,Database类:

using System;     
using System.Data;     
using System.Configuration;     
using System.Linq;     
using System.Web;     
using System.Web.Security;     
using System.Web.UI;     
using System.Web.UI.HtmlControls;     
using System.Web.UI.WebControls;     
using System.Web.UI.WebControls.WebParts;     
using System.Xml.Linq;     
    
using System.Data.SqlClient;     
    
namespace northWind3Tier.DataAccessLayer     
{     
    /// < summary>     
    /// 用于数据访问     
    /// < /summary>     
    public class Database     
    {     
        /// < summary>     
        /// 数据库连接     
        /// < /summary>     
        protected SqlConnection conn;     
        /// < summary>     
        /// 数据库连接字符串     
        /// < /summary>     
        protected string connStr;     
    
        public Database()     
        {     
            this.connStr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;     
        }     
        /// < summary>     
        /// 关闭数据库连接     
        /// < /summary>     
         ~Database()//析构函数不带访问修饰符     
        {     
            try    
            {     
                if (conn != null)     
                {     
                    conn.Close();     
                }     
            }     
            catch { }     
        }     
        /// < summary>     
        /// 打开数据库连接     
        /// < /summary>     
        protected void Open()     
        {     
            if (conn == null)     
            {     
                conn = new SqlConnection(connStr);     
            }     
            if (conn.State.Equals(ConnectionState.Closed))     
            {     
                conn.Open();     
            }     
        }     
        /// < summary>     
        /// 关闭数据库连接     
        /// < /summary>     
        protected void Close()     
        {     
            if (conn != null)     
            {     
                conn.Close();     
            }     
        }     
        /// < summary>     
        /// 获取数据,返回一个dataset     
        /// < /summary>     
        /// < param name="sql">sql语句< /param>     
        /// < returns>< /returns>     
        public DataSet GetDataSet(string sql)     
        {     
            Open();     
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);     
            DataSet dataset = new DataSet();     
            adapter.Fill(dataset);     
            Close();     
            return dataset;               
        }     
    }     
}    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.

ASP.NET MVC三层架构实例:业务逻辑层 Category类:

using System;     
using System.Data;     
using System.Configuration;     
using System.Linq;     
using System.Web;     
using System.Web.Security;     
using System.Web.UI;     
using System.Web.UI.HtmlControls;     
using System.Web.UI.WebControls;     
using System.Web.UI.WebControls.WebParts;     
using System.Xml.Linq;     
    
using System.IO;     
using northWind3Tier.DataAccessLayer;     
namespace northWind3Tier.BusinessLayer     
{     
    public class Category     
    {     
        /// < summary>     
        /// 根据货物ID获取该货物的详细信息     
        /// < /summary>     
        /// < param name="categoryID">< /param>     
        public void LoadData(int categoryID)     
        {     
            Database db = new Database();     
            string sql = "select * from [Categories] where [CategoryID]="+categoryID;     
            DataSet ds = db.GetDataSet(sql);     
            //如果有查询到数据的话,填充属性     
            if (ds.Tables[0].Rows.Count > 0)     
            {     
                this.categoryID =(int) ds.Tables[0].Rows[0]["CategoryID"];     
                this.categoryName = ds.Tables[0].Rows[0]["CategoryName"].ToString();     
                this.description = ds.Tables[0].Rows[0]["Description"].ToString();     
                this.image =(byte[]) ds.Tables[0].Rows[0]["Picture"];                  
            }     
        }     
        /// < summary>     
        /// 字段和属性     
        /// < /summary>    
        #region     
        private int categoryID;     
        /// < summary>     
        /// 编号     
        /// < /summary>     
        public int CategoryID     
        {     
            get { return categoryID; }     
            set { categoryID = value; }     
        }     
        private string categoryName;     
        /// < summary>     
        /// 名称     
        /// < /summary>     
        public string CategoryName     
        {     
            get { return categoryName; }     
            set { categoryName = value; }     
        }     
        private string description;     
        /// < summary>     
        /// 说明     
        /// < /summary>     
        public string Description     
        {     
            get { return description; }     
            set { description = value; }     
        }     
        private byte[] image;     
        /// < summary>     
        /// 图像     
        /// < /summary>     
        public byte[] Image     
        {     
            get { return image; }     
            set { image = value; }     
        }    
        #endregion     
    
    
    
    
    }     
}    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.

ASP.NET MVC三层架构实例:***就是显示层,前台aspx代码:

< %@ Page Language="C#" AutoEventWireup="true" CodeBehind="CateqoryQuery.aspx.cs" Inherits="northWind3Tier.CateqoryQuery" %>     
    
< !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>Untitled Page< /title>     
< /head>     
< body>     
    < form id="form1" runat="server">     
    < div>     
         
        < asp:Label ID="Label1" runat="server" Text="货物编号(1-9):">< /asp:Label>     
        
        < asp:TextBox ID="TextBox1" runat="server">< /asp:TextBox>     
        
        < asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="查询"      
            Width="65px" />     
        < br />     
        < br />     
        < asp:Label ID="lblCategoryInfo" runat="server" Text="Label">< /asp:Label>     
        < br />     
        < asp:Image ID="Image1" runat="server" />     
         
    < /div>     
    < /form>     
< /body>     
< /html>    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

ASP.NET MVC三层架构实例:后台cs代码:

using System;     
using System.Collections;     
using System.Configuration;     
using System.Data;     
using System.Linq;     
using System.Web;     
using System.Web.Security;     
using System.Web.UI;     
using System.Web.UI.HtmlControls;     
using System.Web.UI.WebControls;     
using System.Web.UI.WebControls.WebParts;     
using System.Xml.Linq;     
using northWind3Tier.BusinessLayer;     
using System.IO;     
    
namespace northWind3Tier     
{     
    public partial class CateqoryQuery : System.Web.UI.Page     
    {     
        protected void Page_Load(object sender, EventArgs e)     
        {     
    
        }     
    
        protected void Button1_Click(object sender, EventArgs e)     
        {     
            int categoryID = -1;     
    
            if (TextBox1.Text != "")     
            {     
                try    
                {     
                    categoryID = Convert.ToInt32(TextBox1.Text);     
                    //if ((categoryID <  1) || (categoryID > 9))     
                            
                }     
                catch    
                {     
                    Response.Write("< mce:script type="text/javascript">< !--     
alert('只能输入1-9之间的数字')     
// -->< /mce:script>");     
                     return;     
                }     
            }     
            Category category = new Category();     
            category.LoadData(categoryID);     
    
            lblCategoryInfo.Text = "编号:" + category.CategoryID;     
            lblCategoryInfo.Text += "< BR>名称:" + category.CategoryName;     
            lblCategoryInfo.Text += "< BR>描述:" + category.Description;     
            byte[] image = category.Image;     
            //northwind数据库中的image字段(byte数组)的前面78是无用的,必须剔除才能正常显示图像     
            byte[] temp = new byte[image.Length - 78];     
            Array.Copy(image , 78, temp, 0, image.Length - 78);     
    
            string strPath = "photo/temp.JPG";     
            string strPhotoPath =strPath;     
            //保存图片文件     
            BinaryWriter bw = new BinaryWriter(File.Open(Server.MapPath (strPhotoPath), FileMode.OpenOrCreate));     
            bw.Write(temp);     
            bw.Close();     
            //显示图片     
            this.Image1.ImageUrl = strPath;     
        }     
    }     
}    
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

【编辑推荐】

  1. 利用TemplateField显示GridView中数据的元数据
  2. 使用Calendar控件显示HiredDate字段
  3. 利用TemplateField将姓和名显示在一列中
  4. GridView绑定数据的实现
  5. 通过e.Row实现GridViewRow访问单元格
责任编辑:book05 来源: csdn
相关推荐

2009-07-28 17:25:14

ASP.NET三层结构

2009-07-30 13:07:49

ASP.NET中的三层

2009-07-30 13:30:56

ASP.NET开发模式

2011-04-19 13:53:41

三层架构

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2013-01-09 11:00:20

架构开发三层架构.NET架构

2009-07-28 13:06:45

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-04-30 15:56:50

三层架构MVCMVP

2009-11-02 09:14:51

ASP.NET MVC

2012-02-07 10:40:13

MVCJava

2010-06-23 15:44:03

ASP.NET MVC

2009-07-22 10:13:31

异步ActionASP.NET MVC

2009-04-01 12:00:43

ASP.NETMVC

2009-07-22 09:11:02

Action方法ASP.NET MVC
点赞
收藏

51CTO技术栈公众号