简体   繁体   中英

How to update SQL Server CE database

I have a question regarding updating a SQL Server CE database using Visual Studio & C#.

I have a database called Database1 which has a table called Cars . The table Cars has 4 columns CarID , Manufacturer , Model and Color . CarID is the primary key and Identity is set to true.

I want to update a record in the database using the CarID column as I may want to update all the other columns - Manufacturer , Model and Color . In the attached project file, I have used the CarID Identity Number 1 but how do I do this without using the CarID Identity number? I understand that I need to declare the CarID somehow but cannot figure out how to do this.

Thanks!

Download Project File

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();

您无需声明CarID,因为您已经在此列中设置了身份,它将自动填充。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM