Start New Discussion within our Software Development Community

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!!!

First of all, I'm recommending that you download and install the MySQL Connector for .NET, instead of using ODBC. It's available from the MySQL site.

Then, for inserting data into the database will be practically the same as using a MS SQL database.

Imports Mysql.MySqlClient

Private Sub MainForm_Load(yadda yadda yadda)
   Dim connectionString As String = "<connectionstring>"
   Dim con As New MysqlConnection(connectionString)

   con.Open()

   Dim com As New MysqlCommand("INSERT INTO <table> (field1,field2) VALUES (value1,value2)", con)
   If com.ExecuteNonQuery() = 0 Then
      MessageBox.Show("No recoords stored at this time")
   End If
   con.Close()
End Sub
This article has been dead for over six months. Start a new discussion instead.