I have this code:
private void FormProduction_FormClosed(object sender, FormClosedEventArgs e)
{
if (name != string.Empty && school != string.Empty)
{
if (id == -1)
{
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd = new MySqlCommand("INSERT Person(Name, School) VALUES (name, school)", conn);
conn.Open();
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@school", school);
cmd.ExecuteNonQuery();
conn.Close();
}
else
{
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd = new MySqlCommand("UPDATE Person SET Name=name, School=school WHERE Id=id", conn);
conn.Open();
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@school", school);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
conn.Close();
}
Administration.fill();
}
}
And my table Person doesn't change when I execute a program (any inserts and any updates). Where's an error?
CodePudding user response:
Hope this can help you:
More information on MySQL parametrized queries (official doc.)
private void FormProduction_FormClosed(object sender, FormClosedEventArgs e)
{
if (name != string.Empty && school != string.Empty)
{
if (id == -1)
{
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd = new MySqlCommand("INSERT Person(Name, School) VALUES (@name, @school)", conn);
conn.Open();
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@school", school);
cmd.ExecuteNonQuery();
conn.Close();
}
else
{
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd = new MySqlCommand("UPDATE Person SET Name=@name, School=@school WHERE Id=@id", conn);
conn.Open();
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@school", school);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
conn.Close();
}
Administration.fill();
}
}