How to add data into mysql using c#(编辑组合框选择的table列)

How to add data into mysql using c# (edit table column on combo box selection )

我想根据用户在项目组合框中选择的内容,在 mysql 的数据库中的 table 中添加项目。

例如:如果一个人选择茶(此事件应填充我的 table 中的茶列)并将数量填写为 2,那么我想在 mysql 中的列名称茶下添加 2 table。我正在尝试使用更新语句,但它给出了一个错误 The syntax is not correct.The column to be filled in table changes if the user choose tea or coffee 这就是我使用 [=13 的原因=]

"UPDATE employee.transaction 
 SET   department = ' " + this.department.Text + " ',
 ' " + this.items.Text + " ' =  ' " + this.quantity.Text + " ' ,
 billno = ' " + this.bill_no_txt.Text + "' 
 where billno = ' " + this.bill_no_txt.Text + " ' ;";

我可能会在您的查询中看到几个可能导致问题的点。那么让我们通过它:

SET   department = ' " + this.department.Text + " ',

请确保您在数据库中输入了正确的数据。在 SQL 查询中,如果您在文本或 varchar 字段中输入文本,则需要确保没有 spaces。您在上面输入的是“ TEXT ”而不是 "TEXT"(注意 space)。以下将输入开头和结尾没有 space 的文本:

SET   department = '" + this.department.Text + "',

最有可能导致错误的是:

billno = ' " + this.bill_no_txt.Text + "'

我假设您的 billno 列被定义为 int(这是正确的)。但是您必须确保按原样插入它。通过使用 '(单括号),您正在尝试将文本输入到 int 字段中,这会导致错误。我不能肯定地说,因为我看不到要更新数据的 table 的 table 定义。

还有可能导致问题的是这里列名错误:

 ' " + this.items.Text + " ' =  ' " + this.quantity.Text + " ' ,

 ' " + this.items.Text + " ' 

将基本上输入错误的“'Columnname'”(带有“'”和 spaces)。它可能应该是 "Columnname"。在没有 spaces 和没有 singlebackets 的情况下尝试:

 " + this.items.Text + " =  '" + this.quantity.Text + "' ,

但尽管如此,我认为您的数据库存在架构问题...如果您有 30 种不同的饮料,您将需要 30 列,这可以通过三列之间的 n 到 n 关系来完成 table 和两列 table。我建议您先了解一下。

这是更正后的查询,但请确保您了解更改内容:

UPDATE employee.transaction SET  department = '" + this.department.Text + "'," + this.items.Text +" =  '" + this.quantity.Text + "' , billno = " + this.bill_no_txt.Text + " where billno = " + this.bill_no_txt.Text + ";