我有一个关于使用Visual Studio&更新SQL Server CE数据库的问题。 C#。
我有一个名为Database1
的数据库,其中有一个名为Cars
的表。表格Cars
有4列CarID
,Manufacturer
,Model
和Color
。 CarID
是主键,Identity设置为true。
我想使用CarID
列更新数据库中的记录,因为我可能希望更新所有其他列 - Manufacturer
,Model
和Color
。在附加的项目文件中,我使用了CarID
身份编号1但是如何在不使用CarID
身份编号的情况下执行此操作?我知道我需要以某种方式声明CarID
,但无法弄清楚如何做到这一点。
谢谢!
SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Users\Dell\Desktop\update\update\Database1.sdf");
conn.Open();
SqlCeCommand update = new SqlCeCommand("UPDATE [Cars] SET Manufacturer = @Manufacturer, Model = @Model, Color = @Color WHERE CarID = @CarID", conn);
update.Parameters.AddWithValue("@CarID", 1);
update.Parameters.AddWithValue("@Manufacturer", txtManufacturer.Text);
update.Parameters.AddWithValue("@Model", txtModel.Text);
update.Parameters.AddWithValue("@Color", txtColor.Text);
update.ExecuteNonQuery();
conn.Close();
答案 0 :(得分:0)
您不需要声明CarID,它会自动填充,因为您已经为此列设置了标识。