更新扣除到sql vb不起作用

时间:2014-12-20 11:09:33

标签: sql-server vb.net

我想通过vb更新库存到sql的扣除,它不显示错误但它不会更新。 我希望sql中的music_copies更新了txtstock.text - txtquan.txt的新结果,但它显示成功购买但是当我刷新数据时,它显示与之前相同

  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

1 个答案:

答案 0 :(得分:0)

使用此代码。

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,该命令没有要执行的命令文本。您应该初始化要作为命令文本执行的命令。