一、实现环境
C#、window10、vs2019
数据库SQL Server (SQLEXPRESS)
二、界面要求
1 、连接本地数据库
2 、用户名:用户名可以是数字、下划线(不能开头)、字母、汉字。
3 、密码:密码不能小于8或大于16,密码必须包含数字和字母。
4 、用到的命名空间:
using System.Data.SqlClient;
using System.Text.RegularExpressions;
5 、用户名正则代码如下:
^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$
6、 密码正则代码如下:
^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$
三、具体实现
1、界面布局
密码文本框password属性改为 *
checkbox代码实现部分如下:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
2、运行效果
3、登录实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace bookstore_login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader reader = null;
private string connstring = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=loginDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
private void button2_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connstring);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT login_table.* FROM login_table where(name=@name) and (password=@password)";
cmd.Parameters.AddWithValue("@name", textBox1.Text.Trim());
cmd.Parameters.AddWithValue("@password", textBox2.Text.Trim());
reader = cmd.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("登陆成功", "登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("用户名、密码不正确", "登陆失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "open error", MessageBoxButtons.OK);
}
finally
{
if (reader != null)
{
reader.Close();
}
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form_注册 reg = new Form_注册();
reg.ShowDialog();
}
}
}
4、注册实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace bookstore_login
{
public partial class Form_注册 : Form
{
public Form_注册()
{
InitializeComponent();
}
private static int agree = 0;
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader reader = null;
private string connstring = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=loginDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
private int checkname()
{
Regex re = new Regex("^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$");
Regex re_ = new Regex("^(?!_)");
int flag = 1;
if ( textBox2.Text.Length < 6)
{
MessageBox.Show("用户名不能小于6");
flag += 1;
}
else if (textBox2.Text.Length > 12)
{
MessageBox.Show("用户名不能大于12");
flag += 1;
}
else if (!re_.IsMatch(textBox2.Text.ToString()))
{
MessageBox.Show("用户名下划线不能放在开头");
flag += 1;
}
else if (!re.IsMatch(textBox2.Text.ToString()))
{
MessageBox.Show("含有非法字符");
flag += 1;
}
return flag;
}
private int checkpd()
{
Regex re = new Regex("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$");
int flag = 1;
if (textBox3.Text.Length<8)
{
MessageBox.Show("密码不能小于8");
flag += 1;
}
else if (textBox3.Text.Length>16)
{
MessageBox.Show("密码不能大于16");
flag += 1;
}
else if (!re.IsMatch(textBox3.Text.ToString()))
{
MessageBox.Show("密码必须是字母数字的结合");
flag += 1;
}
return flag;
}
private void button1_Click(object sender, EventArgs e)
{
if (checkname()==1&&checkpd()==1)
{
if (agree == 1)
{
try
{
conn = new SqlConnection(connstring);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO login_table (name, password) VALUES (@name, @password)";
cmd.Parameters.AddWithValue("@name", textBox2.Text.Trim());
cmd.Parameters.AddWithValue("@password", textBox3.Text.Trim());
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("用户名不能为空!");
return;
}
if (String.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("密码不能为空!");
return;
}
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
MessageBox.Show("成功注册,请登录", "insert ok", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("注册失败", "insert error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "open error", MessageBoxButtons.OK);
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
this.Close();
}
else
{
MessageBox.Show("请勾选用户协议和隐私声明");
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
agree = 1;
}
}
}
四、遇到的问题及待解决的问题
1、建议使用.Net Framwork框架,如果使用.NET Core框架,导入using System.Data.SqlClient,使用时会报错,需要手动添加System.Data.SqlClient,具体操作见https://blog.csdn.net/sjtuxx_lee/article/details/94338908主要操作内容如下
控制台输入命令如下:
PM> Install-Package System.Data.Common
PM> Install-Package System.Data.SqlClient
2、界面添加图片时,一定要添加到backgroundimage属性,然后再把backgroundimagelayout改为stretch,不要添加到image属性,这样才能完整的显示图片内容。
3、SQL Server(SQLEXPRESS),注册(插入数据)时,用户名使用汉字时,存入数据库后,汉字会出现乱码,没有找到数据库如何设置编码,还是避免使用汉字吧,希望会的大佬可以评论教一下。
4、未实现功能:本想在用户名和密码文本框内加入隐藏提示内容,像下图一样,
通过焦点事件控制提示事件,由于时间、技术原因未能实现该功能,有大佬知道的话可以发个链接。