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

Posted

技术标签:

【中文标题】如何使用 C# 控制台应用程序中的方法更新表?【英文标题】:how to update table with method from C# console app? 【发布时间】:2020-09-19 21:31:10 【问题描述】:

我尝试在 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 和正确的值。

【问题讨论】:

【参考方案1】:

UPDATE 语法不正确,您不需要 () 中的列名,就像在 INSERT 语句中那样:Products (ProductName, ProductStock, ProductCategoryID, ProductEmployeeID)SET 之前。

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

参考:Update Syntax

感谢使用此语法:command.Parameters.Add("@productid", SqlDbType.Int).Value

【讨论】:

谢谢!这是解决方案,感谢您的赞誉,我刚刚学习 c#。我所知道的大部分内容来自于在 *** 上询问像你这样的人! 最好的学习 C# 和数据库。我向软件开发人员强烈推荐这些 SQL Server 站点(它们提供大量免费内容和视频):nettrax.net/links(Brent Ozar、Erik Darling、Pinal Dave),在过去的一年里,我从他们那里学到了很多关于数据库的知识.

以上是关于如何使用 C# 控制台应用程序中的方法更新表?的主要内容,如果未能解决你的问题,请参考以下文章

每次启动控制台应用程序时,如何更新表中的信息?

使用 DataTable 中的 OracleDataAdapter 更新记录并将记录插入 Oracle 表

如何使用 c# 监控 SQL Server 表更改?

如何使用 c# 监控 SQL Server 表更改?

如何使用 C# 程序中的新值快速更新表中的所有行

使用从我的 C# 应用程序传递的值列表更新兴趣表