I'm developing a very simple VB app in Visual Basic 2008, where i have a textbox being dynamically updated with a value every second, i need this value to be inserted into a mySQL DB, i have made the connection ok but not too sure how to structure the VB code around the SQL Query??

Here's my code so far...

Public Class MainForm

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.Text = EthernetIPforSLCMicro1.ReadAny("F18:0")
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As New Odbc.OdbcConnection
        Dim connString As String = "DRIVER={MySQL ODBC 5.1 Driver};" _
                                & "SERVER=localhost;" _
                                & "PORT=3306;" _
                                & "DATABASE=test;" _
                                & "UID=root;PWD=; OPTION =3;"

        If conn.State = ConnectionState.Open Then conn.Close()

        With conn
            .ConnectionString = connString
            .Open()
        End With
    End Sub
End Class

Any help, very much appreciated!!!

I am not good with VB.NET, so please fix any errors you might encounter. Also, substitute your SQL statement and connection string.

'Declare variables
Dim connString as String
Dim conn as OdbcConnection
Dim cmd as OdbcCommand
Dim prm as OdbcParameter

Private Sub MainForm_Load(ByVal sender as Object, ByVal e as EventArgs)
Handles MyBase.Load
    connString = "[your_connection_string]"
    conn = New OdbcConnection(connString)
    conn.Open()
    cmd = conn.CreateCommand()
    cmd.CommandText = "insert into table1 (column1) values (?)"
    prm = cmd.CreateParameter()
    prm.Type = OdbcType.VarChar   'set correct type here
    prm.Size = 10                 'set correct size here
End Sub

Private Sub MainForm_Unload(ByVal sender as Object, ByVal e as EventArgs)
Handles MyBase.Unload
    cmd.Dispose()
    conn.Close()
    conn.Dispose()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
    Dim s as String
    s = EthernetIPforSLCMicro1.ReadAny("F18:0")
    TextBox1.Text = s
    
    'Set parameter value and execute the command
    prm.Value = s
    cmd.ExecuteNonQuery()
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.