最簡單,最適合入門學習的三層架構例子

  1. 1.打開VS2008後,文件-->新建-->項目-->其他項目類型-->Visual Studio 解決方案-->空白解決方案 就起名爲:MvcTest 吧
  2. 2.建立如圖
  3.  附件: 您所在的用戶組無法下載或查看附件的項目,並在WEB-->App_Data建一個數據文件 DabaBase.mdf 裏面建表:qzzm_user 表內:字段Name,類型:nvarchar(50)
  4. 3.在WEB中引用BLL,Model層新建Post.aspx
  5. [運行代碼] [複製到剪貼板] [ ± ]
  6. CODE:
  7. <%@ Page Language=&amp;quot;C#&amp;quot; AutoEventWireup=&amp;quot;true&amp;quot; CodeFile=&amp;quot;Post.aspx.cs&amp;quot; Inherits=&amp;quot;Post&amp;quot; %>
  8. <!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;>
  9. <html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;>
  10. <head runat=&amp;quot;server&amp;quot;>
  11. <title>無標題頁</title>
  12. </head>
  13. <body>
  14. <form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;>
  15. <div>
  16. <asp:TextBox ID=&amp;quot;tb_name&amp;quot; runat=&amp;quot;server&amp;quot;></asp:TextBox>
  17. <asp:Button ID=&amp;quot;btn_post&amp;quot; runat=&amp;quot;server&amp;quot; οnclick=&amp;quot;btn_post_Click&amp;quot; Text=&amp;quot;提交&amp;quot; />
  18. </div>
  19. </form>
  20. </body>
  21. </html>
  22. Post.aspx.cs 先擱下等寫好類庫再寫
  23. 4.在Model 實體類中新建一個user.cs的類
  24. [運行代碼] [複製到剪貼板] [ ± ]
  25. CODE:
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. namespace Model
  31. {
  32. public class user
  33. {
  34. public user() { }
  35. private string _Name;
  36. public string Name
  37. {
  38. set { _Name = value; }
  39. get { return _Name; } 
  40. }
  41. }
  42. }
  43. 5.在DAL新建userdb.cs,並引用Model層
  44. [運行代碼] [複製到剪貼板] [ ± ]
  45. CODE:
  46. using System;
  47. using System.Collections.Generic;
  48. using System.Linq;
  49. using System.Text;
  50. using System.Data.SqlClient;
  51. using System.Configuration;
  52. namespace DAL
  53. {
  54. public class userdb
  55. {
  56. public bool adduser(Model.user model)
  57. {
  58. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&amp;quot;sqlconn&amp;quot;].ConnectionString);
  59. con.Open();
  60. using (SqlCommand cmd = new SqlCommand(&amp;quot;INSERT INTO qzzm_user(Name) VALUES(@Name)&amp;quot;, con))
  61. cmd.Parameters.AddWithValue(&amp;quot;@Name&amp;quot;, model.Name);
  62. if (cmd.ExecuteNonQuery() > 0)
  63. return true;
  64. else return false;
  65. }
  66. }
  67. }
  68. }
  69. 6.在BLL中新建userbll.cs並引用DAL,Model層
  70. [運行代碼] [複製到剪貼板] [ ± ]
  71. CODE:
  72. using System;
  73. using System.Collections.Generic;
  74. using System.Linq;
  75. using System.Text;
  76. namespace BLL
  77. {
  78. public class userbll
  79. {
  80. DAL.userdb db = new DAL.userdb();
  81. public bool adduser(Model.user model)
  82. {
  83. return db.adduser(model);
  84. }
  85. }
  86. }
  87. 7.可以開始寫Post.aspx.cs了
  88. [運行代碼] [複製到剪貼板] [ ± ]
  89. CODE:
  90. using System;
  91. using System.Collections;
  92. using System.Configuration;
  93. using System.Data;
  94. using System.Linq;
  95. using System.Web;
  96. using System.Web.Security;
  97. using System.Web.UI;
  98. using System.Web.UI.HtmlControls;
  99. using System.Web.UI.WebControls;
  100. using System.Web.UI.WebControls.WebParts;
  101. using System.Xml.Linq;
  102. public partial class Post : System.Web.UI.Page
  103. {
  104. protected void Page_Load(object sender, EventArgs e)
  105. {
  106. }
  107. protected void btn_post_Click(object sender, EventArgs e)
  108. {
  109. Model.user us = new Model.user();
  110. us.Name = tb_name.Text;
  111. BLL.userbll ub = new BLL.userbll();
  112. ub.adduser(us);
  113. }
  114. }
  115. 8.補充之前少寫的Web.config的數據鏈接字符串
  116. [運行代碼] [複製到剪貼板] [ ± ]
  117. CODE:
  118. <connectionStrings>
  119. <add name=&amp;quot;sqlconn&amp;quot; connectionString=&amp;quot;Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|/Database.mdf;Integrated Security=True;User Instance=True&amp;quot; providerName=&amp;quot;System.Data.SqlClient&amp;quot;/>
  120. </connectionStrings>
  121. 最後,說下各層的引用關係:
  122. 1) WEB引用 DAL,Model
  123. 2)BLL引用 DAL,Model
  124. 3)DAL引用Model
  125. 4)Model無引用
發佈了32 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章