我試圖添加一個更改密碼功能到我的程序,但它不斷拉動錯誤。這就是所謂的代碼這就是當你點擊保存運行:在位置0沒有行OleDb數據庫
private void button3_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = (@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\User\Desktop\esoft\gym\gym\bin\Debug\Clients.accdb");
OleDbDataAdapter da = new OleDbDataAdapter(" SELECT COUNT(*) FROM login WHERE username='"+textBox1.Text+ "' AND password='" + textBox2.Text + "'",con);
DataTable dt = new DataTable();
con.Open();
errorProvider1.Clear();
if (dt.Rows[0][0].ToString() == "1")
{
if (textBox3.Text == textBox4.Text)
{
OleDbDataAdapter sda = new OleDbDataAdapter("UPDATE login WHERE username ='" + textBox1.Text + "', password='" + textBox2.Text + "' (password ='" + textBox3.Text + "')", con);
sda.Fill(dt);
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");
}
con.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
現在的主要錯誤是,試圖保存時,它改變的時候拉了一個錯誤,指出沒有行被發現在位置3到[1] [5]它爲該位置提出了相同的錯誤。
我使用您的建議更改了代碼,但仍然得到相同的錯誤。
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"))
{
OleDbDataAdapter da = new OleDbDataAdapter(" ExecuteScalar FROM login WHERE username='" + textBox1.Text + "' AND password='" + textBox2.Text + "'", con);
DataTable dt = new DataTable();
con.Open();
errorProvider1.Clear();
if (dt.Rows[0][0].ToString() == "1")
{
if (textBox3.Text == textBox4.Text)
{
// OleDbDataAdapter sda = new OleDbDataAdapter("UPDATE login SET password ='" + textBox3.Text + "' WHERE username ='" +textBox2.Text+"'");
OleDbCommand com = new OleDbCommand("UPDATE login SET password = '" + textBox3.Text + "' WHERE username = '" +textBox2.Text+"'",con);
com.ExecuteNonQuery();
// sda.Fill(dt);
MessageBox.Show("password successfully changed", "success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
errorProvider1.SetError(textBox3, "passwords dont match");
errorProvider1.SetError(textBox4, "passwords dont match");
}
這行的目的是什麼?OleDbDataAdapter da = new OleDbDataAdapter(「SELECT COUNT(*)FROM login WHERE username ='」+ textBox1.Text +「'AND password ='」+ textBox2.Text +「'」, con);'你聲明它,但永遠不會執行代碼來填充'OleDbDataAdapter',如果你只返回一行google,那麼你應該使用'ExecuteScalar'當你有時間 – MethodMan