0

I would like to have a button wherein I can browse an excel file. I want the data inside to be inserted to my mysql database. I already have a button and it can already browse and select a file. I am having problems when it comes to inserting the data which is inside the excel to the mysql database.

 Dim fname As String = label1.Text
    Try
        If label1.Text = "" Then

        Else

            ' Code to Import from Excel in to database.
            Dim dbFileName As String = fname
            Dim insertSql As String = "INSERT INTO tbl1 SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;HDR=yes;Database=" + dbFileName + "',[Sheet1$])"

            MySqlCmd = New MySqlCommand
            MySqlCmd.Connection = Myconnect
            MySqlCmd.CommandText = insertSql
            MySqlCmd.ExecuteNonQuery()

            MsgBox("import successful")
        End If

    Catch ex As Exception

    End Try

This is what i have searched and tried so far but it the inserting part is not working.

6
  • First you will need to read it into your app, right? Commented Dec 16, 2015 at 2:45
  • Sorry, What do you mean by reading it into my app? Thanks Commented Dec 16, 2015 at 2:47
  • 1
    I am having problems... show the code you are using and exactly what the problems are. We have no where to start with that vague description Commented Dec 16, 2015 at 2:52
  • I have just edited my post. I included the code Commented Dec 16, 2015 at 3:01
  • The code you have is just not going to work. See my answer and take it one step at a time Commented Dec 16, 2015 at 16:31

2 Answers 2

1

This is the answer that should point you into right direction.

Your goal: get data from Excel sheet and load it into MySql DB

Part 1. Open File dialog

Answer: You only need to get a file name out of it once file is selected

Part 2. Reading File

Answer: Use Microsof.Ace.OleDb.vXXX provider to read data from excel. Using this provider you can work with excel just like you would do with a database. i.e. you would open connection [using file name you obtained in step 1], and use command object to open dataReader or fill dataSet.

Part 3. Saving to MySql

Answer: you will iterate reader or data table rows and insert data into MySql. You will use MySql data provider for .NET

Additional comments.

There are few techniques that you can use to transfer data. The cheapest one would be using data reader on Excel and ExecuteNonQuery with INSERT on MySql. You can use data adapter and query builder on MySql side to load a schema to data set and generate insert and update SQL for the data adapter. Then, you will insert records into data table and call .Update(). Ok, really, there are few combinations of how it can be done. But I would stick with reader on Excel/command.ExecuteNonQuery on MySql

Sign up to request clarification or add additional context in comments.

Comments

1

Thanks for all your comments and answers! I already got an answer, I hope this might help others!

Try

        Dim conn As New Connection
        Dim rset As New Recordset
        Dim buff0 As String
        Dim buff1 As String
        Dim buff2 As String
        Dim buff3 As String
        Dim buff4 As String
        Dim buff5 As String

        conn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DriverId=790;Dbq=" & TextBox1.Text & ";" 'Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=d:\temp\test.xls;"
        conn.Open()
        rset.Open("select * from [Sheet1$]", conn, CursorTypeEnum.adOpenForwardOnly)
        Do Until rset.EOF
            buff0 = rset(0).Value
            buff1 = rset(1).Value
            buff2 = rset(2).Value
            buff3 = rset(3).Value
            buff4 = rset(4).Value
            buff5 = rset(5).Value

            MySqlCmd = New MySqlCommand
            MySqlCmd.Connection = Myconnect
            MySqlCmd.CommandText = "INSERT INTO tbl1  VALUES('" & buff0 & "','" & buff1 & "','" & buff2 & "','" & buff3 & "','" & buff4 & "','" & buff5 & "')"
            MySqlCmd.ExecuteNonQuery()
            rset.MoveNext()
        Loop
        MsgBox("Import Successful!", MsgBoxStyle.Information, Title:="SOMS")

    Catch ex As Exception

        MsgBox("Import Unsuccessful!", MsgBoxStyle.Critical, Title:="SOMS")

    End Try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.