【发布时间】:2016-01-07 06:31:11
【问题描述】:
我正在尝试实现密码更改功能,但它似乎不想工作。
private void button3_Click(object sender, EventArgs e)
{
using (OleDbConnection con = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\User\Desktop\esoft\gym\gym\bin\Debug\Clients.accdb"))
{
DataTable dt = new DataTable();
con.Open();
errorProvider1.Clear();
if (dt.Rows[0][0].ToString() == "1")
{
if (textBox3.Text == textBox4.Text)
{
OleDbDataAdapter da = new OleDbDataAdapter(" COUNT (*) FROM login WHERE username= '" + textBox1.Text + "' AND [password]='" + textBox2.Text + "' ", con);
OleDbCommand com = new OleDbCommand("UPDATE login SET [password] = '" + textBox3.Text + "' WHERE username = '" + textBox2.Text + "'", con);
com.ExecuteNonQuery();
MessageBox.Show("password successfully changed", "success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
errorProvider1.SetError(textBox3, "passwords dont match");
errorProvider1.SetError(textBox4, "passwords dont match");
}
}
else
{
errorProvider1.SetError(textBox1, "wrong username");
errorProvider1.SetError(textBox2, "wrong pasword");
}
}
}
if (dt.Rows[0][0].ToString() == "1") 行中有一个错误,指出在该位置未找到数据,但数据表中有 5 行。
当代码在没有上述行的情况下运行时,如//if (dt.Rows[0][0].ToString() == "1")
代码运行,但表中没有数据更新。
再次更新代码,仍然收到同样的错误:
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM login WHERE username= '" + textBox1.Text + "' AND [password]='" + textBox2.Text + "' ", con);
DataTable dt = new DataTable();
da.Fill(dt);
con.Open();
errorProvider1.Clear();
if (dt.Rows[0][0].ToString() == "1")
【问题讨论】:
-
更大的问题是您试图存储密码。不要那样做!!!
-
con.read() 我认为丢失了
-
您只创建了
DataTable的一个新实例,而没有为其分配任何数据! -
并且代码中没有查询sn-pt来填充DataTable