繁体   English   中英

在Windows Form C#中更新密码

[英]Update Password in Windows Form C#

我正在C#中创建登录应用程序(Windows窗体),并在Visual Studio 2012中使用.NET Framework 4.5。

我已经手动将用户名和密码输入数据库了。

因此,在LoginForm中 ,用户输入用户名和密码,我的代码如下:

public partial class LoginForm : Form
{
    public LoginForm()
    {
        InitializeComponent();
    }

    System.Data.SqlServerCe.SqlCeConnection con;
    System.Data.SqlServerCe.SqlCeDataAdapter da;
    DataSet ds1;
    int MaxRows = 0;
    int inc = 0;

    private void go_Click(object sender, EventArgs e)
    {
        DataRow dRow = ds1.Tables["Users"].Rows[inc];
        if (EnterMasterPassword.Text == dRow.ItemArray.GetValue(1).ToString())
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        else
            MessageBox.Show("Try again");
    }

    private void LoginForm_Load(object sender, EventArgs e)
    {
        con = new System.Data.SqlServerCe.SqlCeConnection();
        con.ConnectionString = "Data Source=C:\\PMDatabase.sdf";
        con.Open();
        ds1 = new DataSet();
        string sql = "SELECT * From tbl_login";
        da = new System.Data.SqlServerCe.SqlCeDataAdapter(sql, con);
        da.Fill(ds1, "Users");
        MaxRows = ds1.Tables["Users"].Rows.Count;
        con.Close();
    }
}

*按钮在哪里 *

这可以完美运行,并且用户可以成功登录。 但是我不知道如何使用Windows窗体向用户提供更新/更改密码功能。 如何更新数据库? 以及用户名和密码字段中的内容吗?

到目前为止,我已经完成了以下过程:

当用户单击“更新密码”按钮时,将打开一个新表单(名称:Form7) ,其中包含3个标签,3个文本框和一个按钮,即:-

TextBox1-> CurrentPassword

TextBox2-> NewPassword

TextBox3-> ConfirmNewPassword

按钮-> UpdateUsrPassword

Form7的代码如下:-

public partial class Form7 : Form
    {
        public PasswordsAndData()
        {
            InitializeComponent();
        }
        System.Data.SqlServerCe.SqlCeConnection con1;
        System.Data.SqlServerCe.SqlCeDataAdapter da1;
        DataSet ds2;
        int totalPasswords = 0, inc = 0;
        private void Form7_Load(object sender, EventArgs e)
        {
            con1 = new System.Data.SqlServerCe.SqlCeConnection();
            con1.ConnectionString = "Data Source=C:\\PMDatabase.sdf";
            con1.Open();
            ds2 = new DataSet();
            string sql = "SELECT * From tbl_UserData";
            da1 = new System.Data.SqlServerCe.SqlCeDataAdapter(sql, con1);
            da1.Fill(ds2, "UserData");
            totalPasswords = ds2.Tables["UserData"].Rows.Count;
            con1.Close();
        }
        private void UpdateUsrPassword_Click(object sender, EventArgs e)
        {
            if(NewPassword.Text == ConfirmNewPassword.Text)
            {
                DataRow dRow1 = ds2.Tables["UserData"].NewRow();
                dRow1[1] = ConfirmNewPassword.Text;            
                ds2.Tables["UserData"].Rows.Add(dRow1);
                UpdateDB();
                MessageBox.Show("Password Updated");
                totalPasswords++;
                inc = totalPasswords - 1;
            }
        }
        private void UpdateDB()
        {
            System.Data.SqlServerCe.SqlCeCommandBuilder cb1;
            cb1 = new System.Data.SqlServerCe.SqlCeCommandBuilder(da1);
            cb1.DataAdapter.Update(ds2.Tables["UserData"]);
        }
    }

但是上面的代码并没有更新密码。 如何更正以上代码? 因此,它完美地工作。

请指导我。

DataRow dRow1 = ds2.Tables["UserData"].NewRow();
dRow1[1] = ConfirmNewPassword.Text;            
ds2.Tables["UserData"].Rows.Add(dRow1);
UpdateDB();

您要在UpdateUsrPassword_Click中添加一行,但是不应该更新现有行吗?

你应该做:

  1. 从数据库获取实际密码
  2. 包含实际密码的现有行的更新

而不是添加一行,您应该使用类似以下的内容:

dataSet1.Tables["Customers"].Rows[4]["CompanyName"] = "Updated Company Name";

这将更新数据集中的值,然后可以将其推回数据库。

资料来源,提供有关编辑数据集中值的更多信息:

https://msdn.microsoft.com/zh-CN/library/tat996zc.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM