C#使用语句更新我的数据库

时间:2015-10-30 10:13:13

标签: c# sql-update primary-key

如何在主键的帮助下更新表格中的列值? 或者我可以以某种方式覆盖主键吗?

3 个答案:

答案 0 :(得分:1)

主键是唯一的,因此您无法更改它。如果要使用主键更新值,则可以。但是你无法更新主键。

答案 1 :(得分:1)

这个问题听起来像你试图做另一个插入而不是更新。

如果你正在做

insert into table (col1, col2, col3) values ('primaryKeyValue', 'col2val', 'col3val')

然后你尝试做同样的事情,因为主键约束会失败。

你应该做的

update table set col1 = 'newValue', col2 = 'newValue2' where 'primaryKeyValue' = 'primaryKeyValue'

答案 2 :(得分:1)

使用更新查询:

update tablename set column1 = 'new1', column2 = 'new2', 'primaryKey' = 'newPKValue' where 'primaryKey' = 'PKValue';

只需记住一件事&newCKValue'不应该重复。