代码中没有错误,但是 table 尝试更新时没有发生任何变化
No errors in the code, but no changes occur in the table when trying to update it
我正在尝试更新 C# Win Form 中的数据。
我创建了一个按钮“更新”,但是每当我 运行 它时,我都没有看到 table 中的任何变化和任何发生的错误
void insertdata() {
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM airport";
adapter.SelectCommand = cmd;
table.Clear();
adapter.Fill(table);
dgv.DataSource = table;
}
private void button_update_Click(object sender, EventArgs e)
{
cmd = connection.CreateCommand();
cmd.CommandText = "UPDATE airport SET p_name = '"+textBox2.Text+ "',p_age = '" + textBox3.Text + "', c_name = '" + textBox4.Text + "', date = '" + textBox5.Text + "', city_t = '" + textBox6.Text + "', city_f ='" + textBox7.Text + "', trip_num = '" + textBox8.Text + "', plane_type = '" + textBox9.Text+"' WHERE p_id = '"+textBox1+"'";
cmd.ExecuteNonQuery();
insertdata();
}
我试过添加
connection.Open();
connection.Close();
但是,我不断收到:“System.InvalidOperationException:“连接未关闭。连接已打开。"
更新 table 中的行的代码是否有任何更改,因为每当我 运行 它时我都没有收到任何错误。
请注意你写的内容
WHERE p_id = '"+textBox1+"'
而不是
WHERE p_id = '"+textBox1.Text+"'
可能您没有与文本框相同的 ID...
我正在尝试更新 C# Win Form 中的数据。 我创建了一个按钮“更新”,但是每当我 运行 它时,我都没有看到 table 中的任何变化和任何发生的错误
void insertdata() {
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM airport";
adapter.SelectCommand = cmd;
table.Clear();
adapter.Fill(table);
dgv.DataSource = table;
}
private void button_update_Click(object sender, EventArgs e)
{
cmd = connection.CreateCommand();
cmd.CommandText = "UPDATE airport SET p_name = '"+textBox2.Text+ "',p_age = '" + textBox3.Text + "', c_name = '" + textBox4.Text + "', date = '" + textBox5.Text + "', city_t = '" + textBox6.Text + "', city_f ='" + textBox7.Text + "', trip_num = '" + textBox8.Text + "', plane_type = '" + textBox9.Text+"' WHERE p_id = '"+textBox1+"'";
cmd.ExecuteNonQuery();
insertdata();
}
我试过添加
connection.Open();
connection.Close();
但是,我不断收到:“System.InvalidOperationException:“连接未关闭。连接已打开。"
更新 table 中的行的代码是否有任何更改,因为每当我 运行 它时我都没有收到任何错误。
请注意你写的内容
WHERE p_id = '"+textBox1+"'
而不是
WHERE p_id = '"+textBox1.Text+"'
可能您没有与文本框相同的 ID...