我創造了C#登錄應用程序(Windows窗體),並使用.NET Framework 4.5和Visual Studio 2012更新密碼的Windows窗體C#
我已經手動輸入用戶名和密碼進入數據庫。
所以在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"]);
}
}
但上面的代碼不更新密碼。如何糾正上面的代碼?所以它的作品完美。
請指導我。
現有行的請告訴我錯誤 – meda
旁註:你會在得到負分如果您繼續以純文本形式將密碼保存到數據庫中,您的分配。 –
當您運行上述代碼時,是否收到任何錯誤?請顯示錯誤 – User2012384