您好,登錄后才能下訂單哦!
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; } } }
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 } }
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>
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; } } }
關于MVC三層架構的示例分析問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。