Search code examples
mysqlvisual-studio-express

visual basic inserting into multiple tables on one mysql database


hello I wanted to know if it is possible to get one click on a button to send the same data to one database but multiple tables. because in my current situation i only get the data in to the first of the two. here is the entire code of said button:

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

it is a bit messy but i'm not a good coder so it just all kinds of things that is found to make it work.


Solution

  • 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