我有这个方法
public void Update(int id, string name)
{
OpenConnection();
using (OleDbCommand update = new OleDbCommand("UPDATE Students SET Name = @name WHERE ID = @id", databaseConnection))
{
update.Parameters.AddWithValue("@id", id);
update.Parameters.AddWithValue("@name", name);
MessageBox.Show(update.ExecuteNonQuery().ToString());
}
CloseConnection();
}
当它运行时,我受到0行的影响。我尝试将命令括在选择块中。
using (OleDbCommand select = new OleDbCommand("SELECT * FROM Students WHERE ID = @id", databaseConnection))
{
select.Parameters.AddWithValue("@id", id);
using (OleDbDataReader reader = select.ExecuteReader())
{
using (OleDbCommand update = new OleDbCommand("UPDATE Students SET Name = @name WHERE ID = @id", databaseConnection))
{
update.Parameters.AddWithValue("@id", id);
update.Parameters.AddWithValue("@name", name);
MessageBox.Show(update.ExecuteNonQuery().ToString());
}
}
}
执行此操作时,消息框仍会触发,并且会影响0行。我在这里做错了什么?