我做了一个保存和更新数据库数据的程序,我可以保存和读取数据,我也可以更新,但问题是,我不能选择作为索引,这是我使用作为索引的"ID"
示例代码,"ID"
cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Records SET FirstName = @firstname, LastName = @lastname, Age = @age, Address = @address, Course = @course WHERE [ID] = @id";
cmd.Parameters.AddWithValue("@id", int.Parse(label7.Text));
cmd.Parameters.AddWithValue("@firstname", textBox1.Text);
cmd.Parameters.AddWithValue("@lastname", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.Parameters.AddWithValue("@address", textBox4.Text);
cmd.Parameters.AddWithValue("@course", textBox5.Text);
cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery();
{
MessageBox.Show("Update Success!");
cn.Close();
}
这是我的更新代码,但索引是"firstname"
,
cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Records SET FirstName = @firstname, LastName = @lastname, Age = @age, Address = @address, Course = @course WHERE FirstName = @firstname";
//cmd.Parameters.AddWithValue("@id", int.Parse(label7.Text));
cmd.Parameters.AddWithValue("@firstname", textBox1.Text);
cmd.Parameters.AddWithValue("@lastname", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.Parameters.AddWithValue("@address", textBox4.Text);
cmd.Parameters.AddWithValue("@course", textBox5.Text);
cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery();
{
MessageBox.Show("Update Success!");
cn.Close();`
}
它有效,但问题是我无法更新"FirstName"
,有没有办法也可以更新名字?或使用"ID"
作为索引?谢谢