三层架构之简单用户登录

业务逻辑是简单的登录,如果登录成功,增加10点积分
1.创建一个解决方案取名为:LoginSolution
2.创建项目名为:LoginBLL(业务逻辑层),LoginDAL(数据访问层),LoginUI(表示层),LoginModel(数据模型)
2010060620432987.jpg
 3.创建数据库Login,创建表:user(用户表) 
2010060620455798.jpg
Scores(积分表)
2010060620461339.jpg
4.数据访问层
UserDAO类: 根据用户名和密码,如果数据库中有,将返回用户模型

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Text;
5   using System.Data;
6   using System.Data.SqlClient;
7   namespace Login.DAL
8 {
9 public class UserDAO
10 {
11 public Login.Model.UserInfo SelectUser( string userName, string passWord)
12 {
13 using (SqlConnection con = new SqlConnection(DAL.DbUtil.ConnString))
14 {
15 SqlCommand cmd = con.CreateCommand();
16 cmd.CommandText = @" select * from [user] where UserName=@userName and Password=@passWord " ;
17 cmd.CommandType = CommandType.Text;
18 cmd.Parameters.Add( new SqlParameter( " @userName " ,userName));
19 cmd.Parameters.Add( new SqlParameter( " @passWord " ,passWord));
20
21 con.Open();
22 SqlDataReader reader = cmd.ExecuteReader();
23 Login.Model.UserInfo user = null ;
24 while (reader.Read())
25 {
26 if (user == null )
27 {
28 user = new Model.UserInfo();
29 }
30 user.ID = reader.GetInt32( 0 );
31 user.UserName = reader.GetString( 1 );
32 user.Password = reader.GetString( 2 );
33 if ( ! reader.IsDBNull( 3 ))
34 user.Email = reader.GetString( 3 );
35
36 }
37 return user;
38 }
39
40 }
41 }
42 }

 

ScoreDAO类:如果登陆成功,将积分加10

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Text;
5   using System.Data;
6   using System.Data.SqlClient;
7
8   namespace Login.DAL
9 {
10 public class ScoreDAO
11 {
12 public void UpdateScore( string userName, int value)
13 {
14 using (SqlConnection conn = new SqlConnection(DAL.DbUtil.ConnString))
15 {
16 SqlCommand cmd = conn.CreateCommand();
17 cmd.CommandText = @" insert into Scores(UserName,Score)Values(@userName,@value) " ;
18 cmd.CommandType = CommandType.Text;
19 cmd.Parameters.Add( new SqlParameter( " @userName " ,userName));
20 cmd.Parameters.Add( new SqlParameter( " @value " ,value));
21 conn.Open();
22 cmd.ExecuteNonQuery();
23
24 }
25 }
26 }
27 }
28  

 

DbUtil类:返回连接字符串

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Text;
5
6   namespace Login.DAL
7 {
8 public class DbUtil
9 {
10 public static string ConnString = @" Data Source=admin-PC\SQLEXPRESS;Initial Catalog=Login;Integrated Security=True " ;
11 }
12 }
13  

 

5.业务逻辑层
LoginMange类:表示层传递用户名和密码,如果数据库中有,将积分叫10

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Login.BLL
7 {
8 public class LoginManger
9 {
10
11
12 public Model.UserInfo UserLogin( string username, string password)
13 {
14 DAL.UserDAO uDao = new DAL.UserDAO();
15 Login.Model.UserInfo user = uDao.SelectUser(username,password);
16 if (user != null ) // login sucessfully.
17 {
18 DAL.ScoreDAO sDao = new DAL.ScoreDAO();
19 sDao.UpdateScore(username, 10 );
20 return user;
21 }
22 else
23 {
24 throw new Exception( " 登录失败! " );
25 }
26
27 }
28
29
30 }
31 }

 

为什么要数据模型?
因为函数UserLogin只能返回用户名(string),或密码,但是要想要整个用户信息,可以用用户模型
6.数据模型
UserInfo类:提供用户基本信息:如ID,username 等

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Login.Model
7 {
8 public class UserInfo
9 {
10 public int ID { get ; set ; }
11 public string UserName { get ; set ; }
12 public string Password { get ; set ; }
13 public string Email { get ; set ; }
14 }
15 }
16

 

7.表示层

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 private void btnLogin_Click( object sender, EventArgs e)
2 {
3
4 string username = txtusername.Text.Trim();
5 string password = txtpassword.Text;
6 Login.BLL.LoginManger mgr = new Login.BLL.LoginManger();
7 Login.Model.UserInfo user = mgr.UserLogin(username,password);
8 MessageBox.Show( " 登录用户 " + user.UserName);
9 }

转载于:https://www.cnblogs.com/luhuan860/archive/2010/06/06/1752779.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值