如何使用密码修复更新语句

时间:2019-02-05 04:38:10

标签: c# mysql ms-access-2007

所以我的问题是说我的更新声明中有我的问题,但是我认为我的声明是正确的,如果我错了,请纠正我

                            connection.Open();
                            OleDbCommand command = new OleDbCommand();
                            command.Connection = connection;
                            string query = "update Admin set Password='" + Npassword.Text + "' WHERE Pk='" + txt2.Text + "'";
                            command.CommandText = query;
                            command.ExecuteNonQuery();
                            MessageBox.Show("Password Changed");
                            connection.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error, fill the fields required" + ex);
                            connection.Close();
                        }

1 个答案:

答案 0 :(得分:1)

暂时忽略注释中的好建议,密码是Access SQL中的保留字,因此必须放在括号中

string query = "update Admin set [Password]='" + Npassword.Text + "' WHERE Pk='" + txt2.Text + "'";

此外,如果 Pk 是数字,则不加引号:

string query = "update Admin set [Password]='" + Npassword.Text + "' WHERE Pk=" + txt2.Text + "";