簡體   English   中英

如何使用 C# 控制台應用程序中的方法更新表?

[英]how to update table with method from C# console app?

我試圖在 C# 中創建一個更新方法

我有數據庫類:

        public SqlConnection connection()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = "DESKTOP-UPVVOJP";
            builder.InitialCatalog = "Lagersystem";
            builder.IntegratedSecurity = true;

            return new SqlConnection(builder.ToString());
        }

和產品類別:

public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int ProductStock { get; set; }
        public int ProductCategoryID { get; set; }
        public int ProductEmployeeID { get; set; }
        public DateTime ProductCreatedDate { get; set; }

        // Constructor
        public Product(string productname, int productstock, int productcategoryid, int productemployeeid)
        {
            ProductName = productname;
            ProductStock = productstock;
            ProductCategoryID = productcategoryid;
            ProductEmployeeID = productemployeeid;
            ProductCreatedDate = DateTime.Now;
        }
    }

在我的程序中,我有 UpdateProduct 方法:

static void UpdateProduct(string updateproductname, int updateproductstock, int updateproductcategoryid, int updateproductemployeeid, string updateproductid)
        {
            Database db = new Database();
            SqlConnection conn = db.connection();
            conn.Open();
            using SqlCommand command = new SqlCommand(@"UPDATE Products (ProductName, ProductStock, ProductCategoryID, ProductEmployeeID)
                SET ProductName = @productname, 
                ProductStock = @productstock, 
                ProductCategoryID = @productcategoryid, 
                ProductEmployeeID = @productemployeeid 
                WHERE ProductID = @productid", conn);
            {
                command.Parameters.Add("@productid", SqlDbType.Int).Value = updateproductid;
                command.Parameters.Add("@productname", SqlDbType.NVarChar, 32).Value = updateproductname;
                command.Parameters.Add("@productstock", SqlDbType.Int).Value = updateproductstock;
                command.Parameters.Add("@productcategoryid", SqlDbType.Int).Value = updateproductcategoryid;
                command.Parameters.Add("@productemployeeid", SqlDbType.Int).Value = updateproductemployeeid;

                command.ExecuteNonQuery();
                conn.Close();
            }
        }

我從我的主要方法調用:

Console.Write("Hvilket produkt ID vil du slette?: ");
                        string updateproductid = Console.ReadLine();
                        Console.Write("Indtast nyt Produktnavn: ");
                        string updateproductname = Console.ReadLine();
                        Console.Write("Hvor mange er der på lager?: ");
                        int updateproductstock = int.Parse(Console.ReadLine());
                        Console.Write("Hvilken kategori ID hører produktet til?: ");
                        int updateproductcategoryid = int.Parse(Console.ReadLine());
                        Console.Write("Hvilket medarbejder ID har rettet produktet?: ");
                        int updateproductemployeeid = int.Parse(Console.ReadLine());
                        try
                        {
                            UpdateProduct(updateproductname, updateproductstock, updateproductcategoryid, updateproductemployeeid, updateproductid);
                            Console.WriteLine("\n" + updateproductname + " Opdateret med success!\n\n");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

當我嘗試運行它時,我收到一條錯誤消息,說我的 SQL 查詢在 '(' 附近有一個不正確的語法。當我調試以檢查參數是否正確時,一切似乎都很好,我得到了正確的 id 和正確的值每個參數。

UPDATE語法不正確,您不需要 () 中的列名,就像在 INSERT 語句中一樣: SET之前的Products (ProductName, ProductStock, ProductCategoryID, ProductEmployeeID)

通用示例UPDATE tableName Set ColumnName = @columnValue, ColumnName2 = @columnValue2 WHERE KeyColumn = @key

參考: 更新語法

使用此語法的榮譽: command.Parameters.Add("@productid", SqlDbType.Int).Value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM