How to import mysql tables with data through odbc and vb.net

0

i am working with data that needs updated from a third party source. My problem is that i need to allow my program to automatically do this from an odbc connection. i get the error;

it is a very large file but it will fit into a string. The sql query will create tables and data. it is from a data dump.


Dim oOpen As New OpenFileDialog
Dim oSqlCreate As New MySqlConnections
dim oSqlConn As Odbc.OdbcConnection
Dim osqlcmd As New Odbc.OdbcCommand
Dim sr As StreamReader
Dim sql As String
Dim newFile As String
    oSqlConn = oSqlCreate.OpenEstConn()
    osqlcmd.Connection = oSqlConn
    oOpen.DefaultExt = ".sql"
    If oOpen.ShowDialog() <> DialogResult.OK Then Exit Sub
        Try
            sr = New StreamReader(oOpen.FileName)
            newFile = sr.ReadToEnd()
            sql = "drop database VCDB"
            osqlcmd.CommandText = sql
            osqlcmd.ExecuteNonQuery()
            sql = "Create database VCDB"
            osqlcmd.CommandText = sql
            osqlcmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox("Drop or Create failed with the following error:" & ex.Message)
            Exit Sub
        End Try



newFile = "use VCDB; " & newFile
osqlcmd.CommandText = newFile
'error below
osqlcmd.ExecuteNonQuery()


GetVersions()

MsgBox("Import Complete.")

Error code:

System.InvalidOperationException HResult=0x80131509 Message=The connection has been disabled. Source=System.Data Inner Exception 1: OdbcException: ERROR [08S01] [MySQL][ODBC 8.0(a) Driver][mysqld-8.0.19]MySQL server has gone away
mysql
sql
vb.net
asked on Stack Overflow Apr 3, 2020 by zapdbf • edited Apr 3, 2020 by t1f

1 Answer

0

I see it happening more like this.

Private Sub DropAndCreateDB()
    Using cn As New OdbcConnection("Your connection string"),
            cmd As New OdbcCommand()
        cmd.Connection = cn
        cmd.CommandText = "Drop Database VCDB"
        cn.Open()
        cmd.ExecuteNonQuery()
        cmd.CommandText = "Create database VCDB"
        cmd.ExecuteNonQuery()
    End Using
    CreateTablesAndAddData()
End Sub

Private Sub CreateTablesAndAddData()
    Using cn As New OdbcConnection("Your connection string"),
            cmd As New OdbcCommand("use VCDB; " & ReadFile(), cn)
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

Private Function ReadFile() As String
    Dim oOpen As New OpenFileDialog
    oOpen.DefaultExt = ".sql"
    Return File.ReadAllText(oOpen.FileName)
End Function

If there are statements that are not to the liking of ODBC in the file then that is another issue and we would need to see some sample content of the file.

answered on Stack Overflow Apr 5, 2020 by Mary

User contributions licensed under CC BY-SA 3.0