visual basic在一个mysql数据库中插入多个表

时间:2014-10-29 11:32:55

标签: mysql visual-studio-express

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

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

它有点乱,但我不是一个好的编码器,所以它只是发现了各种有用的东西。

1 个答案:

答案 0 :(得分:1)

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