C#winForm连接SQL Server,创建登录窗口,用户注册窗口
用到的工具:VS2015,SQL Server
数据表userinfo:(直接复制在SQL Server新建查询中即可)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22GO /****** Object: Table [dbo].[userinfo] Script Date: 2020/10/14 18:46:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[userinfo]( [id] [nvarchar](40) NOT NULL, [name] [nvarchar](50) NOT NULL, [pwd] [nvarchar](50) NOT NULL, [role] [nvarchar](20) NOT NULL, CONSTRAINT [PK_userinfo] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
登录界面FrmLogin:,注册界面:
操作数据表的类Userinfo:
Userinfo.cs
复制代码
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using DBConn; namespace 注册.Class { /// <summary> /// 该类包含两个个方法 /// 用户登录,用户注册 /// </summary> class Userinfo { /// <summary> /// 登录方法 /// </summary> /// <param name="user_id">用户名</param> /// <param name="user_pwd">用户密码</param> /// <returns></returns> public string Login(string user_id,string user_pwd) { //创建连接 SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();//快速构建连接SQL字符串 scsb.DataSource = "127.0.0.1";//本机名,可以是127.0.0.1或.或本机计算机名 scsb.UserID = "sa";//数据库登录名 scsb.Password = "123";//数据库登录密码 scsb.InitialCatalog = "Apexbio";//要连接的数据库名 SqlConnection conn = new SqlConnection(scsb.ToString());//实例化连接SQL数据库的类的对象 if (conn.State==ConnectionState.Closed) { conn.Open(); } //拼接T-SQL语句 string sqlStr = "SELECT * FROM userinfo WHERE id='" + user_id + "'AND pwd='" + user_pwd + "'"; SqlCommand comm = new SqlCommand(sqlStr,conn);//实例化SQLCommand对象 SqlDataReader dr = comm.ExecuteReader();//执行SQL语句,并接收返回受影响的行数 if (dr.Read()) //如果行数大于0,则说明用户名和密码没问题 { dr.Close(); conn.Close(); return null; } else { dr.Close(); conn.Close(); return "用户名或密码错误"; } } /// <summary> /// 注册用户 /// </summary> /// <param name="user_id">用户id</param> /// <param name="user_pwd">用户密码</param> /// <param name="user_name">用户名</param> /// <param name="user_group">用户权限</param> /// <returns></returns> public string Register(string user_id, string user_pwd, string user_name,string user_group) { //创建SQL连接 SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();//快速构建连接SQL字符串 scsb.DataSource = "127.0.0.1";//本机名,可以是127.0.0.1或.或本机计算机名 scsb.UserID = "sa";//数据库登录名 scsb.Password = "123";//数据库登录密码 scsb.InitialCatalog = "Apexbio";//要连接的数据库名 SqlConnection conn = new SqlConnection(scsb.ToString());//实例化连接SQL数据库的类的对象 if (conn.State==ConnectionState.Closed) { conn.Open(); } //拼接T-SQL语句 string strSQL = @"INSERT INTO [dbo].[userinfo] ([id] ,[name] ,[pwd] ,[role]) VALUES ('"+user_id+@"' ,'"+user_name+@"' ,'"+user_pwd+@"' ,'"+user_group+"')"; SqlCommand comm = new SqlCommand(strSQL, conn);//实例化SQLcommand对象 try { comm.ExecuteNonQuery();//执行SQL语句 return "注册成功"; } catch(Exception ex) { return ex.Message; } finally { conn.Close(); } } } }
登录界面设计:
FrmLogin.cs
复制代码
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//登录按钮 private void btnLogin_Click(object sender, EventArgs e) { Login(); } //取消按钮 private void txtCancel_Click(object sender, EventArgs e) { this.Close(); } //按下回车时登录 private void FrmLogin_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Return)//判断用户按下的是否为回车键 { Login(); } } //login方法 private void Login() { //不能为空 if (txtUser.Text == "" || txtPwd.Text == "") { MessageBox.Show("用户名或密码不能为空", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //变量定义 string user_id = txtUser.Text.Trim();//trim去前后空格 string user_pwd = txtPwd.Text;//空格也算做密码的一部分,所以不用去 //密码加密 user_pwd = Commons.EncodeHelper.AES_Encrypt(user_pwd); Userinfo us = new Userinfo(); string message = us.Login(user_id,user_pwd); if (string.IsNullOrEmpty(message)) { this.Close(); FrmMain frm = new FrmMain(); frm.Show(); } else { MessageBox.Show(message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //注册按钮 private void lklRes_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { new FrmRegister().ShowDialog(); /*show和showdialog的区别: *showdialog始终获得焦点,其他窗口不能获得焦点 */ }
注册界面FrmRegister:
复制代码
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//注册 private void btnRes_Click(object sender, EventArgs e) { //变量定义 string user_id = txtUser.Text.Trim(); string user_pwd = txtPwd.Text; string user_name = txtUserName.Text.Trim(); string user_role = string.Empty; //密码加密 user_pwd = Commons.EncodeHelper.AES_Encrypt(user_pwd); //实例化对象 Userinfo us = new Userinfo(); if (rbtnAdmin.Checked) { user_role = "admin"; } else if (rbtnGroup.Checked) { user_role = "group"; } else { user_role = "user"; } string mess = us.Register(user_id,user_pwd,user_name,user_role); MessageBox.Show(mess, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void btnCaancel_Click(object sender, EventArgs e) { this.Dispose(); FrmLogin frm = new FrmLogin(); frm.Show(); }
同时,这篇文章在用户密码部分作了加密处理,引用到Core.Common.dll,如果需要密码加密的话,可以选择跳转链接下载https://download.csdn.net/download/qq_42002500/12923364
最后
以上就是干净万宝路最近收集整理的关于C#winForm连接SQL Server,创建登录窗口,用户注册窗口的全部内容,更多相关C#winForm连接SQL内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复