Three-tier implementation of user login C #

Source: Internet
Author: User

in the previous article, we talked about layer three. The next step is to use a detailed example of how to implement user login interface with three layers.

Model Entity (Loginmodel):

namespace loginmodel{     //Join class: UserInfo  model entity action: Encapsulates the data so that the data is transferred in layer three. More inclined to business logic Layer public    class UserInfo    {        //define user Properties public        int ID {get; set;}        public string UserName {get; set;}        public string Password {get; set;}}            }
second, the design of the UI layer:

The first simple interface believes that everyone will:

Next is the code:

namespace loginui{public    partial class Frmlogin:form    {public        frmlogin ()        {            InitializeComponent ();        }        private void Btnlogin_click (object sender, EventArgs e)        {            //ui layer passes user input data to the BLL layer            string userName = TxtUserName.Text.Trim ();            string password = txtPassword.Text;            Loginbll.loginmanager mgr = new Loginbll.loginmanager ();            Loginmodel.userinfo user = Mgr. Userlogin (userName, password);            Returns the UI layer to the user            MessageBox.Show ("Logged in User:" + user) for the data passed by the business layer. UserName);}}}           

Three, the BLL layer code

namespace loginbll{public    class Loginmanager    {public        loginmodel.userinfo userlogin (String userName, String password)        {            Logindal.userdao Udao = new Logindal.userdao ();//Instantiate DAL layer            loginmodel.userinfo user = Udao.selectuser (userName, password);            No need to access the data source to run the business logic directly            if (user! = null)            {                return user;            }            else            {                throw new Exception ("Login Failed");                            }        }    }
Four, the DAL layer code

 public class Userdao {public Loginmodel.userinfo selectuser (string userName, String Password) { Establish database connection string connstring = @ "server=192.168.24.154;database= computer room charge system personnel records; User Id=sa;            Password=123 "; using (SqlConnection conn = new SqlConnection (connstring)) {//Run SQL statement for query sqlcom Mand cmd = conn.                CreateCommand (); Cmd.                CommandText = @ "Select Id,username,password from UserInfo WHERE [email protected] and [email protected]"; Output query result cmd.                CommandType = CommandType.Text; Add two number of parameters to CMD.                Parameters.Add (New SqlParameter (@ "UserName", UserName)); Cmd.                Parameters.Add (New SqlParameter (@ "Password", Password)); Conn.                Open (); SqlDataReader reader = cmd.                ExecuteReader ();                Loginmodel.userinfo user = null; Read the detailed data while (reader. Read ())               {if (user = = null) {user = new loginmodel.us                    Erinfo (); }//Read the query to the data User.ID = reader.                    GetInt32 (0); User. UserName = reader.                    GetString (1); User. Password = reader.                                   GetString (2);            } return user; }        }    }}


Login:

The results show:

Three-tier implementation of user 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.