c# - Oledb 更新命令

标签 c# oledb

我制作了一个从数据库保存和更新数据的程序,我可以保存和读取数据,我也可以更新但问题是,我不能选择 "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",有没有办法让我也可以更新 Firstname?或者使用 "ID" 作为索引?谢谢

最佳答案

我不知道您要针对哪个数据库,但是,我不知道 OleDB 是否对您的参数的顺序很挑剔。即:您是否尝试将“ID”参数放在最后一个位置以匹配更新命令字段的实际顺序?不知道是不是扔出去了。

关于c# - Oledb 更新命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126427/

相关文章:

c# - Xamarin Forms PCL 中的 LoadApplication 错误

c# - 以编程方式向 datagridview 添加新行

c# - 使用微型 ORM 时的最佳策略?

excel - 加载 csv "Could not find installable ISAM"

c# - VSTO 合并单元格

c# - C# winform中的excel列选择查询

database - native OLE DB 和 ADO.NET 之间的速度差异

c# - 泛型类型推断解释

c# - 如何设置菜单的快捷方式?

sql-server-2008 - SSIS不能使用变量(平面文件> OLE DB命令),必须声明标量变量 "@"