Layer-3 logon-c #

Source: Internet
Author: User

Layer-3 logon-c #

After learning the three layers, there is a small logon form exercise, which is the first time we have been in touch with the three layers. Now, I just learned a little bit about it, and I still need to learn a lot about it. Let's talk about my understanding below.

The three layers are the display layer, business logic layer, and data access layer. The display layer directly deals with customers and displays user interfaces; the business logic layer processes the business and serves as a bridge between the display layer and the data access layer to analyze and process user requests and write data to the data source; what does the data access layer do? It mainly operates the database to add, delete, modify, and query data in the database. In this way, each layer has its own specific functions, achieving the effect of high cohesion and low coupling.

This is a simple login form implementation process.

[Display UI Layer]

 

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> private void btnOK_Click (object sender, EventArgs e) // login button string UserName = txtUserName. text. trim (); // assign UserName to UserName string Password = txtPassWord. text; // The Password is assigned to Password Login <span style = "color: # FF0000;">. <strong> BLL. loginManager </strong> </span> mgr = new Login. BLL. loginManager (); // instance the LoginManager In The BLL layer. The two references implement Login. <span style = "color: #333333;"> <strong> Model. userInfo </strong> </span> user = mgr. userLogin (UserName, Password); // call the method UserLogin MessageBox In The BLL layer. show ("Login user:" + user. userName); </span>
Business logic layer BLL Layer]
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> public class <span style = "color: # FF0000; "> <strong> LoginManager </strong> </span> // call LoginManager {public Login. model. userInfo <span style = "color: # FF0000;"> <strong> UserLogin </strong> </span> (string UserName, string Password) {Login. <span style = "color: # FF0000;"> <strong> DAL. userDAO </strong> </span> uDao = new Login. DAL. userDAO (); // instantiate Use in the DAL Layer RADO Login. <strong> Model. userInfo </strong> user = uDao. selectUser (UserName, Password); // In the data layer DAL, call the method SelectUser to determine the user name and Password if (user! = Null) {Login. DAL. scoreDAO sDao = new Login. DAL. scoreDAO (); // instance ScoreDAO at the DAL layer, which is used to add points to successfully logged on users. updateScore (UserName, 10); return user;} else {throw new Exception ("Logon Failed") ;}</span>
Data access layer DAL]
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> public static string conString = @ "Server = LFH; Database = LoginSanCeng; User ID = sa; password = 1 "; // link to the public Login database. model. userInfo <span style = "color: # FF0000;"> <strong> SelectUser </strong> </span> (string UserName, string Password) {using (SqlConnection conn = new SqlConnection (DBUtil. conString) // use the DBUtil parameter. conString open connection data {SqlCommand cmd = conn. CRES AteCommand (); // create cmd and execute the SQL statement cmd. commandText = @ "select id, UserName, Password, Emall FROM Users WHERE UserName = @ UserName AND Password = @ Password"; // SQL statement cmd. commandType = CommandType. text; cmd. parameters. add (new SqlParameter ("@ UserName", UserName); // Parameters Add the cmd parameter. parameters. add (new SqlParameter ("@ Password", Password); conn. open (); // Open the data source SqlDataReader reader = cmd. executeReader (); // read the data and obtain the result Login. model. userInfo user = null; while (reader. read () // Read data {if (user = null) {user = new Login. model. userInfo ();} user. ID = reader. getInt32 (0); user. userName = reader. getString (1); user. password = reader. getString (2); if (! Reader. isDBNull (3) {user. emall = reader. getString (3) ;}} return user ;}} public void <span style = "color: # FF0000; "> <strong> UpdateScore </strong> </span> (string UserName, int value) // operation for updating scores {using (SqlConnection conn = new SqlConnection (DBUtil. conString) // open the database link {SqlCommand cmd = conn. createCommand (); // create and execute the SQL statement cmd. commandText = @ "insert into score (UserName, Score) Values (@ UserName, @ Score)"; // SQL statement & nbsp; cmd. parameters. add (new SqlParameter ("@ UserName", UserName); Add to database cmd. parameters. add (new SqlParameter ("@ Score", value); // cmd. parameters. add (new SqlParameter ("@ ID", value); conn. open (); Open the database cmd. executeNonQuery (); // execute the SQL statement and return the integer variable }}</span>

Among them, layer-3 logon refers to the logic of the BLL layer for business analysis in the UI Layer, and The BLL layer references the DAL layer for data judgment. If the data is correct, the user can log on successfully and return it to the UI Layer through The BLL layer, the UI is displayed to the customer. The Model layer encapsulates data and provides it to the other three layers. It is independent and does not reference the logic of any layer, or even knows the existence of other layers. It only provides services, is the most selfless. The UI references The BLL layer and Model layer. The BLL layer references the DAL and Model layers to realize indirect connections between the UI Layer and the DAL layer, while the DAL layer references the Model layer, mainly for database operations.

The third layer is to divide a system into parts, each of which is responsible for a certain number of responsibilities and performs their respective duties. Each division is responsible for them and cannot be replaced!

Related Article

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.