【问题标题】:C# Update Multiple textbox for one column?C#为一列更新多个文本框?
【发布时间】:2013-03-17 02:57:12
【问题描述】:

我的 Windowform 上有 5 个文本框和一个按钮,它显示来自我的访问数据库文件的数据。

然后我使用更新语句将所有数据键更新到 textbox1、textbox2、textbox3 等中。

我的更新声明:

cmd = new oledbcommand("UPDATE Table2 SET BALANCE = ? " + " WHERE ID = ? ", con);
cmd.parameters.addwithvalue("BALANCE", textbox5.text);
cmd.parameters.addwithvalue("BALANCE", textbox6.text);
cmd.parameters.addwithvalue("BALANCE", textbox7.text);
cmd.parameters.addwithvalue("BALANCE", textbox8.text);
cmd.parameters.addwithvalue("BALANCE", textbox9.text);

cmd.parameters.addwithvalue("ID", textbox10.text)

但它失败了,没有错误。

所以我尝试了另一种方法

cmd.parameters.addwithvalue("BALANCE", textbox5.text + textbox6.text + textbox7.text + textbox8.text + textbox9.text );

我输入的所有值都进入 textbox5。有什么方法可以解决吗?

对不起,我是新人..希望你能理解我的问题。

编辑

I tried to cmd = new oledbcommand("UPDATE Table2 SET BALANCE = ? ", con);
    cmd.parameters.addwithvalue("BALANCE", textbox5.text);
    cmd.parameters.addwithvalue("BALANCE", textbox6.text);
    cmd.parameters.addwithvalue("BALANCE", textbox7.text);
    cmd.parameters.addwithvalue("BALANCE", textbox8.text);
    cmd.parameters.addwithvalue("BALANCE", textbox9.text);

我把不同的数字放到不同的文本框中,然后我预览数据,所有的数据值都是一样的......

【问题讨论】:

  • 根据你的sql查询你只更新余额字段
  • 我显示余额中的所有数据...如何更新多个文本框以平衡?

标签: c# sql-update oledb


【解决方案1】:
cmd.parameters.addwithvalue("BALANCE", textbox5.text +","+ textbox6.text  +","+ textbox7.text  +","+ textbox8.text  +","+ textbox9.text );

当你想使用它们时,按,分开

【讨论】:

  • 它的工作,但所有的价值都转到 textbox5.text.... 我从 textbox5 - 10 输入的
  • 当您在BALANCE 字段中显示get 值并将其拆分为, 并显示值时
  • 我想你误解了我的问题。 textbox1 值将显示在 textbox1 上,textbox2 值将显示在 textbox2 上,依此类推。我不想在 textbox1 中合并。例如,在我的表格中,我得到了 BOOK BALANCE BOOK A 80 BOOK B 70 BOOK C 60
  • 让我直截了当地说你想显示你的数据库有多少行?您所描述的是将 5 个值连接到表的一行中,还是将 5 个不同的行分别与 BALANCE 列的值放在一起?
  • @Paciente8159 ,好的,在我的访问数据库中,我有 2 列是 Book and Balance,我有 5 行来放置书名和余额,在我的窗口窗体中,我显示余额所有书籍都放入不同的文本框(textbox1 - 5),这是允许用户更新。
【解决方案2】:

试试这个

var totalBalance = Convert.ToDouble(Textbox1.Text) +  Convert.ToDouble(Textbox2.Text) + Convert.ToDouble(Textbox3.Text) + Convert.ToDouble(Textbox4.Text) +  Convert.ToDouble(Textbox5.Text) ;

cmd.parameters.addwithvalue("BALANCE", totalBalance);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多