2013-11-26 102 views
-2

什麼是同時執行兩個查詢的最有效方法? 當我點擊提交按鈕時,我想同時執行兩個INSERT sql查詢(不同的表格)。可能嗎?如何在VB中同時執行兩個查詢? (MySql)

這裏就是我所做的:

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 

我想減少儘可能多的SA可能的代碼行。我需要你的幫助。 btw我使用MySql。對不起,因爲我是VB新手。提前致謝。

+0

你的問題不清楚,請重新說明,這一次更精確。 – Chelseawillrecover

+0

感謝您的建議。我想同時執行兩個不同的查詢。 – Potato

+0

當你點擊提交按鈕時,你有沒有觸發代碼? – Chelseawillrecover

回答

2

這將有助於如果你告訴我們你嘗試一下,朋友。

既然這樣,你可以創建一個子程序將執行Nonquery到MySQL,然後調用子兩次任何觸發(按鈕,複選框等):

注:這需要你知道如何使用

  1. C:MySQL的連接器,用於.NET你可以尋找更多的對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 
    

    現在,您可以通過以下方式(S)使用此子ALL兩次

    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 
    

記住正確清潔來自用戶的數據,雖然,以防止SQL注入攻擊。最終,我建議你看看研究Stored Procedures

+0

非常感謝。 – Potato

+0

先生,我得到了這個問題。該查詢被執行兩次。因爲這個,我得到了這個重複的主要錯誤。請幫助。謝謝。我的代碼:[鏈接](http://puu.sh/5vyRN.txt) – Potato

+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 

不管你是否會得到任何性能改善,我都懷疑。

+0

我會試試這個。謝謝。 – Potato

-1

這是基於Web還是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 
+0

這個答案沒有添加任何問題,朋友。也許你想編輯它以包含新的有用的細節? –