业务逻辑是简单的登录,如果登录成功,增加10点积分
1.创建一个解决方案取名为:LoginSolution
2.创建项目名为:LoginBLL(业务逻辑层),LoginDAL(数据访问层),LoginUI(表示层),LoginModel(数据模型)
3.创建数据库Login,创建表:user(用户表)
Scores(积分表)
4.数据访问层
UserDAO类: 根据用户名和密码,如果数据库中有,将返回用户模型
代码
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
代码
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类:返回连接字符串
代码
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
代码
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 等
代码
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.表示层
代码
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 }