Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MVC three-tier Architecture

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

MVC three-tier architecture example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I received an email from CodeProject a few days ago. Asp.net mvc's E-language course is being written. A great foreigner, a free contribution chapter, also has more than 100 pages of content. A lot of LINQ technology is used (it looks like it takes time to look at it, and it's really attractive to face all data sources in a unified way). Of course, LINQ is not necessary for MVC, and you can implement it in many technologies, such as NHibernate or even native ADO.NET.

Since it is an example, I went straight to the code and searched a lot of basic theories, but I still think practice is the most important thing:

ASP.NET MVC three-tier architecture example: the first data access layer, the Database class:

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>

/ / for data access /

< /summary>

Public class Database {/

< summary>

/ / Database connection / /

< /summary>

Protected SqlConnection conn; /

< summary>

/ / Database connection string /

< /summary>

Protected string connStr; public Database () {this.connStr = ConfigurationManager.ConnectionStrings ["DBConnectionString"] .ConnectionString;} / /

< summary>

/ / close database connection / /

< /summary>

~ Database () / destructor without access modifier {try {if (conn! = null) {conn.Close () }} catch {}} /

< summary>

/ / Open database connection / /

< /summary>

Protected void Open () {if (conn = = null) {conn = new SqlConnection (connStr);} if (conn.State.Equals (ConnectionState.Closed)) {conn.Open () }} /

< summary>

/ / close database connection / /

< /summary>

Protected void Close () {if (conn! = null) {conn.Close ();}} / /

< summary>

/ get the data and return a dataset /

< /summary>

/ / /

< param name="sql">

Sql statement

< /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 three-tier architecture example: business logic layer Category class:

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>

/ / obtain the details of the goods according to the goods 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 any data is found, fill in the attribute 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>

/ / Fields and properties /

< /summary>

# region private int categoryID; /

< summary>

/ / number /

< /summary>

Public int CategoryID {get {return categoryID;} set {categoryID = value;}} private string categoryName; /

< summary>

/ / name / /

< /summary>

Public string CategoryName {get {return categoryName;} set {categoryName = value;}} private string description; /

< summary>

/ / description / /

< /summary>

Public string Description {get {return description;} set {description = value;}} private byte [] image; /

< summary>

/ / Image / /

< /summary>

Public byte [] Image {get {return image;} set {image = value;}} # endregion}}

Example of ASP.NET MVC three-tier architecture: * is the display layer, and the foreground aspx code:

< %@ 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>

Example of ASP.NET MVC three-tier architecture: background cs code:

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 =" number: "+ category.CategoryID; lblCategoryInfo.Text + ="

< BR>

Name: "+ category.CategoryName; lblCategoryInfo.Text + ="

< BR>

Description: "the first 78 of the image field (byte array) in the + category.Description; byte [] image = category.Image; / / northwind database is useless and must be removed to display the image byte [] temp = new byte [image.Length-78]; Array.Copy (image, 78, temp, 0, image.Length-78). String strPath = "photo/temp.JPG"; string strPhotoPath = strPath; / / Save the picture file BinaryWriter bw = new BinaryWriter (File.Open (Server.MapPath (strPhotoPath), FileMode.OpenOrCreate); bw.Write (temp); bw.Close () / / display picture this.Image1.ImageUrl = strPath;} the answers to the sample analysis questions about the three-tier MVC architecture are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report