[Original] The simplest, most suitable for getting started learning three-tier architecture example

Source: Internet
Author: User
Tags bool copy reference return string connectionstrings visual studio
1. After opening the VS2008, the file--> the new--> project--> other project types-->visual Studio Solutions--> Blank solution is named: Mvctest Bar

2. To establish a picture
Attachment: Your user group cannot download or view items for attachments and build a data file in Web-->app_data dababase.mdf: Qzzm_user table: Field name, type: nvarchar (50)

3. Referencing the Bll,model layer in the Web new post.aspx

[Run code] [Copy to Clipboard] [ ± ] CODE: <%@ Page language=&quot; c#&quot; autoeventwireup=&quot;true&quot; codefile=&quot; post.aspx.cs&quot; inherits=&quot; post&quot; %>
<! DOCTYPE HTML Public &AMP;QUOT;-//W3C//DTD XHTML 1.0 transitional//en&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>
<title> Untitled Page </title>
<body>
<form id=&quot;form1&quot; Runat=&quot;server&quot;>
<div>

<asp:textbox id=&quot;tb_name&quot; Runat=&quot;server&quot;></asp:textbox>

<asp:button id=&quot;btn_post&quot; runat=&quot;server&quot; onclick=&quot;btn_post_click&quot; text=&quot; Submit &quot; />

</div>
</form>
</body>

Post.aspx.cs the class library before writing.

4. Create a new User.cs class in the Model entity class

[Run code] [Copy to Clipboard] [ ± ] Code:using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Model
{
public class User
{
Public user () {}
private string _name;
public string Name
{
set {_name = value;}
get {return _name;}
}
}
}

5. New Userdb.cs at DAL and Reference Model layer

[Run code] [Copy to Clipboard] [ ± ] Code:using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Data.SqlClient;
Using System.Configuration;
Namespace DAL
{
public class UserDB
{
public bool AddUser (Model.user Model)
{
SqlConnection con = new SqlConnection (Configurationmanager.connectionstrings[&quot;sqlconn&quot;]. ConnectionString);
Con. Open ();
using (SqlCommand cmd = new SqlCommand (&quot;insert into Qzzm_user (Name) VALUES (@Name) &quot;, con)
{
Cmd. Parameters.addwithvalue (&quot; @Name &quot, model. Name);
if (cmd. ExecuteNonQuery () > 0)
return true;
else return false;
}
}
}
}

6. Create a new Userbll.cs in BLL and reference the Dal,model layer

[Run code] [Copy to Clipboard] [ ± ] Code:using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace BLL
{
public class USERBLL
{
Dal.userdb db = new Dal.userdb ();
public bool AddUser (Model.user Model)
{
return Db.adduser (model);
}
}
}

7. You can start writing Post.aspx.cs.

[Run code] [Copy to Clipboard] [ ± ] 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;
public partial class Post:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Btn_post_click (object sender, EventArgs e)
{
Model.user US = new Model.user ();
us. Name = Tb_name. Text;
BLL.USERBLL UB = new BLL.USERBLL ();
Ub.adduser (US);
}
}

8. Add the Web.config data link string that was previously less written

[Run code] [Copy to Clipboard] [ ± ] CODE: <connectionStrings>
<add name=&quot;sqlconn&quot; Connectionstring=&quot;data source=.\sqlexpress; attachdbfilename=| Datadirectory|\database.mdf;integrated security=true; User instance=true&quot; providername=&quot; System.data.sqlclient&quot;/>
</connectionStrings>

Finally, the reference relation of each layer:
1) Web Reference Dal,model
2) BLL Reference Dal,model
3) DAL Reference Model
4 model without reference


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.