如何在VB中同时执行两个查询? (MySQL的)

时间:2013-11-26 08:03:56

标签: mysql vb.net

同时执行两个查询的最有效方法是什么? 我想在单击提交按钮时同时执行两个INSERT sql查询(不同的表)。有可能吗?

以下是我的所作所为:

    Dim conn As New MySqlConnection("Server = localhost; user id = root;password = ; database = ddap_hr")
    Dim sqlQuery1 As String = "INSERT INTO applicants VALUES ( '" & lblID.Text & "' , '" & txtLName.Text & "','" & txtFName.Text & "','" & txtMName.Text & "','" & cmboGender.Text & "','" & mtxtAge.Text & "','" & dtpBdate.Value & "','" & cmboStatus.Text & "','" & txtSSS.Text & "','" & txtTin.Text & "','" & txtReligion.Text & "','" & txtAddress.Text & "','" & mtxtContactNum.Text & "','" & txtEmail.Text & "')"
    Dim sqlQuery2 As String = "INSERT INTO appli_idgen(Lzero) VALUES ('" & lblNum.Text & "')"
    Dim cmd1 As New MySqlCommand(sqlQuery1)
    Dim cmd2 As New MySqlCommand(sqlQuery2)
    Dim rdr As MySqlDataReader

    Dim ConfirmMsg = MessageBox.Show("Are all the datas correct?" & Environment.NewLine & "   • Last Name:  " & txtLName.Text & Environment.NewLine & "   • First Name:  " & txtFName.Text & Environment.NewLine & "   • Middle Name:  " & txtMName.Text & Environment.NewLine & "   • Gender:  " & cmboGender.Text & Environment.NewLine & "   • Age:  " & mtxtAge.Text & Environment.NewLine & "   • Date of Birth:  " & dtpBdate.Text & Environment.NewLine & "   • Status:  " & cmboStatus.Text & Environment.NewLine & "   • SSS:  " & txtSSS.Text & Environment.NewLine & "   • TIN:  " & txtTin.Text & Environment.NewLine & "   • Religion:  " & txtReligion.Text & Environment.NewLine & "   • Address:  " & txtAddress.Text & Environment.NewLine & "   • Contact Number:  " & mtxtContactNum.Text & Environment.NewLine & "   • E-mail:  " & txtEmail.Text & Environment.NewLine, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, False)

    If ConfirmMsg = MsgBoxResult.Yes Then
        Try
            Try
                cmd1.Connection = conn
                conn.Open()
                cmd1.ExecuteNonQuery()
                rdr = cmd.ExecuteReader
                rdr.Read()
            Catch ex1 As MySqlException
                MsgBox(ex1.Message.ToString)
            Finally
                conn.Close()
            End Try

            Try
                cmd2.Connection = conn
                conn.Open()
                cmd2.ExecuteNonQuery()
                rdr = cmd.ExecuteReader
                rdr.Read()
            Catch ex2 As MySqlException
                MsgBox(ex2.Message.ToString)
            Finally
                conn.Close()
            End Try
        Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
        Finally
            Dim addAnother = MessageBox.Show("Do you want to add another applicant?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, False)
            If addAnother = MsgBoxResult.No Then
               Me.Close()
               Main_home.Refresh()
            End If
        End Try

    End If

我想尽可能减少代码行。我需要你的帮助。顺便说一句我正在使用MySql。对不起因为我是VB的新手。提前谢谢。

4 个答案:

答案 0 :(得分:2)

要回答这个问题,你可以尝试启动两个线程,一个接一个地运行一个线程。这实际上是让它们同时运行。

Private Sub Button36_Click(sender As Object, e As EventArgs) Handles Button36.Click
    Dim t1 As New Threading.Thread(AddressOf Insert1)
    Dim t2 As New Threading.Thread(AddressOf Insert2)
    t1.Start()
    t2.Start()
End Sub

Private Sub Insert1()
    'do insert here
End Sub

Private Sub Insert2()
    'do insert here
End Sub

我怀疑你是否会获得任何性能提升。

答案 1 :(得分:2)

如果你向我们展示了你尝试过的东西,那将会有所帮助。

按照目前的情况,你可以创建一个子程序,它将执行Nonquery到MySQL,然后在任何触发器(按钮,复选框等)上调用子TWICE:

注意:这需要您知道如何使用.NET的MySQL连接器您可以在here上查找更多信息

   Public Sub MyNonQuery(ByVal SQCommand as String)
      Dim conn As New MySqlConnection("server=your server ip; port=the server port; uid='your user id';password ='your password';")

      Dim SQLCMD as new MySqlCommand(SQCommand, conn)

      conn.open()
      SQLCMD.ExecuteNonQuery()
      conn.close()
    End Sub

现在你可以用以下方式使用这个子:

  1. 拨打两次

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        MyNonQuery("Insert into db.tbl1 values(...);")
        MyNonQuery("Insert into db.tbl2 values(...);")
    
     End Sub
    
  2. 在一次通话中,发送两个SQL命令

      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        MyNonQuery("Insert into db.tbl1 values(...); Insert into db.tbl2 values(...);")
    
    
     End Sub
    
  3. 记住要正确清理用户的数据,以防止SQL注入攻击。最后,我建议你研究Stored Procedures

答案 2 :(得分:0)

我迟了,但是请尝试一下,希望对您有帮助

Public Sub CreateCommand(ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim query As String = "INSERT INTO tbl_example ..."
        Dim query2 As String = "INSERT INTO tbl_add ..." 
        Dim QueryString As String = String.Concat(query,";",query2)
        Dim command As New SqlCommand(QueryString, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using 
End Sub

答案 3 :(得分:-1)

这是基于网络还是基于Windows?

Windows窗体 - 按照上述注释

进行操作
'Web forms
Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
    'do insert 1 here
    'do insert 2 here
End Sub