I get error at calling cmd.ExecuteNonQuery.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'angelina','biology')'' at line 1
private void button2_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(@"server=localhost;database=name;uid=root;pwd=xxx;");
string query = "INSERT INTO table_student (@name, @major) VALUES ('" + textBox3.Text + "','" + textBox4.Text + "');";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = this.textBox3.Text;
cmd.Parameters.Add("@major", MySqlDbType.VarChar).Value = this.textBox4.Text;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
MessageBox.Show(i + "Data Saved");
}
}
usingand probably not do Database I/O on the Event Thread. But that's not the problem, here. And also for later: You may want to avoid putting username / password into your code.VALUES ('" + textBox3.Text + "','" + textBox4.Text + "')i.e., take user input and put it directly into a SQL string. See explaining xkcd for more