我在我的控制檯中得到的結果。更新語句C#中的語法錯誤Microsoft Access
UPDATE [客戶]設置用戶名= 'ASD',密碼= '房間隔缺損',地址= 'asddd',referenceno = '12345' WHERE ID = 27
當我寫在我的MS Access數據庫查詢,它工作正常。
我不知道爲什麼每當我嘗試將數據更新到數據庫時都會出現此錯誤。
private void buttonUpdate_Click(object sender, EventArgs e) // user click on button update
{
if (cbTable.Text.Equals("User"))
{
string query = "";
query += "username ='" + textBoxUsername.Text.ToString() + "' ,"; //query
query += "password ='" + textBoxPassword.Text.ToString() + "' ,"; //query
query += "contact ='" + ContactNo.Text.ToString() + "' ,"; //query
query += "ref_no = " + textBoxReferenceno.Text.ToString() + " WHERE id = " + Convert.ToInt32(textBoxId.Text.ToString()); //query
try
{
new controllerclass().updateDatabase("User", query); //update database
Console.WriteLine(query);
Console.WriteLine("Saved");
MessageBox.Show("User profile has been updated.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadDatabaseUser();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
//After users enter the update button, this function will be used.
public bool updateDatabase(string type, string query) //update database function
{
try
{
OleDbCommand cmd = new OleDbCommand(); //open connection
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [" + type + "] SET " + query;
cmd.Connection = conn;
Console.WriteLine("UPDATE [" + type + "] SET " + query);
cmd.ExecuteNonQuery(); //execute command
closeConnection();
return true;
}
catch (Exception e)
{
closeConnection(); // close connection
Console.WriteLine(e.Message); //writeline to console
return false;
}
}
您的代碼易受SQL注入攻擊。考慮使用參數化查詢。 –