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.
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. ie 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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.