最简单,最适合入门学习的三层架构例子
来源:互联网 发布:全民枪战刷骑士软件 编辑:程序博客网 时间:2024/04/29 17:02
- 1.打开VS2008后,文件-->新建-->项目-->其他项目类型-->Visual Studio 解决方案-->空白解决方案 就起名为:MvcTest 吧
- 2.建立如图
- 附件: 您所在的用户组无法下载或查看附件的项目,并在WEB-->App_Data建一个数据文件 DabaBase.mdf 里面建表:qzzm_user 表内:字段Name,类型:nvarchar(50)
- 3.在WEB中引用BLL,Model层新建Post.aspx
- [运行代码] [复制到剪贴板] [ ± ]
- CODE:
- <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Post.aspx.cs&quot; Inherits=&quot;Post&quot; %>
- <!DOCTYPE html PUBLIC &quot;-
- <html xmlns=&quot;http:
- <head runat=&quot;server&quot;>
- <title>无标题页</title>
- </head>
- <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;提交&quot; />
- </div>
- </form>
- </body>
- </html>
- Post.aspx.cs 先搁下等写好类库再写
- 4.在Model 实体类中新建一个user.cs的类
- [运行代码] [复制到剪贴板] [ ± ]
- 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.在DAL新建userdb.cs,并引用Model层
- [运行代码] [复制到剪贴板] [ ± ]
- 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.在BLL中新建userbll.cs并引用DAL,Model层
- [运行代码] [复制到剪贴板] [ ± ]
- 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.可以开始写Post.aspx.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;
- 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.补充之前少写的Web.config的数据链接字符串
- [运行代码] [复制到剪贴板] [ ± ]
- 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>
- 最后,说下各层的引用关系:
- 1) WEB引用 DAL,Model
- 2)BLL引用 DAL,Model
- 3)DAL引用Model
- 4)Model无引用