我们可以在同一个按钮操作中同时使用插入和更新吗?
我有一个表名称为“销售额”,另一个表名称为“余额”。我想在销售中使用插入命令并更新余额表。我能做些什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 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
CommandType
是一个枚举,它告诉CommandText
中的值是什么:查询、表名称等。您不能将查询文本放在那里。
相反,您应该执行以下任一操作(从最好到最差):
将两个命令放入服务器端的存储过程中并调用该存储过程。这将是一个更好的方法。
将
UPDATE
命令附加到INSERT
命令。创建
ADO.Command
的另一个实例,并在同一事务中再次运行它。CommandType
is an enum which tells what a value inCommandText
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):
Put both commands into a stored proc on server side and call the stored proc. This would be a better way.
Append the
UPDATE
command to theINSERT
command.Create another instance of
ADO.Command
and run it again in the same transaction.您可以使用
;
将两者发送到服务器:这也是您获取所使用的身份值的方式,您可以在后面附加一个
;select @@identity
插入
。其他人介绍了为什么
CommandType
是错误的属性,因此我将其保留。You can send both to the server using
;
:This is also how you'd get back the identity value used, you'd append a
;select @@identity
after theinsert
.The others covered why
CommandType
is the wrong property to touch so I'll leave it at that.