Click here to Skip to main content
Click here to Skip to main content

Tagged as

Import MS Excel data to SQL Server table using C#

, 13 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Import MS Excel data to SQL Server table using C#.


If you already have data in MS Excel file, and want to migrate your MS Excel data to SQL Server table, follow the below steps:

Step 1: Let’s take an example to import data to SQL Server table. I am going to import student information data from an MS Excel sheet to the tStudent SQL table:

Step 2: Now design a tStudent table in SQL Server


Your MS Excel sheet and SQL table are ready, now it’s time to write C# code to import the Excel sheet into the tStudent table.

Step 3: Add these two namespaces in your class file:


Step 4: Add below method in your class file, you can call this method from any other class and pass the Excel file path:

public void importdatafromexcel(string excelfilepath)
    //declare variables - edit these based on your particular situation
    string ssqltable = "tdatamigrationtable";
    // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have
    string myexceldataquery = "select student,rollno,course from [sheet1$]";
        //create our connection strings
        string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath +
        ";extended properties=" + "\"excel 8.0;hdr=yes;\"";
        string ssqlconnectionstring = "server=mydatabaseservername;user
        id=dbuserid;password=dbuserpassword;database=databasename;connection reset=false";
        //execute a query to erase any previous data from our destination table
        string sclearsql = "delete from " + ssqltable;
        sqlconnection sqlconn = new sqlconnection(ssqlconnectionstring);
        sqlcommand sqlcmd = new sqlcommand(sclearsql, sqlconn);;
        //series of commands to bulk copy data from the excel file into our sql table
        oledbconnection oledbconn = new oledbconnection(sexcelconnectionstring);
        oledbcommand oledbcmd = new oledbcommand(myexceldataquery, oledbconn);;
        oledbdatareader dr = oledbcmd.executereader();
        sqlbulkcopy bulkcopy = new sqlbulkcopy(ssqlconnectionstring);
        bulkcopy.destinationtablename = ssqltable;
        while (
    catch (exception ex)
        //handle exception

In the above function you have to pass the MS Excel file path as a parameter. If you want to import your data by providing the client access to select the Excel file and import, then you might have to use the ASP.NET File control and upload the Excel file on the server in some temp folder, then use the file path of the uploaded Excel file and pass the path in the above function. Once data import is complete then you can delete the temporary file.

The above method first deletes the existing data from the destination table, then imports the Excel data into the same table.


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


About the Author

Software Developer (Senior)
India India
Musakkhir Sayyed is a Software Engineer working in IT Company. He has been a programmer/Software Developer for past 2 years specializing in .NET/C# development. Currently, he is a Silverlight, WPF/Sharepoint developer specializing in BCS Services.
Follow on   Twitter   Google+

Comments and Discussions

Questioncalling the method PinmemberMember 1130700612-Dec-14 12:00 
QuestionMissing Row PinmemberJonathan Pinney29-Oct-14 11:06 
QuestionImport data from Excel to sql server table PinmemberMember 1095553714-Sep-14 1:34 
AnswerRe: Import data from Excel to sql server table PinmemberMusakkhir23-Oct-14 7:25 
BugSir I tried to integrate my code with your ode to import excel data to sql server PinmemberOmkar Hendre8-Sep-14 21:00 
GeneralRe: Sir I tried to integrate my code with your ode to import excel data to sql server PinmemberMusakkhir17-Sep-14 21:47 
GeneralRe: Sir I tried to integrate my code with your ode to import excel data to sql server PinmemberOmkar Hendre17-Sep-14 22:14 
QuestionNice Article PingroupSiva Hyderabad29-Jul-14 20:04 
QuestionOracle database Pinmembergowtha malar20-May-14 5:10 
AnswerRe: Oracle database PinmemberMusakkhir23-Oct-14 7:27 
AnswerImport MS Excel data to SQL Server table using C# PinmemberMember 1080649211-May-14 22:06 
QuestionYou may need to have Excel installed on a server for server-side automation (aspx) Pinmembervrsuarez11-Feb-14 16:28 
SuggestionHow to get the excel sheet name and import into sql server- continue to my previous comment [modified] Pinmemberravi24june19-Jan-14 3:36 
AnswerImport MS Excel data to SQL Server table using C# [modified] Pinmemberravi24june19-Jan-14 2:39 
Suggestionexample didn't work for me until I made a small modification. Pinmemberjohn_17265-Oct-13 9:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 13 Aug 2013
Article Copyright 2013 by Musakkhir
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid