如何使用C#更改密码 [英] How Do I Make Password Change Using C#

查看:79
本文介绍了如何使用C#更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.OleDb;

命名空间 WindowsFormsApplication1
{
public partial class Form2:Form
{
OleDbConnection con = new OleDbConnection( @ Provider = Microsoft.ACE.OLEDB.12.0; Data
Source ='login.accdb'
);

public Form2()
{
InitializeComponent();
}

private void label3_Click( object sender,EventArgs e)
{

}

private void button2_Click( object sender,EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Show();
this .Hide();
}

private void button1_Click( object sender,EventArgs e)
{
string sqlSearch = SELECT * FROM reg WHERE password =' + textBox1.Text + < span class =code-string>';
OleDbCommand cmd = new OleDbCommand(sqlSearch,con);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (textBox2.Text == textBox3.Text)
{

string sqlUpdate = UPDATE reg SET WHERE password =' + textBox1.Text + ;
OleDbCommand cmdd = new OleDbCommand(sqlUpdate,con);
con.Open();
cmdd.ExecuteNonQuery();
con.Close();
MessageBox.Show( 已成功更新);
}
else
{
MessageBox.Show( 正确地重新输入新密码);
}
}

else
{
MessageBox.Show( 找不到记录!);
}
con.Close();

}
}
}

解决方案

要更改密码,请按以下步骤操作



  • 从屏幕上的用户获取当前密码(txtCurrentPassword.Text)
  • 从数据库中读取密码用户。 (string currentPassword;)

  • 比较以上两个值,如果不匹配则提示你当前的密码不匹配,请输入正确的密码

    如果匹配则可以继续下一步。

  • 新密码(来自(文本框)txtNewPassword.Text)

    重新输入新密码(来自(文本框)txtNewPassword.Text)

  • 比较新密码和重新输入密码。如果不匹配,则提示不匹配

    如果匹配则可以将新密码保存在数据库中





处理查询语句:查询和语句帮助文档 [ ^ ]

Quote:

string sqlUpdate =UPDATE reg SET WHERE password ='+ textBox1.Text +;



应该是

 string sqlUpdate =UPDATE reg SET password ='+ textBox2.Text +'WHERE password = '+ textBox1.Text +'; 





但是,我建议你阅读这篇文章:密码存储:如何操作。 [ ^ ]。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source='login.accdb'");

        public Form2()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            frm1.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlSearch = "SELECT * FROM reg WHERE password='" + textBox1.Text + "'";
            OleDbCommand cmd = new OleDbCommand(sqlSearch, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                if (textBox2.Text == textBox3.Text)
                {

                    string sqlUpdate = "UPDATE reg SET WHERE password='" + textBox1.Text + "";
                    OleDbCommand cmdd = new OleDbCommand(sqlUpdate, con);
                    con.Open();
                    cmdd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("successfully updated");
                }
                else 
                {
                    MessageBox.Show("retype your new password correctly");
                }
            }

            else
            {
                MessageBox.Show("No Record Found!");
            }
            con.Close();

        }
    }
}

解决方案

For changing password follow these things

  • Get the current password from the User in screen ( txtCurrentPassword.Text )
  • Read the password from the DB for that user. ( string currentPassword ; )
  • compare the above two values, if it is not matching prompt a message saying "you current password is not matching, pls enter correct one"
    if it is matching then you can proceed the next step.
  • New Password ( get from (textbox) txtNewPassword.Text )
    ReType New Password ( get from (textbox) txtNewPassword.Text )
  • compare the new password and retype password .if it is not matching, prompt as "not matching"
    if it is matching then you can save the New password in the db


Take care of Query Statements : Querys and statements help doc[^]


Quote:

string sqlUpdate = "UPDATE reg SET WHERE password='" + textBox1.Text + "";


That should be

string sqlUpdate = "UPDATE reg SET password='" + textBox2.Text + "' WHERE password='" + textBox1.Text + "'";



However, I suggest you to read this article: "Password Storage: How to do it."[^].


这篇关于如何使用C#更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆