gpt4 book ai didi

mysql - visual basic插入一个mysql数据库上的多个表

转载 作者:行者123 更新时间:2023-11-29 00:08:00 25 4
gpt4 key购买 nike

你好,我想知道是否可以通过单击按钮将相同的数据发送到一个数据库但多个表。因为在我目前的情况下,我只将数据输入到两者中的第一个。这是所述按钮的完整代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MsgBox("Meld enige gebreken bij je leraar samen de naam van de computer (deze staat op de stikker die op het apparaat te vinden is)")

Dim myHost As String = Dns.GetHostName
Dim ipEntry As IPHostEntry = Dns.GetHostEntry(myHost)
Dim ip As String = ""
Dim nee As String
Dim user As String
nee = "nee"

dbconn = New MySqlConnection("Data Source=172.17.80.153;user id=connectinguser;password=123456;database=vbtest;")
Try
dbconn.Open()
Catch ex As Exception
MsgBox("Error in connection, please check Database and connection server.")
End Try

For Each tmpIpAddress As IPAddress In ipEntry.AddressList
If tmpIpAddress.AddressFamily = Sockets.AddressFamily.InterNetwork Then
Dim ipAddress As String = tmpIpAddress.ToString
ip = ipAddress
Exit For
End If
Next



If ip = "" Then
Throw New Exception("No 10. IP found!")
End If

If TypeOf My.User.CurrentPrincipal Is System.Security.Principal.WindowsPrincipal Then
' The application is using Windows authentication.
' The name format is DOMAIN\USERNAME.
Dim parts() As String = Split(My.User.Name, "\")
Dim username As String = parts(1)

Dim uname As String = My.User.Name

uname = uname.Remove(0, 10)
user = uname

End If

sql = "INSERT INTO tbl_nee(Gebruikersnaam,Computernaam,IPAddress, Keuze) VALUES('" & user & "', '" & My.Computer.Name & "', '" & ip & "', '" & nee & "' )"

sql = "INSERT INTO tbl_alles(Gebruikersnaam,Computernaam,IPAddress, Keuze) VALUES('" & user & "', '" & My.Computer.Name & "', '" & ip & "', '" & nee & "' )"



Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader()
dbread.Close()
Catch ex As Exception
MsgBox("Error in saving to Database. Error is :" & ex.Message)
dbread.Close()
Exit Sub
End Try

Me.Close()
End Sub

它有点乱,但我不是一个好的编码员,所以它只是找到了让它工作的各种东西。

最佳答案

Dim sql1 As String = ""
Dim sql2 As String = ""

sql1 = "INSERT INTO tbl_nee(Gebruikersnaam,Computernaam,IPAddress, Keuze) VALUES('" & user & "', '" & My.Computer.Name & "', '" & ip & "', '" & nee & "' )"

sql2 = "INSERT INTO tbl_alles(Gebruikersnaam,Computernaam,IPAddress, Keuze) VALUES('" & user & "', '" & My.Computer.Name & "', '" & ip & "', '" & nee & "' )"

Dim sql = String.Concat(sql1, ";", sql2)


Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader()
dbread.Close()
Catch ex As Exception
MsgBox("Error in saving to Database. Error is :" & ex.Message)
dbread.Close()
Exit Sub
End Try

关于mysql - visual basic插入一个mysql数据库上的多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26629249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com