vb.net 如何在VB中同时执行两个查询?(MySql)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20211503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute two queries at the same time in VB? (MySql)
提问by Potato
What's the most efficient way to execute two queries at the same time? I want to execute two INSERT sql queries (different tables) simultaneously when I click the submit button. is it possible?
同时执行两个查询的最有效方法是什么?当我单击提交按钮时,我想同时执行两个 INSERT sql 查询(不同的表)。是否可以?
Here's what I did:
这是我所做的:
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
I want to reduce the line of codes as much sa possible. I need your help. btw I'm using MySql. Sorry cause I'm new in VB. Thanks in advance.
我想尽可能地减少代码行。我需要你的帮助。顺便说一句,我正在使用 MySql。抱歉,因为我是 VB 新手。提前致谢。
回答by Matt Wilko
To answer the question you could try and start two threads running one immeddiately after the other. This is effectively having them run at the same time.
要回答这个问题,您可以尝试启动两个线程,一个接一个地立即运行。这实际上是让它们同时运行。
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
Whether you will get any performance improvement I doubt.
我怀疑您是否会获得任何性能改进。
回答by Malcolm Salvador
It would help if you've shown us what you've tried, friend.
朋友,如果您向我们展示了您尝试过的方法,那将会有所帮助。
As it stands, you can create a subroutine which will execute Nonquery to MySQL then CALL that sub TWICE on any trigger(button, checkbox, etc.):
就目前而言,您可以创建一个子例程,该子例程将对 MySQL 执行 Nonquery,然后在任何触发器(按钮、复选框等)上调用该子 TWICE:
Note: this requires you know how to use the MySQL connector for .NET You can look for more on that here
注意:这要求您知道如何将 MySQL 连接器用于 .NET 您可以在此处查找更多信息
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
Now you can use this sub in the following manner(s):
现在您可以通过以下方式使用此子程序:
CALL it twice
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 SubOn a single call, Send TWO SQL commands
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
调用它两次
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一次调用,发送两个 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
Remember to properly Clean the data from the user though, to prevent SQL Injection attacks. Ultimately I would suggest you look into studying Stored Procedures
记住要正确清理用户的数据,以防止 SQL 注入攻击。最终我建议你研究一下存储过程
回答by VivRichards
Is this web or windows based?
这是基于 web 还是 windows 的?
Windows forms - do as above comment
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

