三层包图:
表示层UI,业务逻辑层BLL,数据访问层DAL,和实体层Entity。
类图:
UI层的类图是FrmLogin.
BLL层的类图是Login
DAL层的类图是UserDAO
Entity层的类图是UserInfo
序列图:
登录界面:
UI层代码:
public partial class FrmLogin : Form { public FrmLogin() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { string userName = txtUserName.Text.Trim(); string password = txtPassword.Text; try { Login.BLL.LoginManager mgr = new Login.BLL.LoginManager(); Login.Model.UserInfo user = mgr.UserLogin(userName, password); MessageBox.Show("登录用户:" + user.UserName); } catch (Exception err) { MessageBox.Show(err.Message.ToString() ); } }
BLL层代码:
public class LoginManager { public Login .Model .UserInfo UserLogin(string userName, string password) { Login.DAL.UserDAO uDao = new Login.DAL.UserDAO(); if (userName == null) { throw new Exception("请输入用户名!"); } if (password == null) { throw new Exception("请输入密码!"); } Login .Model .UserInfo user= uDao.SelectUser(userName, password); if (user != null) { return user; } else { throw new Exception("登录失败!"); } } }
DAL层代码:
public class UserDAO { public static string ConnString = @"Server=XIAOZUO-PC;Database=Login;User ID =sa;Password=123456"; public Login .Model .UserInfo SelectUser(string userName, string password) { using (SqlConnection conn = new SqlConnection(ConnString)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText =@"select ID,UserName,Password,Email from Users where UserName=@UserName and Password=@Password"; cmd .CommandType =CommandType .Text ; cmd.Parameters.Add(new SqlParameter("@UserName", userName)); cmd.Parameters.Add(new SqlParameter("@Password", password)); conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); Login.Model .UserInfo user=null; while (reader .Read ()) { 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.Email =reader.GetString (3); } } return user ; } } }
Entity层代码:
public class UserInfo { public int ID { get; set; } public string UserName {get;set;} public string Password {get;set;} public string Email {get;set;} }