自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET MVC三層架構(gòu)實(shí)例

開發(fā) 后端
本文以Northwind數(shù)據(jù)庫的表Categories為例,簡明的演示了一個簡單的ASP.NET MVC三層架構(gòu)的例子。

前幾天收到CodeProject的電郵,asp.net mvc 的E文教程正在編寫,一個老外蠻偉大的,免費(fèi)貢獻(xiàn)***章,也有100多頁的內(nèi)容。其中大量應(yīng)用了LINQ技術(shù)(看來得花時間看看了,用統(tǒng)一的方式來面對所有數(shù)據(jù)源,確實(shí)還是蠻吸引人的)。當(dāng)然,LINQ不是MVC必須的,你可以用很多技術(shù)實(shí)現(xiàn),比如NHibernate,甚至原生的ADO.NET。

既然是實(shí)例,我直接上代碼了,基礎(chǔ)理論一搜一大把,但我還是覺得實(shí)踐才是最重要的:

ASP.NET MVC三層架構(gòu)實(shí)例:首先的數(shù)據(jù)訪問層,Database類:

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

ASP.NET MVC三層架構(gòu)實(shí)例:業(yè)務(wù)邏輯層 Category類:

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

ASP.NET MVC三層架構(gòu)實(shí)例:***就是顯示層,前臺aspx代碼:

  1. < %@ Page Language="C#" AutoEventWireup="true" CodeBehind="CateqoryQuery.aspx.cs" Inherits="northWind3Tier.CateqoryQuery" %>     
  2.     
  3. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     
  4.     
  5. < html xmlns="http://www.w3.org/1999/xhtml" >     
  6. < head runat="server">     
  7.     < title>Untitled Page< /title>     
  8. < /head>     
  9. < body>     
  10.     < form id="form1" runat="server">     
  11.     < div>     
  12.          
  13.         < asp:Label ID="Label1" runat="server" Text="貨物編號(1-9):">< /asp:Label>     
  14.         
  15.         < asp:TextBox ID="TextBox1" runat="server">< /asp:TextBox>     
  16.         
  17.         < asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="查詢"      
  18.             Width="65px" />     
  19.         < br />     
  20.         < br />     
  21.         < asp:Label ID="lblCategoryInfo" runat="server" Text="Label">< /asp:Label>     
  22.         < br />     
  23.         < asp:Image ID="Image1" runat="server" />     
  24.          
  25.     < /div>     
  26.     < /form>     
  27. < /body>     
  28. < /html>    

ASP.NET MVC三層架構(gòu)實(shí)例:后臺cs代碼:

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

【編輯推薦】

  1. 利用TemplateField顯示GridView中數(shù)據(jù)的元數(shù)據(jù)
  2. 使用Calendar控件顯示HiredDate字段
  3. 利用TemplateField將姓和名顯示在一列中
  4. GridView綁定數(shù)據(jù)的實(shí)現(xiàn)
  5. 通過e.Row實(shí)現(xiàn)GridViewRow訪問單元格
責(zé)任編輯:book05 來源: csdn
相關(guān)推薦

2009-07-28 17:25:14

ASP.NET三層結(jié)構(gòu)

2009-07-30 13:07:49

ASP.NET中的三層

2009-07-30 13:30:56

ASP.NET開發(fā)模式

2011-04-19 13:53:41

三層架構(gòu)

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2013-01-09 11:00:20

架構(gòu)開發(fā)三層架構(gòu).NET架構(gòu)

2009-07-28 13:06:45

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-04-30 15:56:50

三層架構(gòu)MVCMVP

2009-11-02 09:14:51

ASP.NET MVC

2010-06-23 15:44:03

ASP.NET MVC

2012-02-07 10:40:13

MVCJava

2009-07-24 11:33:12

MVC單元測試ASP.NET

2009-07-22 10:34:37

ActionInvokASP.NET MVC

2010-03-19 09:17:16

ASP.NET MVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號