You are on page 1of 4

Mohammad Nizamuddin

http://insqlserver.com | http://spshare.blogspot.com | http://shaikhnizam.blogspot.com

Steps
To
Import MS Excel Data to SQL Server table
using C# .Net

Page 1 of 4

Mohammad Nizamuddin

http://insqlserver.com | http://spshare.blogspot.com | http://shaikhnizam.blogspot.com

If you already have data in MS Excel file, and want to migrate your MS Excel data to SQL Server table, follow below steps
Step 1: Lets take an example to import the data to SQL Server table, I am going to import student information data from ms
excel sheet to tStudent SQL table,
My Excel sheet structure is looks like

Step 2: Now design a tStudent table in SQL server


CREATE TABLE
(
STUDENT VARCHAR(64),
ROLLNO VARCHAR(16),
COURSE VARCHAR(32),
)

Your ms excel sheet and SQL table is ready, now its time to write c# code to import the excel sheet into tStudent table
Step 3: Add these two name space in your class file
USING SYSTEM.DATA.OLEDB;
USING SYSTEM.DATA.SQLCLIENT;

Page 2 of 4

Mohammad Nizamuddin

http://insqlserver.com | http://spshare.blogspot.com | http://shaikhnizam.blogspot.com

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
DIFFERENT
STRING MYEXCELDATAQUERY = "SELECT STUDENT,ROLLNO,COURSE FROM [SHEET1$]";
TRY
{
//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);
SQLCONN.OPEN();
SQLCMD.EXECUTENONQUERY();
SQLCONN.CLOSE();
//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);
OLEDBCONN.OPEN();
OLEDBDATAREADER DR = OLEDBCMD.EXECUTEREADER();
SQLBULKCOPY BULKCOPY = NEW SQLBULKCOPY(SSQLCONNECTIONSTRING);
BULKCOPY.DESTINATIONTABLENAME = SSQLTABLE;
WHILE (DR.READ())
{
BULKCOPY.WRITETOSERVER(DR);
}

Page 3 of 4

Mohammad Nizamuddin

http://insqlserver.com | http://spshare.blogspot.com | http://shaikhnizam.blogspot.com

OLEDBCONN.CLOSE();
}
CATCH (EXCEPTION EX)
{
//HANDLE EXCEPTION
}
}

In above function you have to pass ms excel file path as a parameter, if you want to import your data by providing client an
access to select the excel file and import, then you might have to use asp.net file control, and upload the excel file on the
server in some temp folder, then use the file path of the upload excel file and pass the path in above function. Once data
import is completed then you can delete temporary file.
The above method, first delete the existing data from the destination table, then import the excel data into the same table.

Page 4 of 4

You might also like