Sql server 将演绎更新为sql vb不起作用
我想通过vb将股票的演绎更新为sql,它不会显示错误,但不会更新。 我希望sql中的music_副本更新为txtstock.text-txtquan.txt的新结果,但它显示已成功购买,但当我刷新数据时,它显示与以前相同Sql server 将演绎更新为sql vb不起作用,sql-server,vb.net,Sql Server,Vb.net,我想通过vb将股票的演绎更新为sql,它不会显示错误,但不会更新。 我希望sql中的music_副本更新为txtstock.text-txtquan.txt的新结果,但它显示已成功购买,但当我刷新数据时,它显示与以前相同 Private Sub btnwalkinadd_Click(sender As Object, e As EventArgs) Handles btnwalkinadd.Click Dim con As New SqlConnection Dim cm
Private Sub btnwalkinadd_Click(sender As Object, e As EventArgs) Handles btnwalkinadd.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim rd As SqlDataReader
txtcal.Text = txtstock.Text - txtquan.Text 'This only for my reference
Try
If txtcal.Text >= 0 Then
con.ConnectionString = "Server=KAVIER;Database=vb;Trusted_Connection=True;"
con.Open()
cmd.Connection = con
Dim updatecmd As String = "UPDATE music SET music_copies = ' " & txtstock.Text - txtquan.Text & "' WHERE mus_id = '" & txtid.Text & "'"
updatecmd = cmd.ExecuteNonQuery() 'execute the command
MsgBox("Successfully Purchased")
con.Close()
Else
MsgBox("Insufficient Stock")
End If
txttotal.Clear()
txtartist.Clear()
txtprice.Clear()
txtid.Clear()
txtgenre.Clear()
txtalbum.Clear()
txtcal.Clear()
txtstock.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
使用此代码
Private Sub btnwalkinadd_Click(sender As Object, e As EventArgs) Handles btnwalkinadd.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim rd As SqlDataReader
txtcal.Text = txtstock.Text - txtquan.Text 'This only for my reference
Try
If txtcal.Text >= 0 Then
con.ConnectionString = "Server=KAVIER;Database=vb;Trusted_Connection=True;"
con.Open()
cmd.Connection = con
cmd.commandtext = "UPDATE music SET music_copies = ' " & txtstock.Text - txtquan.Text & "' WHERE mus_id = '" & txtid.Text & "'"
cmd.ExecuteNonQuery() 'execute the command
MsgBox("Successfully Purchased")
con.Close()
Else
MsgBox("Insufficient Stock")
End If
txttotal.Clear()
txtartist.Clear()
txtprice.Clear()
txtid.Clear()
txtgenre.Clear()
txtalbum.Clear()
txtcal.Clear()
txtstock.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
这是您的命令文本的问题。您将命令文本指定为updatecmd,并将其定义为字符串,但执行了sql commadn cmd,该命令没有要执行的命令文本。您应该将要执行的命令初始化为命令文本。很抱歉,您的代码充满了缺陷。连接需要一个连接字符串。该命令需要连接和命令文本。最后需要处理连接、命令和读取器。你从不使用阅读器。您的查询已为SQL注入打开。你需要打开选项strict。好的。。。我设法添加了你提到的内容,最后它成功了=)谢谢=)谢谢=))我设法用上面的评论修复了它