Click here to Skip to main content
13,094,983 members (83,088 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


45 bookmarked
Posted 9 Dec 2008

Simple Excel 2007 Data Loader to SQL 2008 Server

, 9 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Simple Excel data loader to SQL Server using SQlBulkCopy


This is a very simple Excel 2007 data loader to SQL 2008 database. I am using SqlBulkCopy class to make data load fast. (10K record loaded in less than 3s).

One catch: You will need existing table in SQL DB with matching columns. I skipped dynamic table creation to keep the code simple.

Using the Code

This is the most basic version, without error checking to make the code clear. Below is the completed code behind the form.

Button 1 - Opens the Excel file.

Button 2 - Uploads data to SQL Server.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;

namespace SimpleExcelLoader
    public partial class Form1 : Form
        string filename="";
        public Form1()
      private void button1_Click(object sender, EventArgs e)
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Excel 2007 files 
			(*.xlsx)|*.xlsx|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
               filename = openFileDialog1.FileName;
               textBox1.Text = filename;
               button2.Enabled = true;
               toolStripStatusLabel1.Text = "File Selected. 
				Enter DB Info and click Upload Data";
        private void button2_Click(object sender, EventArgs e)
            string server = textBox2.Text;
            string db = textBox3.Text;
            string user = textBox4.Text;
            string pass = textBox5.Text;
            string tableName=textBox6.Text;
            string serverConnectionString="Server="+server+";
            //step 1
            // Load Excel data into DataTable
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source=" + filename + ";Extended Properties='Excel 12.0;HDR=YES;'";
            string strSQL = "SELECT * FROM [Sheet1$]";
            OleDbConnection excelConnection = new OleDbConnection(connectionString);
            excelConnection.Open(); // This code will open excel file.
            OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
            // create data table
            DataTable dTable = new DataTable();
            //fill table with Excel data
            //step 2 
            //connect to server
            using (SqlConnection destinationConnection =
                   new SqlConnection(serverConnectionString))
                    using (SqlBulkCopy bulkCopy =
                            new SqlBulkCopy(destinationConnection))
                        //Destination Table must match columns in Excel sheet
                        bulkCopy.DestinationTableName = tableName;
                            // Write from the source to the destination.
                            toolStripStatusLabel1.Text="Data Uploaded";
                        catch (Exception ex)
                     destinationConnection.Close(); }}
                // dispose used objects

Points of Interest

The code was tested with Excel 2007 and SQL Server 2008.


  • 9th December, 2008: Initial post


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


About the Author

Inga Bemman
Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
phoohtoo26-Dec-12 22:59
memberphoohtoo26-Dec-12 22:59 
QuestionExcel to sql conversion Pin
netdevny4-Dec-12 10:32
membernetdevny4-Dec-12 10:32 
Generalxlsx 2 sql Pin
Bernie Su16-Jun-09 7:05
memberBernie Su16-Jun-09 7:05 
GeneralSSIS... Pin
Tarabanko Yury9-Dec-08 14:26
memberTarabanko Yury9-Dec-08 14:26 
GeneralRe: SSIS... Pin
Inga Bemman9-Dec-08 23:53
memberInga Bemman9-Dec-08 23:53 
GeneralRe: SSIS... Pin
Tarabanko Yury10-Dec-08 3:54
memberTarabanko Yury10-Dec-08 3:54 
GeneralWell... Pin
JayDial9-Dec-08 14:07
memberJayDial9-Dec-08 14:07 
GeneralRe: Well... Pin
Inga Bemman9-Dec-08 23:49
memberInga Bemman9-Dec-08 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 9 Dec 2008
Article Copyright 2008 by Inga Bemman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid