1. 准备工作
新建一个数据库StudentDB
1
2
3
4
5
6
7
8
|
-- 使用master 数据库 use master go if exists(select *from sysdatabases where name= 'StudentDB' ) drop database StudentDB go create database StudentDB go |
在StudentDB中新建三张表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
use StudentDB go -- 新建学生表 if exists (select *from sysobjects where name= 'Student' ) drop table Student go create table Student ( stuId int primary key identity(2000,1), stuName varchar(30) not null , stuSex char (2) not null , stuAge int not null , stuTel varchar(11) not null , stuPWd varchar(30) not null ) -- 添加学生表的数据 insert into Student (stuName,stuSex,stuAge,stuTel,stuPWd)values( '张三' , '男' ,21, '12345678543' , '123456' ) insert into Student (stuName,stuSex,stuAge,stuTel,stuPWd)values( '赵六' , '男' ,21, '12345678543' , '123456' ) insert into Student (stuName,stuSex,stuAge,stuTel,stuPWd)values( '韩菲' , '女' ,20, '12345678543' , '123456' ) -- 新建教师表 if exists (select *from sysobjects where name= 'Teacher' ) drop table Teacher go create table Teacher ( tId int primary key identity(4000,1), tName varchar(30) not null , tSex char (2) not null , tAge int not null , tTel varchar(11) not null , tTitle varchar(20), tPwd varchar(30) not null ) --添加教师表 insert into Teacher(tName,tSex,tAge,tTel,tTitle,tPwd)values( 'xx' , '男' ,32, '12345678901' , '副教授' , '123456' ) -- 新建管理员 if exists (select *from sysobjects where name= 'Admin' ) drop table Admin go create table Admin ( adminId int primary key identity(4000,1), adminName varchar(30) not null , adminPWd varchar(30) not null ) -- 添加管理员表 insert into Admin(adminName,adminPwd) values( 'admin' , '123456' ) |
新建一个winform 项目,修改文本框name 为,txtUserName,txtPwd;登录按钮name 为btnLogin、btnExit。
2.实现登录
功能实现分析
- 当用户点击登录的时候,程序首先判断用户名、密码是否为空,然后再根据单选按钮的值,去判断是哪一个角色进行登录。
- 上面的事情做好以后,我们要去把用户名和密码拿到数据库进行比较。先使用用户名当作查询条件,返回一个用户对象(管理员、学生、教师,根据具体情况而定,因为我们是用主键当作用户名,主键可以区分一个用户,所以使用用户名查询只返回一条数据)。判断对象是否为null,如果为null则说明用户不存在。否则就判断密码是否正确。
准备实体类
在项目中新建三个类,类名和表名一致,字段名和表里面的字段名一致。
添加类,选中项目->添加->类
管理员类
1
2
3
4
5
6
|
public class Admin { public int adminId { get ; set ; } public String adminName { get ; set ; } public String adminPwd { get ; set ; } } |
学生类
1
2
3
4
5
6
7
8
9
10
11
|
public class Student { public int stuId { get ; set ; } public string stuName { get ; set ; } public string stuSex { get ; set ; } public int stuAge { get ; set ; } public string stuTel { get ; set ; } public string stuPwd { get ; set ; } } |
教师类
1
2
3
4
5
6
7
8
9
10
|
public class Teacher { public int tId { get ; set ; } public string tName { get ; set ; } public string tSex { get ; set ; } public int tAge { get ; set ; } public string tTel { get ; set ; } public string tTitle { get ; set ; } public string tPWd { get ; set ; } } |
准备DBHelper类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
public class DbHelper { /// <summary> /// 获取连接对象 /// </summary> /// <returns></returns> public static SqlConnection GetConnection() { SqlConnection conn = null ; try { //可能发生错误的代码 if (conn == null ) { conn = new SqlConnection(); conn.ConnectionString = ConfigurationManager.ConnectionStrings[ "connString" ].ToString(); conn.Open(); conn.Close(); } return conn; } catch (Exception ex) { //发生异常以后要做的事情 throw ex; // 把问题抛出,让程序员知道那里出了错误 } } /// <summary> /// 执行增删改 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int GetExcuet( string sql) { // 1. 获取连接对象 SqlConnection conn = GetConnection(); try { // 2.打开链接 conn.Open(); //3.创建SqlCommand对象,sql语句,连接对象 SqlCommand cmd = new SqlCommand(sql, conn); // 4.执行SQL,并返回受影响的行数 return cmd.ExecuteNonQuery(); ; } catch (Exception ex) { throw ex; } finally { conn.Close(); conn.Dispose(); } } /// <summary> /// 返回执行查询的结果 /// </summary> /// <returns></returns> public static DataTable GetDataSet(String sql) { try { // 1.获取链接对象 SqlConnection conn = GetConnection(); //2.创建适配器对象 SqlDataAdapter da = new SqlDataAdapter(sql, conn); //3.创建DataSet 对象 DataSet ds = new DataSet(); da.Fill(ds); return ds.Tables[0]; } catch (Exception ex) { throw ex; } } } |
在App.config中添加 数据库连接字符串,在configuration标签下进行添加
1
2
3
|
<connectionStrings> <add name= "connString" connectionString= "Data Source=.;Initial Catalog=StudentDB;Persist Security Info=True;User ID=sa;Password=123456" /> </connectionStrings> |
实现点击事件
当用户点击时候我们就去执行登录事件
根据我们分析,我们首先要判断用户和密码是否正确
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//获取用户名和密码 string username = txtUserName.Text.Trim(); string pwd = txtPwd.Text.Trim(); //当用户名为空的时候就不往下面执行了 if (username.Equals( "" )) { MessageBox.Show( "用户名不能为空" ); return ; } if (pwd.Equals( "" )) { MessageBox.Show( "密码不能为空" ); return ; } |
在判断完所有的公共问题以后,接下来我们就要去判断是哪一个用户进行的登录的,我们可以通过单选按钮的checked属性,进行判断,然后分别去调用他们进行登录的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//管理员登录 if (radAdmin.Checked) { AdminLogin(username); } //学生登录 if (radStudent.Checked) { StudentLogin(); } //教师登录 if (radTeacher.Checked) { TeacherLogin(); } |
管理员登录方法实现,根据管理员的用户名进行查询,判断返回表的行数,如果行数小于1,那么表示改用户不存在,返回null,否则返回一个管理员对象。其他的类似
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
private Admin AdminLogin(String username) { string sql = string .Format( "select *from Admin where adminId={0}" ,username); DataTable table= DbHelper.GetDataSet(sql); //判断表的行数,大于等于1表示有数据,用户存在,否则返回null if (table.Rows.Count < 1) return null ; //新建一个admin对象 Admin admin = new Admin(); admin.adminId = Convert.ToInt32(table.Rows[0][ "adminId" ]); admin.adminName = table.Rows[0][ "adminName" ].ToString(); admin.adminPwd = table.Rows[0][ "adminPwd" ].ToString(); return admin; } |
学生的登录方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private Student StudentLogin( string username) { string sql = string .Format( "select *from Student where stuId={0}" , username); DataTable table = DbHelper.GetDataSet(sql); //判断表的行数,大于等于1表示有数据,用户存在,否则返回null if (table.Rows.Count < 1) return null ; /*新建一个student对象 ,这里只给了三个字段进行了赋值, * 因为我们登录的时候,只用到了id和密码, * 其他时候根据需求进行赋值 */ Student student = new Student(); student.stuId = Convert.ToInt32(table.Rows[0][ "stuId" ]); student.stuName = table.Rows[0][ "stuName" ].ToString(); student.stuPwd = table.Rows[0][ "stuPwd" ].ToString(); return student; } |
教师的登录方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private Teacher TeacherLogin( string username) { string sql = string .Format( "select *from Teacher where tId={0}" , username); DataTable table = DbHelper.GetDataSet(sql); //判断表的行数,大于等于1表示有数据,用户存在,否则返回null if (table.Rows.Count < 1) return null ; /*新建一个student对象 ,这里只给了三个字段进行了赋值, * 因为我们登录的时候,只用到了id和密码, * 其他时候根据需求进行赋值 */ Teacher teacher = new Teacher(); teacher.tId = Convert.ToInt32(table.Rows[0][ "tId" ]); teacher.tName = table.Rows[0][ "tName" ].ToString(); teacher.tPWd = table.Rows[0][ "tPWd" ].ToString(); return teacher; } |
登录方法完成以后,我要对返回来的结果进行处理。首先判断对象是否为null,为null就说用户不存在。反之对象的密码进行比较,密码正确就弹出登录成功,密码不正确就提示密码不正确。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
private void btnLogin_Click( object sender, EventArgs e) { //获取用户名和密码 string username = txtUserName.Text.Trim(); string pwd = txtPwd.Text.Trim(); if (username.Equals( "" )) { MessageBox.Show( "用户名不能为空" ); return ; } if (pwd.Equals( "" )) { MessageBox.Show( "密码不能为空" ); return ; } //管理员登录 if (radAdmin.Checked) { /*为什么要返回来,因为以后处理逻辑可能在不同类里面, * 这里只是模拟进行分层操作*/ Admin admin= AdminLogin(username); if (admin == null ) { MessageBox.Show( "用户不存在" ); return ; } if (!admin.adminPwd.Equals(pwd)) { MessageBox.Show( "密码错误" ); return ; } } //学生登录 if (radStudent.Checked) { Student student= StudentLogin(username); if (student == null ) { MessageBox.Show( "用户不存在" ); return ; } if (!student.stuPwd.Equals(pwd)) { MessageBox.Show( "密码错误" ); return ; } } //教师登录 if (radTeacher.Checked) { Teacher teacher= TeacherLogin(username); if (teacher == null ) { MessageBox.Show( "用户不存在" ); return ; } if (!teacher.tPWd.Equals(pwd)) { MessageBox.Show( "密码错误" ); return ; } } MessageBox.Show( "登录成功" ); } |
总结
- 在登录中,我们首先先判断公共的条件,比如说用户名、密码为空等情况,巧用return ,让整个代码的逻辑变得简单。
- 在判断完公共的条件以后,在针对每一个具体的角色进行判判断。
- 使用使用用户名进行查询,判断返回表的行数,如果行数小于1那么,用户不存在,否则实例一个对象,并给对象赋值
- 判断返回对象的是否为空,如果为空则用户不存在,否则用户存在
- 最后对密码进行判断,如果密码正确则执行登录成功的操作,如果密码不正确则弹出密码不正确的提示。
- 项目下载地址:点击这里
到此这篇关于C#中实现登录功能的文章就介绍到这了,更多相关C#登录功能内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/golden_stone_fish/article/details/117601767