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 #