C# Oledb 更新命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15126427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oledb Update command
提问by Pyromancer
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,
我制作了一个从数据库中保存和更新数据的程序,我可以保存和读取数据,我也可以更新但问题是,我不能选择"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();
}
and here is my update code that works, but the index is the "firstname"
,
这是我的更新代码有效,但索引是"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();`
}
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
它有效,但问题是我无法更新"FirstName"
,有没有办法也可以更新名字?或使用"ID"
作为索引?谢谢
采纳答案by DRapp
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 是否对您的参数顺序有挑剔。即:您是否尝试将“ID”参数放在最后一个位置以匹配更新命令字段的实际顺序?不知道是不是扔了
回答by user4618777
You should add the following code after the last line of 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("@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.Parameters.AddWithValue("@id", int.Parse(label7.Text));
cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery(); {
MessageBox.Show("Update Success!");
cn.Close();
}