Three-tier login-c#

Source: Internet
Author: User

Learn three layers and have a login window for a little exercise. It was the first time we touched the three-storey prelude. Now just a simple understanding of some, need to learn a lot, the following talk about their own understanding.

The three layer we're talking about is layering the display layer, the business logic layer, and the data access layer. The display layer is directly with the customer, is the user's display interface, and the business logic layer is to deal with the business, is the Middle bridge. Contact the display layer and data access layer, the user's request for analysis and processing, in the data source to write; So what does the data access layer do? The main manipulation of the database, the data in the database for the processing of increased deletion and checking. In this way, each level has its own specific function, which achieves the effect of cohesion-poly-low coupling.

This is a simple login window implementation process.


"Show 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 () //username assigns a value to username        string Password=txtpassword. Text;     Password assigns a value to password        login<span style= "color: #FF0000;" >.<strong>bll. Loginmanager</strong></span> mgr = new Login.BLL.LoginManager ();//instantiation of Loginmanager in the BLL layer, implementation        of two references Login.<span style= "color: #333333;" ><strong>Model.UserInfo</strong></span> User=mgr. Userlogin (UserName, Password);//Call the method in the BLL layer userlogin        MessageBox.Show ("Logged in User:" + user.) UserName);</span>
"Business logic layer of the 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 & in the BLL layer Lt;span style= "color: #FF0000;" ><strong>UserLogin</strong></span> (String username,string Password) {Login.<spa n style= "color: #FF0000;" ><strong>dal. userdao</strong></span> Udao = new Login.DAL.UserDAO ();//Instantiate Userado Login.<strong>model in the DAL layer . Userinfo</strong> User=udao.selectuser (UserName, Password);//In the data layer dal. Call Method Selectuser inference username and password if (user!=null) {Login.DAL.ScoreDAO SDao = new Log In. DAL. Scoredao ();//instance the Scoredao of the DAL layer.                Add the operation to the user who has logged in successfully sdao.updatescore (UserName, 10);            return user;            } else {throw new Exception ("Login 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 Database public Login.Model.UserInfo <span style=" color: #FF0000; " ><strong>SelectUser</strong></span> (String username,string Password) {using (Sqlco Nnection conn = new SqlConnection (dbutil.constring))//Open connection data through the parameters dbutil.constring {SqlCommand c MD = conn. CreateCommand ();//create CMD to run SQL statement cmd. CommandText = @ "Select Id,username,password,emall from Users WHERE [email protected] and [email protected]"; /sql statement cmd.                CommandType = CommandType.Text; Cmd. Parameters.Add (New SqlParameter ("@UserName", UserName));//parameters joins the parameter cmd.                Parameters.Add (New SqlParameter ("@Password", Password)); Conn. Open ();//Opens the data source SqlDataReader reader = cmd. ExecuteReader ();//Read data and get results Login.model.                UserInfo user = null; while (reader. Read ())//reading 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)//update fractional operation {using (Sq Lconnection conn=new SqlConnection (dbutil.constring)//Open database link {SqlCommand cmd = conn. CreateCommand ();//create Run SQL statement cmd.commandtext = @ "INSERT into score (username,score) Values (@UserName,@Score) ";//sql statement  cmd. Parameters.Add (New SqlParameter ("@UserName", UserName)); Add cmd to the database.                Parameters.Add (New SqlParameter ("@Score", value)); Cmd.                Parameters.Add (New SqlParameter ("@ID", value)); Conn. Open (); Turn on database cmd. ExecuteNonQuery ();//Run SQL statement, return integer variable}}</span>

The three-layer login is the UI layer referencing the BLL layer logic for business analysis, the BLL layer refers to the DAL layer for data inference, assuming that the correct data users can log on successfully through the BLL layer back to the UI layer, UI display to the customer. The model layer is encapsulated data, provided to the other three levels, it is independent, does not reference no matter what layer of logic, and even do not know the existence of other levels, just provide services, is the most selfless. The UI refers to the BLL layer and model layer, and the BLL layer refers to the Dal and model layers. Thus the interface between the UI layer and the Dal is realized, and the DAL layer refers to the model layer, mainly to the operation of the database.

Three layer is to divide a system into parts, each part is responsible for a certain responsibility, each of its duties, the division of its Shou. No More zu generation!


Three-tier login-c#

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.