I hope you will give me some advice on some of my experiences.
For a layer-3 architecture, you must first understand its mechanism. For details, refer to my blog post. There must be a foundation, and the more important thing is the class. It is best to study JAVA first.
1. Open VS2008 and create an empty solution as follows:
1. File → new → Project
2. Expand other project types → Visual Studio solution → blank solution
3. Start a name and confirm (for example)
4. In Solution Explorer, right-click solution> Add> new project> class library, and enter Model
5. Create a New BLL and DAL class libraries in the same way.
6. In Solution Explorer, right-click solution> Add> new project (or website)> ASP. NET web application
PS: click "New Website" for a small site. For a larger site, use a web application.
Creating a website and creating a web application have different effects after compilation and release:
After a new website is compiled and released, a randomly named. dll file will be generated for each aspx page under the bin directory. By default, the added cs files will be managed in the App_Code directory;
Create a new web application compilation Conference. In the bin directory, only the dll file of the current project is generated.
The file directory has been created, for example.
2. database establishment and Connection
1. Right-click the APP_DATA folder on the website to create a Database file Database. mdf. Then add a table user with the name Type of a field in the table nvarchar (50 ).
2. Configure the web. config file:
<ConnectionStrings>
<Add name = "ConnectionString" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ Database. mdf; Integrated Security = True; User Instance = True"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>
3. Add the user. cs file and the following code to the Model:
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5
6 namespace Model
7 {
8 public class user
9 {
10 public user (){}
11 private string _ name;
12 public string name
13 {
14 set {_ name = value ;}
15 get {return _ name ;}
16}
17
18}
19}
4. Add reference to Model and system. configuration in DAL, and then add userDB. cs and the following code:
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using System. Data;
6 using System. Data. SqlClient;
7 using Model;
8 using System. Configuration;
9 namespace DAL
10 {
11 public class userDB
12 {
13 public bool addUser (Model. user model)
14 {
15 string setting = ConfigurationManager. ConnectionStrings ["ConnectionString"]. ToString ();
16 SqlConnection myconn = new SqlConnection (setting );
17 myconn. Open ();
18 SqlCommand cmd = new SqlCommand ("insert into dbo. [user] ([name]) values (@ name)", myconn );
19 cmd. Parameters. AddWithValue ("@ name", model. name );
20 if (cmd. ExecuteNonQuery ()> 0)
21 {
22 return true;
23}
24 else
25 {
26 return false;
27}
28}
29
30}
31}
5. Add a reference to MODEL and DAL in BLL, create a userBLL. cs file, and add the following code:
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using DAL;
6
7 namespace BLL
8 {
9 public class userBLL
10 {
11 DAL. userDB db = new userDB ();
12 public bool addUser (Model. user model)
13 {
14 return db. addUser (model );
15}
16}
17}
6. Add a textbox Control in default. aspx on the website with the ID TB_username. Then add a button control and double-click the button. Enter the following code on the CS page:
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Web;
5 using System. Web. UI;
6 using System. Web. UI. WebControls;
7 using Model;
8 using BLL;
9 namespace WEB
10 {
11 public partial class _ Default: System. Web. UI. Page
12 {
13 protected void Page_Load (object sender, EventArgs e)
14 {
15
16}
17
18 protected void button#click (object sender, EventArgs e)
19 {
20 Model. user thisUser = new user ();
21 thisUser. name = TB_username.Text.ToString ();
22 BLL. userBLL uB = new userBLL ();
23 if (uB. addUser (thisUser ))
24 {
25 Response. Write ("true ");
26}
27 else
28 {
29 Response. Write ("false ");
30}
31}
32}
33}
7. Press Ctrl + F5 to run default. aspx, enter the value in the text box, and click the button. The value in the textbox is added to the database.
PS:
Possible errors:
Cause: no reference is added. The above code has been resolved.
From Mu ran