Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys
so I had this problem with a simple project that I build, I wanted to import more than one excel files into tables in the database.
I could only import one file to the table, so I want to import multiple excel files to multiple tables in the database. is anyone can help me ? I would really appreciate it

What I have tried:

here's the code that I use and it only works to import one file to one table, and what I need is to import more than one files into different tables in the database.


VB
Dim koneksi_excel As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & TextBox1.Text & "';Extended Properties=""Excel 12.0 Xml;HDR=YES""")
       koneksi_excel.Open()
       Dim query_excel As String = "Select * from [Sheet1$]"
       Dim cmd As OleDbCommand = New OleDbCommand(query_excel, koneksi_excel)
       Dim rd As OleDbDataReader
       rd = cmd.ExecuteReader()

       Dim koneksi As New SqlConnection()
       Dim koneksidatabase As String = "server=DESKTOP-KJQ8PNO\SQLEXPRESS;DATABASE=otto;Integrated Security=True"
       koneksi.ConnectionString = koneksidatabase
       koneksi.Open()

       Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(koneksi)
           bulkcopy.DestinationTableName = "DAFTAR1"
           bulkcopy.BulkCopyTimeout = 600
           bulkcopy.WriteToServer(rd)
           rd.Close()
           koneksi.Close()

           MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded")
           TextBox1.Text = ""
       End Using

   End Sub



Best regards

Nick
Posted
Updated 11-Mar-17 0:05am
v2
Comments
Michael_Davies 11-Mar-17 2:31am    
What does not work, it does work as you say it imports a file successfully.

Help us to help you, "it won't work" does not mean anything.

Click Improve question and give the details of what does not work, if it is just the need to import multiple files is into the same table or separate tables?
Member 13052135 11-Mar-17 4:21am    
hello Michael thanks for your answer
well the code only works to import one file to one table, so what I want to do with this is to import multiple files to separate tables.
so for example I have 3 excel files and I need to import these files into different tables in the database
Michael_Davies 11-Mar-17 4:52am    
So what you need to do is;

1. A sub that collects the file names to be imported then uses a loop to call 2. below with the file name and a table name.

2. A sub that accepts a filename and table name as string parameters that contains the code you have already shown using the parameters for the file name and table name.

Job done.
Member 13052135 11-Mar-17 5:01am    
I'll try to figure it out first
thanks man I really appreciate it

1 solution

Here is your code for part 2 of my comments adjusted to do the job of importing a single file, all you need to do is write the sub that collects the file names and call the sub below for each file in turn;

VB
Public Sub ImportExcelFielToTable(ExcelFile As String, TargetTable As String)
Dim koneksi_excel As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ExcelFile & "';Extended Properties=""Excel 12.0 Xml;HDR=YES""")
        koneksi_excel.Open()
        Dim query_excel As String = "Select * from [Sheet1$]"
        Dim cmd As OleDbCommand = New OleDbCommand(query_excel, koneksi_excel)
        Dim rd As OleDbDataReader
        rd = cmd.ExecuteReader()

        Using koneksi As New SqlConnection("server=DESKTOP-KJQ8PNO\SQLEXPRESS;DATABASE=otto;Integrated Security=True")
        koneksi.Open()

        Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(koneksi)
            bulkcopy.DestinationTableName = TargetTable
            bulkcopy.BulkCopyTimeout = 600
            bulkcopy.WriteToServer(rd)
            rd.Close()

            MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded")
            TextBox1.Text = ""
        End Using
        koneksi.Close()
      End Using
    End Sub
 
Share this answer
 
Comments
Member 13052135 12-Mar-17 5:27am    
I understand by write the sub to collects the file names.
I'm a newbie with vb
Michael_Davies 12-Mar-17 6:24am    
Currently you take a single file name into a textbox to import to a static table name. If you have multiple files you need to get the files you wish to import together in a list then batch process them one at a time with some naming convention for your table names.

How you gather the list is up to you, we are not privy to how you work with the files and there is more than one way to do it.

You could present the user with a checkbox list of XL files in a specified directory and let the user click the ones they want to import, you could also set up a watch on a directory and import then delete any XL file dropped into it. The list goes on.

For getting a list of files check out ; https://msdn.microsoft.com/en-us/library/ms127994(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900