• 九、忘记密码功能的实现


    一、页面设计

    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);//每个字符间隔20
                    g.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();
            }
        }
    }
    
    
    • 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
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154

    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);//每个字符间隔20
                    g.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();
            }
        }
        
    }
    
    
    • 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
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    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();
                }
            }
        }
    }
    
    
    • 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

    四、效果展示

    程序运行
    在这里插入图片描述
    登录
    在这里插入图片描述
    忘记密码?点我找回
    在这里插入图片描述
    在这里插入图片描述
    通过手机号来校验
    在这里插入图片描述
    验证码输入错误
    在这里插入图片描述
    调试时输出数据库中的注册手机号
    在这里插入图片描述
    与输入框中的手机号对比
    在这里插入图片描述
    校验成功,跳转到修改密码change_password页面
    在这里插入图片描述
    两次密码必须一致
    调试时,获取登录用户的账号
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

    新密码必须在3-16字符之间

    修改成功
    在这里插入图片描述

    点击确定自动返回登录页面
    在这里插入图片描述
    原始数据库
    在这里插入图片描述
    右击yy_user
    选择前1000行
    可看到密码已经修改成功
    在这里插入图片描述

  • 相关阅读:
    docker部署neo4j
    短视频矩阵系统,抖音矩阵系统,抖音SEO源码。
    细粒度IP定位参文2(Corr-SLG):A street-level IP geolocation method (2021年)
    JAVA面向接口编程课堂练习(马戏团)
    煤矿皮带运行视频监控系统
    Mybatis中拦截器的使用场景和技巧
    解决Win10电脑无线网卡的移动热点无法开启问题
    算法训练 第三周
    Seata入门系列【8】Seata之AT模式执行流程解析
    神奇的python的上下文管理器
  • 原文地址:https://blog.csdn.net/qq_41264055/article/details/125329735