Import an excel spread sheet into SQL Server database and build the SQL Server table on the fly

Posted on 2014-12-28
Last Modified: 2016-02-18
I’m not a C# or VB developer can someone help me with C# or VB code that will import an excel spread sheet into SQL Server database and build the SQL Server table on the fly.

I need to develop an ETL Process that needs to load an excel spread sheet into a SQL Server Database. The amount of columns will be dynamic, but I do know that the name of the first three columns and the rest of the columns will be integer values. For example the spread sheet structure will be like so

My first idea was to use a distributed query like below, but this is not allowed in current environment that I’m in.
  'Data Source=C:\excel-sql-server.xlsx;Extended Properties=Excel 12.0')...[Sheet1$]

So I think my best option is script task and use C# or vb code that will do the same thing as the SQL code above.
Question by:kbennett13

Expert Comment

by:Ali HND
ID: 40520618
I think this link is useful for you:
How to import data from Excel to SQL Server

Accepted Solution

Utkarsh Kulkarni earned 500 total points
ID: 40537456
More simple code you can get at

Or Code snippet like
            string sqlConnectionString = string.Format("Data Source=.\SQLEXPRESS;Initial Catalog=DbName;" + "Integrated Security=SSPI; User Instance=False");
            string excelConnectionString = string.Format("provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\user\\ExcelFileName.xlsx; Extended Properties ='Excel 12.0 xml; HDR=YES;IMEX=1'");
            using (SqlConnection SQLconnection =
                new SqlConnection(sqlConnectionString))
            using (OleDbConnection connection =
                new OleDbConnection(excelConnectionString))
                OleDbCommand command = new OleDbCommand
                ("SELECT * INTO dbo.newTable FROM [SheetName$];", connection);

                using (OleDbDataReader dr = command.ExecuteReader())

                using (SqlBulkCopy bulkcopy =
                    new SqlBulkCopy(SQLconnection))
                    bulkcopy.DestinationTableName = "dbo.newTable";
                    Console.WriteLine("The data has been exported from Excel to SQL.");

        catch (Exception ex)

Open in new window


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSSQL Speen Degradation 4 21
This query failed in sql 2014 5 29
Linked Server Issue with SQL2012 3 22
While in ##Table - Help 4 10
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

815 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

8 Experts available now in Live!

Get 1:1 Help Now