目录
这是本人用C#窗体做的一个登录程序,如标题所示,它包含了账号登录、注册账号、修改密码、注销账号和实名认证五个功能。对于有一定基础知识的小伙伴来说,应该不算太难,里面有注释说明,可能咋一看感觉代码运行的逻辑有点乱,不过没关系,相信对你会有所帮助。以下是详细的操作过程。
一、配置数据库文件和程序代码
1、由于这个登录程序连接了SQL Server数据库,所以需要配置一下相关数据,创建一个“账号登录”数据库,新建一个“登录注册表”和“实名信息表”,并记住这个服务器名称,等下会用到,如下图所示。
登录注册表设置两个列名:username(账号)和password(密码)以及对应的数据类型(可自定义)。
实名信息表要设置3个列名,分别是:username(账号)、Name(姓名)、IDcard(身份证号)。设置username列名是为了关联这两张表。
SQL查询显示结果,password和userpwd(即username+password)均显示为密文;如果账号未实名,则Name和IDcard两栏没有数据。
2、然后打开VS2019,新建一个窗体程序(我新建的窗体是CT12),在里面设置一下App.config的SQL数据库连接信息,如下图所示。
App.config里面的代码格式:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<connectionStrings>
<add name="sqlserver" connectionString="server=SQLEXPRESS;database=账号登录;integrated security = true;"/>
//这是以windows身份验证登录连接,SQLEXPRESS是服务器名称,“账号登录”是新建的数据库名称.
</connectionStrings>
</configuration>
二、配置C#窗体控件布局和源代码
该登录程序包含了6个窗体,分别为:Form1——登录界面、Form2——注册界面、Form3——主界面、Form4——修改密码界面、Form5——账号注销界面、Form6——实名认证界面。
注意:由于连接了SQL数据库,这5个窗体之间数据相互传递、相互关联,如果只截取一段窗体代码运行,则会直接报错。
1、窗体Form1:账号登录界面
label1——账号,label2——密码,
textBox1——对应账号输入,textBox2——对应密码输入,
button1——登录,button2——重置,
button3——退出,button4——注册。
checkBox1——是否显示密码
窗体Form1的控件布局:
窗体Form1源代码
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Text;
using System.Diagnostics;
namespace CT12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "账号登录";
ScreenSize();
textBox2.PasswordChar = '*';//密码显示为"*"
}
public void ScreenSize() //设置窗体样式和居中显示
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.MaximumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Form1_Load(object sender, EventArgs e)
{
ScreenSize(); //调用函数
this.ActiveControl = textBox1; //焦点在控件textBox1上面
}
private void Button4_Click(object sender, EventArgs e) //跳转注册界面
{
this.Hide();
Form2 sd = new Form2();
sd.Show();
}
private void Button3_Click(object sender, EventArgs e) //退出程序
{
Process.GetCurrentProcess().Kill();
}
private void Button2_Click(object sender, EventArgs e) //重置登录信息
{
textBox1.Text = "";
textBox2.Text = "";
this.ActiveControl = textBox1;
}
public string MD5Encrypt(string rawPass, string salt) //MD5加盐加密
{
MD5 md5 = MD5.Create();
byte[] bs = Encoding.UTF8.GetBytes(rawPass + salt);
byte[] ts = md5.ComputeHash(bs);
StringBuilder std = new StringBuilder();
foreach (byte i in ts)
{
std.Append(i.ToString("x2"));
}
return std.ToString(); //返回密文
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //创建数据库连接
public static string username; //设置Form1的一个参数,登录成功后将显示在主界面Form3
public static string password;
private void Button1_Click(object sender, EventArgs e) //登录代码
{
SqlConnection conn = new SqlConnection(Stu); //创建数据库游标对象conn
conn.Open(); //打开数据库
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from 登录注册表 where username='" + textBox1.Text.Trim() + "'";
SqlDataReader sdt = cmd.ExecuteReader();
if (textBox1.Text == "")
{
MessageBox.Show("账号不得为空!", "提示");
this.ActiveControl = textBox1;
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不得为空!", "提示");
this.ActiveControl = textBox2;
}
else if (sdt.Read() == true) //读取到记录
{
string result = sdt.GetString(sdt.GetOrdinal("password"));
if (MD5Encrypt(textBox2.Text.Trim(), textBox1.Text.Trim()) == result)
{
MessageBox.Show("登录成功!", "提示");
username = textBox1.Text.Trim(); //将textBox1的值赋给参数username
password = textBox2.Text.Trim(); //将textBox2的值赋给参数password
conn.Close(); //关闭数据库连接
this.Hide(); //隐藏当前窗口
Form3 sd = new Form3();
sd.Show(); //显示主界面Form3
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox2.Text = "";
this.ActiveControl = textBox2;
}
}
else
{
DialogResult dr = MessageBox.Show("账号不存在,是否选择注册一个新账号?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr == DialogResult.OK)
{
this.Hide();
Form2 er = new Form2();
er.Show();
conn.Close();
}
else
{
textBox1.Text = "";
textBox2.Text = "";
this.ActiveControl = textBox1;
}
}
conn.Close(); //关闭游标对象连接
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e) //是否显示密码输入为“*”
{
if (checkBox1.Checked)
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
protected override void OnClosing(CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
窗体Form1运行效果截图
2、窗体Form2:账号注册界面
label1——新用户名,label2——用户密码,label3——确认密码
textBox1(对应新用户名),textBox2(对应用户密码),textBox3(对应确认密码)
button1——确认,button2——重置,button3——返回
窗体Form2的控件布局
窗体Form2源代码
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace CT12
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.Text = "账号注册";
ScreenSize();
}
private void Form2_Load(object sender, EventArgs e)
{
ScreenSize();
textBox2.PasswordChar = '*'; //textBox2——用户密码显示为"*"
textBox3.PasswordChar = '*'; //textBox3——确认密码显示为"*"
this.ActiveControl = textBox1; //焦点在输入框textBox1上
}
public void ScreenSize() //设置窗体大小尺寸和居中显示
{
int x = Screen.GetBounds(this).Width; //获取屏幕宽度
int y = Screen.GetBounds(this).Height; //获取屏幕高度
this.MinimumSize = new Size(x / 2, y / 2); //设置窗口最小尺寸
this.MaximumSize = new Size(x / 2, y / 2); //设置窗口最大尺寸
this.CenterToParent();
this.CenterToScreen();
}
private void Button2_Click(object sender, EventArgs e) //重置输入框信息
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox1;
}
private void Button3_Click(object sender, EventArgs e) //返回登录界面Form1
{
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
public string MD5Encrypt(string rawPass, string salt) //MD5加盐加密操作
{
MD5 md5 = MD5.Create();
byte[] bs = Encoding.UTF8.GetBytes(rawPass + salt);
byte[] hs = md5.ComputeHash(bs);
StringBuilder stb = new StringBuilder();
foreach (byte b in hs)
{
stb.Append(b.ToString("x2"));
}
return stb.ToString();
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
private void Button1_Click(object sender, EventArgs e)
{
//创建数据库对象连接
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
string sql = "Select username from 登录注册表 where username='" + textBox1.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sql, conn); //判断账号是否已存在
cmd.CommandType = CommandType.Text;
SqlDataReader sdt = cmd.ExecuteReader();
if (textBox1.Text == "")
{
MessageBox.Show("注册账号不得为空!", "提示");
}
else if (textBox2.Text == "")
{
MessageBox.Show("注册账号密码不得为空!", "提示");
}
else if (textBox3.Text == "" || textBox2.Text != textBox3.Text)
{
MessageBox.Show("请重新确认密码", "提示");
textBox2.Text = "";
textBox3.Text = "";
}
/*else if(textBox1.Text.Length<8)
{
MessageBox.Show("请输入8位以上的新用户名!","提示");
textBox1.Text = "";
}
else if (Regex.IsMatch(textBox2.Text.ToString(),@"^\w+_$") == false)
{
MessageBox.Show("请输入8~16位由字母、数字和下划线组成的密码!");
textBox2.Text = "";
textBox3.Text = "";
}
else if(textBox2.Text.Length<8||textBox2.Text.Length>16)
{
MessageBox.Show("请输入8~16位由字母、数字和下划线组成的密码!");
textBox2.Text = "";
textBox3.Text = "";
}*/
else
{
if (sdt.Read())
{
MessageBox.Show("该账号已注册,请重新输入新账号!", "提示");
textBox1.Text = "";
}
else
{
//插入的信息分别为账号,密文,账号+密文
Insert(textBox1.Text.Trim(), MD5Encrypt(textBox2.Text.Trim(), textBox1.Text.Trim()));
//调用函数Insert()保存注册信息到数据库
conn.Close();
this.Hide();
Form1 df = new Form1();
df.Show();
}
}
conn.Close();
}
public void Insert(string txt1, string txt2) //数据库插入注册成功后的账号信息
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
try
{
string InsertStr = string.Format("insert into 登录注册表 values('{0}','{1}')", txt1, txt2);
SqlCommand comm = conn.CreateCommand();
comm.CommandText = InsertStr;
SqlCommand mycom = new SqlCommand(InsertStr, conn);
mycom.ExecuteNonQuery();
MessageBox.Show("账号注册成功!", "提示");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}
conn.Close();
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e) //textBox2文本框输入显示为"*"
{
if (checkBox1.Checked)
textBox2.PasswordChar = '\0';
else
textBox2.PasswordChar = '*';
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e) //textBox3文本框输入为"*"
{
if (checkBox2.Checked)
textBox3.PasswordChar = '\0';
else
textBox3.PasswordChar = '*';
}
protected override void OnClosing(CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
窗体Form2运行效果截图
3、窗体Form3:主界面
checkBox1——当前账号,Label1——显示账号
textBox1——显示姓名,textBox2——显示身份证号
Button1——修改密码,Button2——注销账号,Button5——实名认证,
Button3——返回,Button4——退出
窗体Form3的控件布局
窗体Form3的源代码
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT12
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.Text = "账号信息";
ScreenSize();
}
public void ScreenSize()
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Form3_Load(object sender, EventArgs e)
{
ScreenSize();
ShowUser(Form1.username);
this.HideDcard(Form1.username);
ShowName();
}
public void ShowName()
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name,IDcard from 实名信息表 where username='" + Form1.username + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() == true)
{
this.textBox2.Text = reader.GetString(reader.GetOrdinal("Name"));
//实名认证成功后,主界面Form3默认显示姓名
}
else
{
this.textBox2.Text = "";
}
conn.Close();
}
private void Button1_Click(object sender, EventArgs e) //跳转窗体Form4:修改密码界面
{
this.Hide();
Form4 df = new Form4();
df.Show();
}
private void Button2_Click(object sender, EventArgs e) //跳转窗体Form5:注销账号界面
{
this.Hide();
Form5 sd = new Form5();
sd.Show();
}
private void Button4_Click(object sender, EventArgs e) //强制退出所有窗口程序和线程
{
Process.GetCurrentProcess().Kill();
}
private void Button3_Click(object sender, EventArgs e) //返回窗体Form1:登录界面
{
DialogResult dr = MessageBox.Show("是否返回登录界面?", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
this.Hide();
Form1 sdd = new Form1();
sdd.Show();
}
}
private void Button5_Click_1(object sender, EventArgs e)
{
this.Hide();
Form6 asd = new Form6();
asd.Show();
}
public void ShowUser(string text)
{
char StarChar = '*';
string value = text;
int startlen = 1, endlen = 1;
int lenth = value.Length - startlen - endlen;
string str1 = value.Substring(startlen, lenth);
string str2 = string.Empty;
for (int i = 0; i < str1.Length; i++)
{
str2 += StarChar;
}
label1.Text = value.Replace(str1, str2);
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
private void CheckBox1_CheckedChanged(object sender, EventArgs e) //是否显示账号
{
if (checkBox1.Checked)
{
label1.Text = Form1.username;
}
else
{
ShowUser(Form1.username); //调用方法显示*号
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e) //是否显示身份证号
{
SqlConnection con = new SqlConnection(Stu);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select Name,IDcard from 实名信息表 where username='" + Form1.username + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() == true)
{
if (checkBox2.Checked)
{
textBox2.Text = reader.GetString(reader.GetOrdinal("Name"));
ShowIDcard(Form1.username); //调用方法函数,显示完整身份证号
}
else
{
HideDcard(Form1.username); //显示加密后的身份证号
}
}
con.Close();
}
public void ShowIDcard(string text) //显示完整的实名信息
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name,IDcard from 实名信息表 where username='" + text + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() == true)
{
textBox3.Text = reader.GetString(reader.GetOrdinal("IDcard"));
}
conn.Close();
}
public void HideDcard(string text) //用'*'号加密身份证号
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name,IDcard from 实名信息表 where username='" + text + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() == true)
{
string sdd = "*";
string IDcard = reader.GetString(reader.GetOrdinal("IDcard"));
for (int i = 0; i < IDcard.Length - 3; i++)
{
sdd += "*";
}
textBox3.Text = IDcard.Substring(0, 1) + sdd + IDcard.Substring(IDcard.Length - 2, 2);
}
else
{
textBox3.Text = "";
}
conn.Close();
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
protected override void OnClosing(CancelEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
窗体Form3界面效果截图
4、窗体Form4:修改密码界面
label2——旧密码,label3——新密码
textBox2(对应旧密码),textBox3(对应新密码)
Button1——确认,Button2——重置,Button3——取消
窗体Form4的控件布局
窗体Form4的源代码
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace CT12
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
this.Text = "修改密码";
ScreenSize();
}
public void ScreenSize()
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Form4_Load(object sender, EventArgs e)
{
ScreenSize();
textBox2.PasswordChar = '*';
textBox3.PasswordChar = '*';
this.ActiveControl = textBox2;
}
private void Button3_Click(object sender, EventArgs e) //返回窗体Form3:主界面
{
this.Hide();
Form3 df = new Form3();
df.Show();
}
private void Button2_Click(object sender, EventArgs e) //重置textBox文本框的输入信息
{
this.ActiveControl = textBox2;
textBox2.Text = "";
textBox3.Text = "";
}
public string MD5Encrypt(string rawPass, string salt) //MD5加盐加密
{
MD5 md5 = MD5.Create();
byte[] bs = Encoding.UTF8.GetBytes(rawPass + salt);
byte[] ts = md5.ComputeHash(bs);
StringBuilder std = new StringBuilder();
foreach (byte i in ts)
{
std.Append(i.ToString("x2"));
}
return std.ToString(); //返回密文
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //创建数据库连接
private void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
string sql = "Select * from 登录注册表 where username='" + Form1.username.Trim() + "'";
SqlCommand cmd = new SqlCommand(sql, conn); //调用登录窗体Form1参数username,判断当前账号密码是否正确
SqlDataReader reader = cmd.ExecuteReader();
if (textBox2.Text == "")
{
MessageBox.Show("账号密码不得为空!", "提示");
this.ActiveControl = textBox2;
}
else if (textBox3.Text == "")
{
MessageBox.Show("请输入新密码!", "提示");
this.ActiveControl = textBox3;
}
else if (reader.Read())
{
string result = reader.GetString(reader.GetOrdinal("password"));
if (MD5Encrypt(textBox2.Text.Trim(), Form1.username) == result)
{
Change(Form1.username, MD5Encrypt(textBox3.Text, Form1.username));
//调用Change()函数方法保存修改后的账号信息到数据库
this.Hide();
Form1 sd = new Form1();
sd.Show();
conn.Close();
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox2;
}
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox2;
}
conn.Close();
}
public void Change(string txt1, string txt2) //数据库保存修改后的账号信息
{//txt1为当前账号,txt2为加密后的密文,txt3为当前账号+密文
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
try
{
string InsertStr = string.Format("update 登录注册表 set password='{0}' WHERE username='{1}';", txt2.ToString(), txt1.ToString());
SqlCommand comm = conn.CreateCommand();
SqlCommand mycom = new SqlCommand(InsertStr, conn);
mycom.ExecuteNonQuery();
conn.Close();
MessageBox.Show("账号密码修改成功!", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
textBox2.Text = "";
textBox3.Text = "";
}
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e) //textBox2文本框输入是否显示为"*"
{
if (checkBox1.Checked)
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e) // //textBox3文本框输入是否显示为"*"
{
if (checkBox1.Checked)
{
textBox3.PasswordChar = '\0';
}
else
{
textBox3.PasswordChar = '*';
}
}
private void Form4_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
窗体Form4运行效果截图
5、窗体Form5:账号注销界面
label2——账号密码,textBox2——对应当前账号密码,
button1——注销,button2——重置,button3——取消
窗体Form5的控件布局
窗体Form5的源代码
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace CT12
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
this.Text = "账号注销";
ScreenSize();
}
public void ScreenSize()
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Form5_Load(object sender, EventArgs e)
{
ScreenSize();
this.ActiveControl = textBox2;
}
private void Button3_Click(object sender, EventArgs e) //返回窗体Form3:主界面
{
this.Hide();
Form3 sd = new Form3();
sd.Show();
}
private void Button2_Click(object sender, EventArgs e) //重置textBox文本框的信息
{
textBox2.Text = "";
this.ActiveControl = textBox2;
}
public string MD5Encrypt(string rawPass, string salt) //MD5加盐加密
{
MD5 md5 = MD5.Create();
byte[] bs = Encoding.UTF8.GetBytes(rawPass + salt);
byte[] ts = md5.ComputeHash(bs);
StringBuilder std = new StringBuilder();
foreach (byte i in ts)
{
std.Append(i.ToString("x2"));
}
return std.ToString(); //返回密文
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
private void Button1_Click(object sender, EventArgs e) //账号注销功能
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
string sql = "Select * from 登录注册表 where username='" + Form1.username + "'";
SqlCommand cmd = new SqlCommand(sql, conn); //判断当前账号密码是否正确
cmd.CommandType = CommandType.Text;
SqlDataReader reader = cmd.ExecuteReader();
if (textBox2.Text == "")
{
MessageBox.Show("账号密码不得为空!", "提示");
}
else if (reader.Read() == true)
{
string result = reader.GetString(reader.GetOrdinal("password"));
if (MD5Encrypt(textBox2.Text.Trim(), Form1.username) == result)
{
MessageBox.Show("账号注销成功!", "提示");
Delete(); //调用方法,删除注销后的账号信息
conn.Close(); //关闭数据库对象连接
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox2.Text = "";
this.ActiveControl = textBox2;
}
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox2.Text = "";
this.ActiveControl = textBox2;
}
conn.Close();
}
public void Delete() //数据库删除注销后的账号信息
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
try
{
string sql2 = "delete from 登录注册表 where username='" + Form1.username + "'";
SqlCommand cmd1 = new SqlCommand(sql2, conn);
cmd1.ExecuteNonQuery();
conn.Close();
Delete2();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Delete2() //删除注销账号的实名信息
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
try
{
string sql1 = "delete from 实名信息表 where username='" + Form1.username + "'";
SqlCommand cmd = new SqlCommand(sql1, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form5_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
6、窗体Form6:实名认证界面
label1——姓名,label2——身份证号
textBox1——对应姓名,textBox2——对应身份证号
button1——绑定,button2——重置,button3——返回
窗体Form6的控件布局
窗体Form6源代码
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace CT12
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
this.Text = "实名认证";
ScreenSize();
}
private void Form6_Load(object sender, EventArgs e)
{
ScreenSize();
}
public void ScreenSize()
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Button3_Click(object sender, EventArgs e)
{
this.Hide();
Form3 sd = new Form3();
sd.Show();
}
private void Button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
public string MD5Encrypt(string rawPass, string salt) //MD5加盐加密
{
MD5 md5 = MD5.Create();
byte[] bs = Encoding.UTF8.GetBytes(rawPass + salt);
byte[] ts = md5.ComputeHash(bs);
StringBuilder std = new StringBuilder();
foreach (byte i in ts)
{
std.Append(i.ToString("x2"));
}
return std.ToString(); //返回密文
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
public static string name; //创建参数name,实名认证成功后显示在主界面Form3
public static string IDcard; //创建参数IDcard,实名认证成功后显示在主界面Form3
private void Button1_Click(object sender, EventArgs e) //绑定操作
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
string sql1 = "Select IDcard from 实名信息表 where IDcard='" + textBox2.Text + "'";
SqlCommand cmd = new SqlCommand(sql1, conn);
cmd.CommandType = CommandType.Text;
SqlDataReader stt;
stt = cmd.ExecuteReader();
if (textBox1.Text == "")
{
MessageBox.Show("提示", "真实姓名不得为空!");
this.ActiveControl = textBox1;
}
else if (textBox2.Text == "")
{
MessageBox.Show("身份证号码不得为空!", "提示");
this.ActiveControl = textBox2;
}
/*else if (Regex.IsMatch(textBox1.Text.ToString(),@"[\u4E00-\u9FA5]+$") == false)
{
MessageBox.Show("请输入真实有效的姓名!","提示");
textBox1.Text = "";
this.ActiveControl = textBox1;
}
else if (Regex.IsMatch(textBox2.Text.ToString(),@"^\d+$") == false && textBox2.Text.Length != 18)
{
MessageBox.Show("请输入真实有效的身份证号码!","提示");
textBox2.Text = "";
this.ActiveControl = textBox2;
}*/
else
{
if (stt.Read() == true) //读取到一条记录就返回
{
MessageBox.Show("该身份证号已绑定其他账号,请重新绑定!", "提示");
textBox1.Text = "";
textBox2.Text = "";
}
else
{
Verfy(); //调用函数判断当前账号是否已经实名认证
conn.Close();
}
}
conn.Close();
}
public void Verfy() //判断账号是否已经实名认证
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
string sql2 = "Select Name,IDcard from 实名信息表 where username='" + Form1.username + "'";
SqlCommand cmd1 = new SqlCommand(sql2, conn);
cmd1.CommandType = CommandType.Text;
SqlDataReader sdt;
sdt = cmd1.ExecuteReader();
if (sdt.Read() == true)
{
MessageBox.Show("该账号已实名认证!", "提示");
textBox1.Text = "";
textBox2.Text = "";
}
else
{
Insert(Form1.username, textBox1.Text, textBox2.Text); //调用方法Insert()
name = textBox1.Text;
IDcard = textBox2.Text;
conn.Close();
this.Hide();
Form3 sw = new Form3();
sw.Show();
}
conn.Close();
}
public void Insert(string txt1, string txt2, string txt3) //保存实名后的信息到数据库
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
try
{
MessageBox.Show("实名认证成功!", "提示");
string InsertStr = string.Format("insert into 实名信息表 values('{0}','{1}','{2}')", txt2, txt3);
SqlCommand mycom = new SqlCommand(InsertStr, conn);
mycom.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form6_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
Form6运行效果截图
总结
以上就是今天要跟大家分享的内容,可能代码有点多,代码逻辑看起来比较乱,花了几个星期时间写的,期间修修改改了很多次,尚且还有不足之处,仅供学习和参考;感兴趣的小伙伴可以来看看,希望对你有所帮助,也欢迎大家的批评指正。
本人声明:未经本人许可,禁止转载!!!如果要引用部分代码,请标明出处!!