我们可以在同一个按钮操作中同时使用插入和更新吗?

发布于 2024-11-03 19:16:27 字数 1008 浏览 1 评论 0原文

我有一个表名称为“销售额”,另一个表名称为“余额”。我想在销售中使用插入命令并更新余额表。我能做些什么?

     cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " )"
     Dim NB As Double
    NB = TextBox11.Text
    ST = ComboBox1.SelectedValue.ToString
    cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & " " & NB & " " & " WHERE         CUSTOMERNAME =  " & " '" & ST & "'" & " "
    cmd.Connection = con
    cmd.ExecuteNonQuery()
    con.Close()

它显示错误为从字符串“ UPDATE BALANCE SET OBBALANCE = ”到类型“Integer”的转换无效

I have a Table name as sales and another is balance. I want to use insert command in sales and update for the balance table. What can I do?

     cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " )"
     Dim NB As Double
    NB = TextBox11.Text
    ST = ComboBox1.SelectedValue.ToString
    cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & " " & NB & " " & " WHERE         CUSTOMERNAME =  " & " '" & ST & "'" & " "
    cmd.Connection = con
    cmd.ExecuteNonQuery()
    con.Close()

It shows an error as Conversion from string " UPDATE BALANCE SET OBBALANCE = " to type 'Integer' is not valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒与心事 2024-11-10 19:16:27

您可以在 INSERT 末尾添加 分号语句将它们分成两个单独的语句。但您的错误是您将 CommandType 设置为字符串,这是一个枚举: CommandType 枚举

You could add a semi colon at the end of the INSERT statement to split them into two separate statements. But your error is that your are setting your CommandType to a string and this is an enumeration: CommandType Enumeration

芯好空 2024-11-10 19:16:27

CommandType 是一个枚举,它告诉 CommandText 中的值是什么:查询、表名称等。

您不能将查询文本放在那里。

相反,您应该执行以下任一操作(从最好到最差):

  1. 将两个命令放入服务器端的存储过程中并调用该存储过程。这将是一个更好的方法。

  2. UPDATE 命令附加到 INSERT 命令。

  3. 创建 ADO.Command 的另一个实例,并在同一事务中再次运行它。

CommandType is an enum which tells what a value in CommandText is: a query, a table name etc.

You can't put the query text there.

Instead, you should do either of the following (from best to worst):

  1. Put both commands into a stored proc on server side and call the stored proc. This would be a better way.

  2. Append the UPDATE command to the INSERT command.

  3. Create another instance of ADO.Command and run it again in the same transaction.

傻比既视感 2024-11-10 19:16:27

您可以使用 ; 将两者发送到服务器:

cmd.CommandText="insert into ...(...) values(...); update ... set ..."
cmd.Connection=con
cmd.ExecuteNonQuery()

这也是您获取所使用的身份值的方式,您可以在后面附加一个 ;select @@identity 插入

其他人介绍了为什么 CommandType 是错误的属性,因此我将其保留。

You can send both to the server using ;:

cmd.CommandText="insert into ...(...) values(...); update ... set ..."
cmd.Connection=con
cmd.ExecuteNonQuery()

This is also how you'd get back the identity value used, you'd append a ;select @@identity after the insert.

The others covered why CommandType is the wrong property to touch so I'll leave it at that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文