Oledb更新命令 [英] Oledb Update command
问题描述
我制作了一个程序来保存和更新数据库中的数据,我可以保存和读取数据,也可以更新,但是问题是,我不能选择"ID"
作为索引,这是我的示例"ID"
作为索引的代码,
I make a program that saves and update a data from the database, I can save and read data, I can also update but the problem is, I can't select the "ID"
as the index, here is my sample code using "ID"
as the index,
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"
and here is my update code that works, but the index is the "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"
作为索引?谢谢
It works but the problem is I can't update the "FirstName"
, Is there a way that I can also update the Firstname? or use the "ID"
as the index? thanks
推荐答案
我不知道您要针对哪个数据库,但是,我不知道OleDB是否对您的参数的顺序序列感到挑剔.即:您是否尝试过将"ID"参数放在最后一个位置以匹配update命令字段的实际顺序?我不知道它是否会扔掉.
I don't know what database you are going against, however, I don't know if the OleDB is being picky on the ordinal sequence of your parameters. ie: Have you tried putting your "ID" parameter in the last position to match the actual order of the fields of your update command? I don't know if it's throwing it out.
这篇关于Oledb更新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!