一、頁面設計
login頁面,和第二篇博文(用戶登錄和注冊)頁面基本一樣,只不過多了一個按鈕
其中忘記密碼?點我找回 為button3
retrieve_password頁面
change_password頁面
頁面如下:
二、數據庫
因為是忘記密碼,也就是修改密碼而已,數據表仍為yy_user不變
id設置為主鍵自增
隨便添加些數據
三、代碼如下
login頁面代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;namespace fiber_yy
{public partial class login : Form{public string name = "";public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);public string identification = null;public login(){InitializeComponent();}private void button1_Click(object sender, EventArgs e)//登錄{conn.Open();string sex = "";string phone = "";string day = DateTime.Now.ToLocalTime().ToString();//string time = DateTime.Now.ToLongTimeString().ToString();string username = textBox1.Text;string password = textBox2.Text;string identify = textBox3.Text;if (username.Equals("") || password.Equals("") || identify.Equals("")){MessageBox.Show("提示:請輸入用戶名、密碼、驗證碼!", "警告");}else{string sqlSel = "select count(*) from yy_user where username = '" + username + "' and password = '" + password + "'";SqlCommand cmd = new SqlCommand(sqlSel, conn);if (Convert.ToInt32(cmd.ExecuteScalar()) > 0 )//賬號密碼正確{string sql = "select username,sex,phone from yy_user where username = '" + username + "'";SqlCommand com = new SqlCommand(sql, conn);SqlDataReader read = com.ExecuteReader();while (read.Read())//獲取yy_user表中的username,sex,phone{//int number = Convert.ToInt32(read["username"]);//查詢列名1的數據,方法為: read(變量名)["列名"]; 該方法返回的是object類型name = read["username"].ToString();//MessageBox.Show(name);sex = read["sex"].ToString();//MessageBox.Show(sex);phone = read["phone"].ToString();//MessageBox.Show(phone);}read.Close();if (identify==identification)//判斷驗證碼是否輸入正確{string INSERT_sql = string.Format("INSERT INTO yy_user_record VALUES ('{0}','{1}','{2}','{3}')", name,sex,phone, DateTime.Now.ToLocalTime());SqlCommand INSERT_cmd = new SqlCommand(INSERT_sql, conn);int count = INSERT_cmd.ExecuteNonQuery();if (count > 0){MessageBox.Show(name);MessageBox.Show("記錄用戶登錄!");new main_page().Show();this.Hide();}else{MessageBox.Show("記錄用戶失敗");}}else {MessageBox.Show("驗證碼輸入錯誤");}}else{MessageBox.Show("請檢查賬號密碼");}}}private void pictureBox1_Click(object sender, EventArgs e){Random r = new Random();string str = null;for (int i = 0; i < 5; i++){int n = r.Next(0, 10);str += n;//包括字符串在內}identification = str;Bitmap b = new Bitmap(100, 15);Graphics g = Graphics.FromImage(b);for (int i = 0; i < 5; i++){String[] fonts = { "宋體", "黑體", "隸書", "仿宋", "微軟雅黑" };//字體數組Color[] colors = { Color.Red, Color.Black, Color.Blue,Color.YellowGreen ,Color.Green };//顏色數組Font f = new Font(fonts[r.Next(0, 5)], 25, FontStyle.Bold);SolidBrush s = new SolidBrush(colors[r.Next(0, 5)]);//定義一個單獨的畫筆,使每個字符的顏色隨機Point p = new Point(i * 20, 0);//每個字符間隔20g.DrawString(str[i].ToString(), Font, s, p);}for (int a = 0; a < 5; a++){Point p1 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));Point p2 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));//線的兩點不能超過圖片的長和寬Pen pen = new Pen(Brushes.Cyan);//青色線段g.DrawLine(pen, p1, p2);}pictureBox1.Image = b;}private void button2_Click(object sender, EventArgs e){new Form1().Show();this.Hide();}private void button3_Click(object sender, EventArgs e){new retrieve_password().Show();this.Hide();}}
}
retrieve_password頁面代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;namespace fiber_yy
{public partial class retrieve_password : Form{public string name = "";public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);public string identification = null;public string phone = "";public string phone_db = "";public string username = "";public string username_db = "";public string identify = "";public retrieve_password(){InitializeComponent();}private void pictureBox1_Click(object sender, EventArgs e){Random r = new Random();string str = null;for (int i = 0; i < 5; i++){int n = r.Next(0, 10);str += n;//包括字符串在內}identification = str;Bitmap b = new Bitmap(100, 15);Graphics g = Graphics.FromImage(b);for (int i = 0; i < 5; i++){String[] fonts = { "宋體", "黑體", "隸書", "仿宋", "微軟雅黑" };//字體數組Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.YellowGreen, Color.Green };//顏色數組Font f = new Font(fonts[r.Next(0, 5)], 25, FontStyle.Bold);SolidBrush s = new SolidBrush(colors[r.Next(0, 5)]);//定義一個單獨的畫筆,使每個字符的顏色隨機Point p = new Point(i * 20, 0);//每個字符間隔20g.DrawString(str[i].ToString(), Font, s, p);}for (int a = 0; a < 5; a++){Point p1 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));Point p2 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));//線的兩點不能超過圖片的長和寬Pen pen = new Pen(Brushes.Cyan);//青色線段g.DrawLine(pen, p1, p2);}pictureBox1.Image = b;}private void button1_Click(object sender, EventArgs e){username = textBox1.Text;phone = textBox2.Text;identify = textBox3.Text;//MessageBox.Show(identify);//MessageBox.Show(identification);if (identify == identification)//判斷驗證碼是否輸入正確{conn.Open();string sql = "select username,phone from yy_user where username = '" + username + "'";SqlCommand com = new SqlCommand(sql, conn);SqlDataReader read = com.ExecuteReader();while (read.Read())//獲取yy_user表中的username,sex,phone{//int number = Convert.ToInt32(read["username"]);//查詢列名1的數據,方法為: read(變量名)["列名"]; 該方法返回的是object類型username_db = read["username"].ToString();//MessageBox.Show(name);//sex = read["sex"].ToString();//MessageBox.Show(sex);phone_db = read["phone"].ToString();//MessageBox.Show(phone);}read.Close();MessageBox.Show(phone_db);if(phone_db==phone){//conn.Close();MessageBox.Show("校驗通過");new change_password(username).Show();this.Hide();}else {MessageBox.Show("注冊手機號不對");}}else{MessageBox.Show("驗證碼輸入錯誤");}}private void button2_Click(object sender, EventArgs e){new login().Show();this.Hide();}}}
change_password頁面代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace fiber_yy
{public partial class change_password : Form{public string name = "";public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);//public string identification = null;public string password = "";public string password1 = "";public string username = "";//OleDbCommand cmd = new OleDbCommand();//cmd.Connection = conn;public change_password(){InitializeComponent();}public change_password(string yy){InitializeComponent();username = yy;}private void button1_Click(object sender, EventArgs e){MessageBox.Show(username);password = textBox1.Text;password1 = textBox2.Text;if (password.Length <= 3 || password.Length >= 16){label3.Text = "密碼長度應該在3~16字符之間";}else if(password != password1){label3.Text = "兩次輸入密碼不一致";//MessageBox.Show("兩次輸入密碼不一致");}else if(password==password1){conn.Open();string sql = "update yy_user set password = '"+ password + "' where username = '" + username + "'";SqlCommand com = new SqlCommand(sql, conn);com.ExecuteNonQuery();//返回值為操作的條數MessageBox.Show("修改成功");conn.Close();new login().Show();this.Hide();}}}
}
四、效果展示
程序運行
登錄
忘記密碼?點我找回
通過手機號來校驗
驗證碼輸入錯誤
調試時輸出數據庫中的注冊手機號
與輸入框中的手機號對比
校驗成功,跳轉到修改密碼change_password頁面
兩次密碼必須一致
調試時,獲取登錄用戶的賬號
新密碼必須在3-16字符之間
修改成功
點擊確定自動返回登錄頁面
原始數據庫
右擊yy_user表
選擇前1000行
可看到密碼已經修改成功