Transfer data from Excel to MS SQL Server database

Posted on 2011-10-06
Last Modified: 2012-05-12
I'm trying to find on the web any good example for moving data from excel to SQL Server..
For this purpose I allready use SQL BulkCopy but what I'm interested in how to validate data types between source (excel worksheet) and destination table (on SQL Server database)...
Also, i'm get stuck on how to provide end users to manually mapp columns between 2 datasources using sqlBulcColumnMappings...
Link, code or some constructive advice would be very much appreciated...
Thanks in advance
Question by:alsam
    LVL 83

    Expert Comment

    LVL 1

    Author Comment

    Thanks, but I allready have seen this article before...anyway thanks....
    Regarding this part
     ' The column order in the source doesn't match the order
                    ' in the destination, so ColumnMappings must be defined.
                    bulkCopy.ColumnMappings.Add("ProductID", "ProdID")
                    bulkCopy.ColumnMappings.Add("Name", "ProdName")
                    bulkCopy.ColumnMappings.Add("ProductNumber", "ProdNum")

    Do you maybe have any advice how to make this "user frendly"...I dont want to fix those columns in code but I want to let end user to make this mapping as he/she wants to... Something similar like in Import/Export Wizard in SQL Server...

    Also, do you have any suggesstion about data type validation between 2 sources (excel vs. sql server db table)
    LVL 83

    Accepted Solution

    To allow user to do the mapping, you can design a form with may be a grid with two columns each populated as a combobox with column names from either side. Then you can loop through this grid and add the mappings like

    For i As Integer = 0 to dgv.Rows.Count - 1
         bulkCopy.ColumnMappings.Add(dgv.Rows(i).Cells(0), dgv.Rows(i).Cells(1))

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now