這是本人用Visual Studio2019做的一個(gè)C#窗體登錄程序,如標(biāo)題所示,它包含了賬號(hào)登錄、注冊(cè)賬號(hào)、修改密碼、注銷(xiāo)賬號(hào)和實(shí)名認(rèn)證五個(gè)功能。對(duì)于有一定基礎(chǔ)知識(shí)的小伙伴來(lái)說(shuō),應(yīng)該不算太難,里面有注釋說(shuō)明,可能咋一看感覺(jué)代碼運(yùn)行的邏輯有點(diǎn)亂,不過(guò)沒(méi)關(guān)系,相信對(duì)你會(huì)有所幫助。以下是詳細(xì)的操作過(guò)程。
一、配置數(shù)據(jù)庫(kù)文件和程序代碼
1、由于這個(gè)登錄程序連接了SQL Server數(shù)據(jù)庫(kù),所以需要配置一下相關(guān)數(shù)據(jù),創(chuàng)建一個(gè)“賬號(hào)登錄”數(shù)據(jù)庫(kù),新建一個(gè)“登錄注冊(cè)表”和“實(shí)名信息表”,并記住這個(gè)服務(wù)器名稱(chēng),等下會(huì)用到,如下圖所示。
登錄注冊(cè)表設(shè)置兩個(gè)列名:username(賬號(hào))和password(密碼)以及對(duì)應(yīng)的數(shù)據(jù)類(lèi)型(可自定義)。
實(shí)名信息表要設(shè)置3個(gè)列名,分別是:username(賬號(hào))、Name(姓名)、IDcard(身份證號(hào))。設(shè)置username列名是為了關(guān)聯(lián)這兩張表。
SQL查詢(xún)顯示結(jié)果,password和userpwd(即username+password)均顯示為密文;如果賬號(hào)未實(shí)名,則Name和IDcard兩欄沒(méi)有數(shù)據(jù)。
2、然后打開(kāi)VS2019,新建一個(gè)窗體程序(我新建的窗體是CT12),在里面設(shè)置一下App.config的SQL數(shù)據(jù)庫(kù)連接信息,如下圖所示。
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=賬號(hào)登錄;integrated security = true;"/>
//這是以windows身份驗(yàn)證登錄連接,SQLEXPRESS是服務(wù)器名稱(chēng),“賬號(hào)登錄”是新建的數(shù)據(jù)庫(kù)名稱(chēng).
</connectionStrings>
</configuration>
二、配置C#窗體控件布局和源代碼
該登錄程序包含了6個(gè)窗體,分別為:Form1——登錄界面、Form2——注冊(cè)界面、Form3——主界面、Form4——修改密碼界面、Form5——賬號(hào)注銷(xiāo)界面、Form6——實(shí)名認(rèn)證界面。
這里還要新創(chuàng)建一個(gè)公共類(lèi)Encrypt1.cs,在這些窗體中都會(huì)調(diào)用到。
using System;
using System.Security.Cryptography;
using System.Text;
namespace CT12
{
class Encrypt1
{
public string EncryptPassword(string userpwd)
{
byte[] saltBytes = Encoding.UTF8.GetBytes(userpwd); // 自定義鹽值,用于固定加密結(jié)果
byte[] passwordBytes = Encoding.UTF8.GetBytes(userpwd + Convert.ToBase64String(saltBytes));
byte[] hashedBytes;
using (SHA256 sha256 = SHA256.Create())
{
hashedBytes = sha256.ComputeHash(passwordBytes);
}
string hashedPassword = Convert.ToBase64String(hashedBytes);
int length = hashedPassword.Length;
if (length % 2 == 0) // 如果加密后的密碼長(zhǎng)度為偶數(shù),則(加密后的密碼長(zhǎng)度/2)并輸出
{
hashedPassword = hashedPassword.Substring(0, length / 2);
}
else // 如果為奇數(shù),則(加密后的密碼-1)/2并輸出
{
hashedPassword = hashedPassword.Substring(0, (length - 1) / 2);
}
return hashedPassword;
}
}
}
注意:由于連接了SQL數(shù)據(jù)庫(kù),這5個(gè)窗體之間數(shù)據(jù)相互傳遞、相互關(guān)聯(lián),如果只截取一段窗體代碼運(yùn)行,則會(huì)直接報(bào)錯(cuò)。
1、窗體Form1:賬號(hào)登錄界面
label1——賬號(hào),label2——密碼,
textBox1——對(duì)應(yīng)賬號(hào)輸入,textBox2——對(duì)應(yīng)密碼輸入,
button1——登錄,button2——重置,
button3——退出,button4——注冊(cè)。
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 = "賬號(hào)登錄";
ScreenSize();
textBox2.PasswordChar = '*';//密碼顯示為"*"
}
public void ScreenSize() //設(shè)置窗體樣式和居中顯示
{
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(); //調(diào)用函數(shù)
this.ActiveControl = textBox1; //焦點(diǎn)在控件textBox1上面
}
private void Button4_Click(object sender, EventArgs e) //跳轉(zhuǎn)注冊(cè)界面
{
this.Hide();
Form2 sd = new Form2();
sd.Show();
}
private void Button2_Click(object sender, EventArgs e) //重置登錄信息
{
textBox1.Text = "";
textBox2.Text = "";
this.ActiveControl = textBox1;
}
private void button3_Click(object sender, EventArgs e)//強(qiáng)制退出窗體程序
{
Process.GetCurrentProcess().Kill();
}
private static readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //創(chuàng)建數(shù)據(jù)庫(kù)連接
public static string username; //設(shè)置Form1的一個(gè)參數(shù),登錄成功后將顯示在主界面Form3
public static string password;
private void Button1_Click(object sender, EventArgs e) //登錄代碼
{
Encrypt1 SHA256 = new Encrypt1();//調(diào)用該加密類(lèi)
if (textBox1.Text.Equals(""))
{
MessageBox.Show("賬號(hào)不得為空!", "提示");
this.ActiveControl = textBox1;
}
else if (textBox2.Text.Equals(""))
{
MessageBox.Show("密碼不得為空!", "提示");
this.ActiveControl = textBox2;
}
else
{
using (SqlConnection conn = new SqlConnection(Stu))
{
conn.Open();
string sql = "select count(*) from 登錄注冊(cè)表 where username=@username";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", textBox1.Text.Trim());
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count <= 0)
{
DialogResult dr = MessageBox.Show("賬號(hào)不存在,是否選擇注冊(cè)一個(gè)新賬號(hào)?", "提示", 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;
}
return;
}
// 驗(yàn)證用戶(hù)密碼
sql = "SELECT COUNT(1) FROM 登錄注冊(cè)表 WHERE username=@username AND password=@password";
cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", textBox1.Text.Trim());
cmd.Parameters.AddWithValue("@password", SHA256.EncryptPassword(textBox1.Text.Trim()+textBox2.Text.Trim()));
count = Convert.ToInt32(cmd.ExecuteScalar());
if (count <= 0)
{
MessageBox.Show("賬號(hào)密碼錯(cuò)誤!");
textBox2.Text = "";
return;
}
// 賬號(hào)登錄成功
MessageBox.Show("登錄成功!");
username = textBox1.Text.Trim(); //將textBox1的值賦給參數(shù)username
password = textBox2.Text.Trim(); //將textBox2的值賦給參數(shù)password
conn.Close();
this.Hide();
Form3 sd = new Form3();
sd.Show(); //顯示主界面Form3
}
}
}
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運(yùn)行效果截圖
2、窗體Form2:賬號(hào)注冊(cè)界面
label1——新用戶(hù)名,label2——用戶(hù)密碼,label3——確認(rèn)密碼
textBox1(對(duì)應(yīng)新用戶(hù)名),textBox2(對(duì)應(yīng)用戶(hù)密碼),textBox3(對(duì)應(yīng)確認(rèn)密碼)
button1——確認(rèn),button2——重置,button3——返回
窗體Form2的控件布局
窗體Form2源代碼
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
this.Text = "賬號(hào)注冊(cè)";
ScreenSize();
}
private void Form2_Load(object sender, EventArgs e)
{
ScreenSize();
textBox2.PasswordChar = '*'; //textBox2——用戶(hù)密碼顯示為"*"
textBox3.PasswordChar = '*'; //textBox3——確認(rèn)密碼顯示為"*"
this.ActiveControl = textBox1; //焦點(diǎn)在輸入框textBox1上
}
private void ScreenSize() //設(shè)置窗體大小尺寸和居中顯示
{
int x = Screen.GetBounds(this).Width; //獲取屏幕寬度
int y = Screen.GetBounds(this).Height; //獲取屏幕高度
this.MinimumSize = new Size(x / 2, y / 2); //設(shè)置窗口最小尺寸
this.MaximumSize = new Size(x / 2, y / 2); //設(shè)置窗口最大尺寸
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();
}
readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
private void Button1_Click(object sender, EventArgs e)
{
Encrypt1 SHA256 = new Encrypt1();
if (textBox1.Text.Equals(""))
{
MessageBox.Show("注冊(cè)賬號(hào)不得為空!", "提示");
}
else if (textBox2.Text.Equals(""))
{
MessageBox.Show("注冊(cè)賬號(hào)密碼不得為空!", "提示");
}
else if (textBox3.Text.Equals("") || !textBox2.Text.Equals(textBox3.Text))
{
MessageBox.Show("請(qǐng)重新確認(rèn)密碼", "提示");
textBox2.Text = "";
textBox3.Text = "";
}
/*else if(textBox1.Text.Length<8)
{
MessageBox.Show("請(qǐng)輸入8位以上的新用戶(hù)名!", "提示");
textBox1.Text = "";
}
else if (Regex.IsMatch(textBox2.Text.ToString(), @"^\w+_$") == false&&(textBox2.Text.Length<8||textBox2.Text.Length>16))
{
MessageBox.Show("請(qǐng)輸入8~16位由字母、數(shù)字和下劃線(xiàn)組成的密碼!");
textBox2.Text = "";
textBox3.Text = "";
}*/
else
{
using (SqlConnection conn = new SqlConnection(Stu))
{
conn.Open();
SqlCommand command = new SqlCommand
{
Connection = conn,
CommandText = "SELECT COUNT(*) FROM 登錄注冊(cè)表 WHERE username = @username"
};
command.Parameters.AddWithValue("@username", textBox1.Text.Trim());
int count = (int)command.ExecuteScalar();
if (count > 0)
{
MessageBox.Show("賬號(hào)已注冊(cè)!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox1;
return;
}
conn.Close();
}
using (SqlConnection connection = new SqlConnection(Stu))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand
{
Connection = connection,
CommandText = "INSERT INTO 登錄注冊(cè)表 (username, password) VALUES (@username, @password)"
};
command.Parameters.AddWithValue("@username", textBox1.Text);
command.Parameters.AddWithValue("@password", SHA256.EncryptPassword(textBox1.Text.Trim()+textBox2.Text.Trim()));
command.ExecuteNonQuery();
MessageBox.Show("賬號(hào)注冊(cè)成功!");
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
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運(yùn)行效果截圖
3、窗體Form3:主界面
checkBox1——當(dāng)前賬號(hào),Label1——顯示賬號(hào)
textBox1——顯示姓名,textBox2——顯示身份證號(hào)
Button1——修改密碼,Button2——注銷(xiāo)賬號(hào),Button5——實(shí)名認(rèn)證,
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 = "賬號(hào)信息";
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.MaximumSize = 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()//在主界面顯示實(shí)名信息
{
using (SqlConnection conn = new SqlConnection(Stu))
{
// 打開(kāi)連接
conn.Open();
// 查詢(xún)?cè)撡~戶(hù)是否實(shí)名認(rèn)證
string query = "SELECT Name, IDcard FROM 實(shí)名信息表 WHERE username = @username ";
using (SqlCommand command = new SqlCommand(query, conn))
{
command.Parameters.AddWithValue("@username", Form1.username);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
// 如果已經(jīng)實(shí)名認(rèn)證,則顯示姓名
textBox2.Text = reader["Name"].ToString();
}
else
{
textBox2.Text = "";
}
reader.Close();
}
}
}
private void Button1_Click(object sender, EventArgs e) //跳轉(zhuǎn)窗體Form4:修改密碼界面
{
this.Hide();
Form4 df = new Form4();
df.Show();
}
private void Button2_Click(object sender, EventArgs e) //跳轉(zhuǎn)窗體Form5:注銷(xiāo)賬號(hào)界面
{
this.Hide();
Form5 sd = new Form5();
sd.Show();
}
private void Button4_Click(object sender, EventArgs e) //強(qiáng)制退出所有窗口程序和線(xiàn)程
{
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);
}
public static readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
private void CheckBox1_CheckedChanged(object sender, EventArgs e) //是否顯示賬號(hào)
{
if (checkBox1.Checked)
{
label1.Text = Form1.username;
}
else
{
ShowUser(Form1.username); //調(diào)用方法顯示*號(hào)
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e) //是否顯示身份證號(hào)
{
SqlConnection con = new SqlConnection(Stu);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select Name,IDcard from 實(shí)名信息表 where username='" + Form1.username + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
if (checkBox2.Checked)
{
textBox2.Text = reader.GetString(reader.GetOrdinal("Name"));
ShowIDcard(Form1.username); //調(diào)用方法函數(shù),顯示完整身份證號(hào)
}
else
{
HideDcard(Form1.username); //顯示加密后的身份證號(hào)
}
}
con.Close();
}
public void ShowIDcard(string text) //顯示完整的實(shí)名信息
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name,IDcard from 實(shí)名信息表 where username='" + text + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() )
{
textBox3.Text = reader.GetString(reader.GetOrdinal("IDcard"));
}
conn.Close();
}
public void HideDcard(string text) //用'*'號(hào)加密身份證號(hào)
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name,IDcard from 實(shí)名信息表 where username='" + text + "'";
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
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(對(duì)應(yīng)舊密碼),textBox3(對(duì)應(yīng)新密碼)
Button1——確認(rèn),Button2——重置,Button3——取消
窗體Form4的控件布局
窗體Form4的源代碼
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT12
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
this.Text = "修改密碼";
ScreenSize();
}
private 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 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 = "";
}
private readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //創(chuàng)建數(shù)據(jù)庫(kù)連接
private void Button1_Click(object sender, EventArgs e)
{
Encrypt1 SHA256 = new Encrypt1();
string oldPassword = textBox2.Text.Trim();//舊密碼
string newPassword = textBox3.Text.Trim();//新密碼
using (SqlConnection conn = new SqlConnection(Stu))
{
conn.Open();
string sql = "SELECT password FROM 登錄注冊(cè)表 WHERE username=@username";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@username", Form1.username);
object result = cmd.ExecuteScalar();
// 判斷密碼是否正確
if (result != null && result.ToString() == SHA256.EncryptPassword(Form1.username + oldPassword))
{
// 判斷新舊密碼是否相同
if (oldPassword != newPassword)
{
// 更新密碼
sql = "update 登錄注冊(cè)表 set password=@password WHERE username=@username";
using (SqlCommand updateCmd = new SqlCommand(sql, conn))
{
updateCmd.Parameters.AddWithValue("@password", SHA256.EncryptPassword(Form1.username + newPassword));
updateCmd.Parameters.AddWithValue("@username", Form1.username);
int rowsAffected = updateCmd.ExecuteNonQuery();
// 提示密碼修改成功
MessageBox.Show("賬號(hào)密碼修改成功!請(qǐng)重新登錄!");
this.Hide();
Form1 sd = new Form1();
sd.Show();
conn.Close();
}
}
else
{
MessageBox.Show("新舊密碼不得相同!");
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox2;
}
}
else
{
MessageBox.Show("賬號(hào)密碼錯(cuò)誤!");
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox2;
}
}
}
}
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運(yùn)行效果截圖
5、窗體Form5:賬號(hào)注銷(xiāo)界面
label2——賬號(hào)密碼,textBox2——對(duì)應(yīng)當(dāng)前賬號(hào)密碼,
button1——注銷(xiāo),button2——重置,button3——取消
窗體Form5的控件布局
窗體Form5的源代碼
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT12
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
this.Text = "賬號(hào)注銷(xiāo)";
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.MaximumSize = 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;
}
private readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
private void Button1_Click(object sender, EventArgs e) //賬號(hào)注銷(xiāo)功能
{
Encrypt1 SHA256 = new Encrypt1();
using (SqlConnection conn = new SqlConnection(Stu))
{
conn.Open();
SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM 登錄注冊(cè)表 WHERE username=@username AND password=@password", conn);
command.Parameters.AddWithValue("@username", Form1.username); // 賬號(hào)使用 Form1 中的 username
command.Parameters.AddWithValue("@password", SHA256.EncryptPassword(Form1.username+textBox2.Text));
int count = (int)command.ExecuteScalar();
if (count == 1) // 如果存在該賬號(hào)且密碼正確
{
try
{
// 構(gòu)建 SQL 刪除語(yǔ)句,刪除該賬號(hào)和密碼
SqlCommand deleteCommand = new SqlCommand("DELETE FROM 登錄注冊(cè)表 WHERE username=@username AND password=@password", conn);
deleteCommand.Parameters.AddWithValue("@username", Form1.username); // 使用 form1 中的 username
deleteCommand.Parameters.AddWithValue("@password", SHA256.EncryptPassword(Form1.username+textBox2.Text));
deleteCommand.ExecuteNonQuery();
MessageBox.Show("賬號(hào)注銷(xiāo)成功!");
Delete2();//同時(shí)刪除實(shí)名信息的數(shù)據(jù)
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
catch (Exception ex)
{
MessageBox.Show("賬號(hào)注銷(xiāo)失敗!" + ex.Message);
}
}
else
{
MessageBox.Show("賬號(hào)密碼錯(cuò)誤!");
textBox2.Text = "";
this.ActiveControl = textBox2;
}
}
}
private void Delete2() //刪除注銷(xiāo)賬號(hào)的實(shí)名信息
{
using (SqlConnection conn = new SqlConnection(Stu))
{
conn.Open();
try
{
// 構(gòu)建 SQL 刪除語(yǔ)句,刪除該賬號(hào)和密碼
SqlCommand deleteCommand = new SqlCommand("delete from 實(shí)名信息表 where username=@username", conn);
deleteCommand.Parameters.AddWithValue("@username", Form1.username); // 使用 form1 中的 username
deleteCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("賬號(hào)注銷(xiāo)失??!" + ex.Message);
}
conn.Close();
}
}
private void Form5_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
6、窗體Form6:實(shí)名認(rèn)證界面
label1——姓名,label2——身份證號(hào)
textBox1——對(duì)應(yīng)姓名,textBox2——對(duì)應(yīng)身份證號(hào)
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace CT12
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
this.Text = "實(shí)名認(rèn)證";
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.MaximumSize = 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(); //返回密文
}
public static readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;
public static string name; //創(chuàng)建參數(shù)name,實(shí)名認(rèn)證成功后顯示在主界面Form3
public static string IDcard; //創(chuàng)建參數(shù)IDcard,實(shí)名認(rèn)證成功后顯示在主界面Form3
private void Button1_Click(object sender, EventArgs e) //綁定操作
{
SqlConnection conn = new SqlConnection(Stu);
conn.Open();
// 查詢(xún)?cè)撡~號(hào)是否已經(jīng)實(shí)名認(rèn)證過(guò)
string sqlQuery = $"SELECT COUNT(*) FROM 實(shí)名信息表 WHERE username='{Form1.username}'";
SqlCommand cmdQuery = new SqlCommand(sqlQuery, conn);
int count = (int)cmdQuery.ExecuteScalar();
if (count > 0)
{
MessageBox.Show("該賬號(hào)已實(shí)名認(rèn)證");
}
else
{
// 查詢(xún)?cè)撋矸葑C號(hào)是否已被其他人注冊(cè)
string sqlCheckID = $"SELECT COUNT(*) FROM 實(shí)名信息表 WHERE IDcard='{textBox2.Text.Trim()}'";
SqlCommand cmdCheckID = new SqlCommand(sqlCheckID, conn);
int idCount = (int)cmdCheckID.ExecuteScalar();
if (idCount > 0)
{
MessageBox.Show("該身份證已經(jīng)被別人注冊(cè)");
}
else
{
try
{
// 綁定實(shí)名認(rèn)證賬號(hào)
string sqlInsert = $"INSERT INTO 實(shí)名信息表 (username, Name, IDcard) VALUES ('{Form1.username}', '{textBox1.Text.Trim()}', '{textBox2.Text.Trim()}')";
SqlCommand cmdInsert = new SqlCommand(sqlInsert, conn);
cmdInsert.ExecuteNonQuery();
conn.Close();
MessageBox.Show("實(shí)名認(rèn)證成功!");
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
catch(Exception ex)
{
MessageBox.Show("實(shí)名認(rèn)證失?。? + ex.Message);
}
}
}
conn.Close(); // 關(guān)閉數(shù)據(jù)庫(kù)連接
}
private void Form6_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
Form6運(yùn)行效果截圖
總結(jié)
以上就是今天要跟大家分享的內(nèi)容,可能代碼有點(diǎn)多,代碼邏輯看起來(lái)比較亂,花了幾個(gè)星期時(shí)間寫(xiě)的,期間修修改改了很多次,尚且還有不足之處,僅供學(xué)習(xí)和參考;感興趣的小伙伴可以來(lái)看看,希望對(duì)你有所幫助,也歡迎大家的批評(píng)指正。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-787042.html
本人聲明:未經(jīng)本人許可,禁止轉(zhuǎn)載?。?!如果要引用部分代碼,請(qǐng)標(biāo)明出處?。?/strong>文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-787042.html
到了這里,關(guān)于C#窗體程序連接SQL Server數(shù)據(jù)庫(kù)實(shí)現(xiàn)賬號(hào)登錄、賬號(hào)注冊(cè)、修改密碼、賬號(hào)注銷(xiāo)和實(shí)名認(rèn)證(不定時(shí)更新)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!