- 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;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>
- <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
- <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; οnclick=&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無引用
最簡單,最適合入門學習的三層架構例子
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.