c#更新数据库但不更新数据库

时间:2013-07-01 17:40:10

标签: sql-server-ce

我一直在尝试更新我的数据库。不知何故,我的编码没有错误,但数据没有更新数据库中的值。下面是我的代码

SqlCeConnection conn = new SqlCeConnection("Data Source = Program Files\\PROGRAM\\DATABSE.sdf");
conn.Open();

SqlCeCommand command1 = new SqlCeCommand("Update LOT_ATT SET UPI ='" + txtUPI.Text + "' WHERE HEX_ID = (Select UPI from LOT_ATT Where ID ='" + txtUPI.Text + "'", conn);
SqlCeCommand command2 = new SqlCeCommand("Update LOT_ATT SET NEGERI='" + txtNegeri.Text + "'WHERE HEX_ID = (Select NEGERI from LOT_ATT Where ID ='" + txtUPI.Text + "'", conn);
SqlCeCommand command3 = new SqlCeCommand("Update LOT_ATT SET DAERAH='" + txtDaerah.Text + "'WHERE HEX_ID = (Select DAERAH from LOT_ATT Where ID ='" + txtUPI.Text + "'", conn);
SqlCeCommand command4 = new SqlCeCommand("Update LOT_ATT SET MUKIM ='" + txtMukim.Text + "'WHERE HEX_ID = (Select MUKIM from LOT_ATT Where ID ='" + txtUPI.Text + "'", conn);
SqlCeCommand command5 = new SqlCeCommand("Update LOT_ATT SET LOT ='" + txtLot.Text + "'WHERE HEX_ID= (Select LOT from LOT_ATT Where ID ='" + txtUPI.Text + "'", conn);
SqlCeCommand command6 = new SqlCeCommand("Update LOT_ATT SET AREA='" + txtArea.Text + "'WHERE HEX_ID = (Select AREA from LOT_ATT Where ID ='" + txtUPI.Text + "'", conn);

conn.Close();
MessageBox.Show("Updated!");

1 个答案:

答案 0 :(得分:1)

您需要执行命令。只是定义它们不会执行它们。

command1.ExecuteNonQuery();
command2.ExecuteNonQuery();
// etc, etc.

确保在关闭连接之前执行此操作。