最简单的三层实例(C#)

三层包图:

表示层UI,业务逻辑层BLL,数据访问层DAL,和实体层Entity。

类图:

UI层的类图是FrmLogin.

BLL层的类图是Login

DAL层的类图是UserDAO

Entity层的类图是UserInfo

序列图:

登录界面:

UI层代码:

[csharp]  view plain copy
  1. public partial class FrmLogin : Form  
  2.     {  
  3.         public FrmLogin()  
  4.         {  
  5.             InitializeComponent();  
  6.         }  
  7.   
  8.         private void btnLogin_Click(object sender, EventArgs e)  
  9.         {  
  10.             string userName = txtUserName.Text.Trim();  
  11.             string password = txtPassword.Text;  
  12.               
  13.             try  
  14.             {  
  15.                 Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();  
  16.                 Login.Model.UserInfo user = mgr.UserLogin(userName, password);  
  17.                 MessageBox.Show("登录用户:" + user.UserName);  
  18.             }  
  19.             catch (Exception err)  
  20.             {  
  21.                 MessageBox.Show(err.Message.ToString() );  
  22.             }           
  23.   
  24.         }  


BLL层代码:

  

[csharp]  view plain copy
  1. public class LoginManager  
  2. {  
  3.     public Login .Model .UserInfo  UserLogin(string userName, string password)  
  4.     {  
  5.          
  6.         Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();  
  7.         if (userName == null)  
  8.         {  
  9.             throw new Exception("请输入用户名!");                  
  10.         }  
  11.   
  12.         if (password == null)  
  13.         {  
  14.             throw new Exception("请输入密码!");  
  15.         }  
  16.   
  17.         Login .Model .UserInfo user= uDao.SelectUser(userName, password);  
  18.   
  19.         if (user != null)  
  20.         {  
  21.             return user;  
  22.         }  
  23.         else  
  24.         {  
  25.             throw new Exception("登录失败!");  
  26.         }  
  27.     }  
  28. }  


DAL层代码:

[csharp]  view plain copy
  1. public class UserDAO  
  2. {  
  3.     public static string ConnString = @"Server=XIAOZUO-PC;Database=Login;User ID =sa;Password=123456";  
  4.     public Login .Model .UserInfo  SelectUser(string userName, string password)  
  5.     {              
  6.         using (SqlConnection conn = new SqlConnection(ConnString))     
  7.         {  
  8.             SqlCommand cmd = conn.CreateCommand();  
  9.             cmd.CommandText =@"select ID,UserName,Password,Email from Users where UserName=@UserName and Password=@Password";  
  10.               
  11.             cmd .CommandType =CommandType .Text ;  
  12.             cmd.Parameters.Add(new SqlParameter("@UserName", userName));  
  13.             cmd.Parameters.Add(new SqlParameter("@Password", password));  
  14.   
  15.             conn.Open();  
  16.             SqlDataReader reader=cmd.ExecuteReader();  
  17.               
  18.             Login.Model .UserInfo user=null;  
  19.             while (reader .Read ())  
  20.             {  
  21.                 if (user ==null)  
  22.                 {  
  23.                     user =new Login.Model .UserInfo ();  
  24.                 }  
  25.                 user .ID =reader .GetInt32 (0);  
  26.                 user .UserName =reader.GetString (1);  
  27.                 user.Password =reader.GetString (2);  
  28.                 if (reader .IsDBNull (3))  
  29.                 {  
  30.                     user.Email =reader.GetString (3);  
  31.                 }  
  32.             }  
  33.             return user ;  
  34.         }  
  35.     }  
  36.   
  37. }  


Entity层代码:

[csharp]  view plain copy
  1. public class UserInfo  
  2. {  
  3.     public int ID { getset; }  
  4.     public  string UserName {get;set;}  
  5.     public string Password {get;set;}  
  6.     public string Email {get;set;}  
  7.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值