如何检查vb.net中是否有两个或多个查询分别执行

qvk1mo1f  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(275)

我有两个程序运行顺利命名 update_students 以及 insert_payment .

Sub insert_payment() 

con.open
Using cmd as New MySQLCommand  
  With cmd
    .Connection = con
    .CommandType = CommanType.Text
    .CommandText = "INSERT INTO tblpayment(stud_id,payment_amount) VALUES (@stud_id,@payment_amount);"
    .Parameters.AddWithValue("@stud_id", txtid.text)
    .Parameters.AddWithValue("@payment_amount", txtpay.text)
    .ExecuteNonQuery()
    End With
con.Close()
End Using

End Sub

Sub update_students() 

con.open
Using cmd as New MySQLCommand  
  With cmd
    .Connection = con
    .CommandType = CommanType.Text
    .CommandText = "UPDATE tbl_stud SET tuition_status=@tuition_status,address_school=@address_school;"
    .Parameters.AddWithValue("@ID", txtid.text)
    .Parameters.AddWithValue("@tuition_status", txtns.text)
    .Parameters.AddWithValue("@address_school", txtadd.text)
    .ExecuteNonQuery()
    End With
con.Close()
End Using

End Sub

现在在addbuttonclick事件中,我调用了两个过程来执行。虽然我没有遇到错误。
我的问题是,我想知道这两个过程是否完全同时执行。如果第1个或第2个或两者都遇到错误,它应该通知,那么插入和更新都不会执行。
我这样做是因为我想通过同步插入和更新两个表来添加/更新它们。
对不起,我英语不好。我希望这里有人能帮我。
提前谢谢。

nwlqm0z1

nwlqm0z11#

这是我做的,而且很有效。
我担心如果这个代码不干净,不能使用。有什么建议吗?

Public Sub bTrans()
        
        con.Open()
        Dim trans As MySqlTransaction
        trans = con.BeginTransaction()
        Try
            Using cmd As New MySqlCommand
                With cmd
                    .Connection = con
                    .CommandType = CommandType.Text
                    .CommandText = "INSERT INTO tblinventory (itemcode,stocks,unit,unitprice,supplierprice,additional,postingdate) values (@itemcode,@stocks,@unit,@unitprice,@supplierprice,@additional,@postingdate);"
                    .Parameters.AddWithValue("@itemcode", txtCode.Text)
                    .Parameters.AddWithValue("@stocks", txtNew.Text)
                    .Parameters.AddWithValue("@unit", cbUnit.Text)
                    .Parameters.AddWithValue("@unitprice", txtTotalPrice.Text)
                    .Parameters.AddWithValue("@supplierprice", txtOld.Text)
                    .Parameters.AddWithValue("@additional", txtAddition.Text)
                    .Parameters.AddWithValue("@postingdate", Now())
                    .ExecuteNonQuery()
                End With
            End Using
            Using cmd As New MySqlCommand
                With cmd
                    .Connection = con
                    .Transaction = trans
                    .CommandType = CommandType.Text
                    .CommandText = "UPDATE tblMedicine SET AddedStocks=@AddedStocks where ItemCode=@ItemCode ;"
                    .Parameters.AddWithValue("@ItemCode", txtCode.Text)
                    .Parameters.AddWithValue("@AddedStocks", 1)
                    .ExecuteNonQuery()
                End With
            End Using
            Using cmd As New MySqlCommand
                With cmd
                    .Connection = con
                    .CommandType = CommandType.Text
                    .CommandText = "INSERT INTO tblstocktransaction (itemcode,acquiredate,quantity,unit,unitprice,expirydate,supplier,postingdate,supplierprice,additionalprice) values (@itemcode,@acquiredate,@quantity,@unit,@unitprice,@expirydate,@supplier,@postingdate,@supplierprice,@additionalprice);"
                    .Parameters.AddWithValue("@itemcode", txtCode.Text)
                    .Parameters.AddWithValue("@acquiredate", dtAcq.Value)
                    .Parameters.AddWithValue("@quantity", txtNew.Text)
                    .Parameters.AddWithValue("@unit", cbUnit.Text)
                    .Parameters.AddWithValue("@unitprice", txtTotalPrice.Text)
                    .Parameters.AddWithValue("@expirydate", dtExp.Value)
                    .Parameters.AddWithValue("@supplier", cbSupplier.Text)
                    .Parameters.AddWithValue("@postingdate", dtPost.Value)
                    .Parameters.AddWithValue("@supplierprice", txtOld.Text)
                    .Parameters.AddWithValue("@additionalprice", txtAddition.Text)
                    .ExecuteNonQuery()
                End With
            End Using
            trans.Commit()
            MsgBox("Succcessfully added!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pharmacy Inventory and Management System")

        Catch e As Exception
            Try
                trans.Rollback()
            Catch ex As MySqlException
                If Not trans.Connection Is Nothing Then
                    MsgBox("Transaction failed due to wrong entry! Please try again.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pharmacy Inventory and Management System")
                End If
            End Try
            MsgBox("Transaction failed due to wrong entry! Please try again.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pharmacy Inventory and Management System")
        Finally
            con.Close()
            trans.Dispose()
        End Try

    End Sub

相关问题