Three-tier architecture and four kings-Growth

Source: Internet
Author: User

1. Previous collection Review

In the previous article, we introduced the operating mechanism of the three-tier architecture, which includes an understanding of the entity class. Then, we took a simple login as an example to show it, next let's take a look at the interaction between the three-tier architecture and the "Four Kings.

The four most basic and important functions are addition, deletion, modification, and query of data. Next we will show you how to add data to the database by using Layer 3.

2. Add (1) UI Layer First, the user enters the data to be added to the U layer.

The following code confirms the addition:
Private void button#click (object sender, EventArgs e) {Login. model. userInfo user = new Login. model. userInfo (); // instantiate a user's entity, the most layer-3 information transferred by Login. model. userInfo User = new Login. model. userInfo (); // instantiate a user entity to receive the returned signal UserAdd. BLL. addBLL userAB = new UserAdd. BLL. addBLL (); // instantiate the B-layer object // obtain the data to be saved, all packaged into the user object. userName = txtUserName. text. trim (); user. password = txtPassword. text; // send the user to layer B for processing and return the result to Us Er User = userAB. Add (user); if (User. Flag) {MessageBox. Show ("User Added successfully! ");} Else if (User. Flag = false) {MessageBox. Show (" this User already exists! ");} Else {MessageBox. Show (" failed to add user! ");}}
(2) BLL Layer 
The entity sent from the U layer is handed over to the D Layer for processing, and then the entity sent from the D layer is returned. 
Public class AddBLL {// Method for adding information, passing in an object type, and returning an object type public Login. model. userInfo Add (Login. model. userInfo user) {// instantiate layer D UserAdd. DAL. addDAL userAD = new UserAdd. DAL. addDAL (); // instantiate an object to receive the returned data Login. model. userInfo User = new Login. model. userInfo (); // transmits the entity user to layer D for processing. The entity User receives the returned data User = userAD. add (user); return User; // returns entity information }}
(3) DAL LayerWrite the entity information sent from layer B to the database, and then return whether the operation is successful (this is omitted due to the DBUtil class in the previous article)
Public class AddDAL {public Login. model. userInfo Add (Login. model. userInfo user) {// query function to check whether the user exists. If so, you cannot add using (SqlConnection conn = new SqlConnection (DBUtil. connString) // used to connect to the database and automatically disable {SqlCommand cmd = conn. createCommand (); // used to operate the database cmd. commandText = @ "SELECT * from users where UserName = @ UserName"; // query language cmd. commandType = CommandType. text; cmd. parameters. add (new SqlParameter ("@ UserName", user. userName); // input the conn parameter. open (); // Open the link SqlDataReader reader = cmd. executeReader (); // read the variable Login of the row. model. userInfo User = new Login. model. userInfo (); // instantiate the object and use the while (reader. read () {// if the User name exists, the returned User is Flase if (User = null) {User. flag = false;} return User; // return information, end operation }}// if the User does not exist, save the information using (SqlConnection conn = new SqlConnection (DBUtil. connString) {SqlCommand cmd = conn. createCommand (); cmd. commandText = @ "insert into users (UserName, Password, Email) VALUES (@ username, @ password, @ email)"; cmd. commandType = CommandType. text; // input the cmd parameter. parameters. add (new SqlParameter ("@ username", user. userName); cmd. parameters. add (new SqlParameter ("@ password", user. password); cmd. parameters. add (new SqlParameter ("@ email", user. email); conn. open (); cmd. executeNonQuery (); // execute the SQL add statement, return 1 // instantiate the object, and return Login. model. userInfo User = new Login. model. userInfo (); User. flag = true; return User ;}}}
Finally, return the information, judge, and add successfully.



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.