Click here to Skip to main content
11,924,800 members (56,697 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked

Import Data from excel to SQL server using C# in ASP. NET

, 16 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
When the user uploads the Excel file to the server its data must dumped to the SQL server database
The Page having a FileUpload control and the Upload button, on selecting the Excel file user needs to click on Upload button to store the data to Server. Here we are treating the uploaded file as database hence we need to create OLEDB connection to this file, from this connection will be created and the data is fetched to C# as DataTable. '[Sheet1$]' is the Name of the Worksheet where requires data is present.
string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +  + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Sheet1$]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
From this code connection will be created for the Excel file and the data in Sheet1 will be dumped to the dtExcel of type DataTable.
Note:'$' must be there after the worksheet name.

Whole code for the same is here.
fileName = FileUpload1.ResolveClientUrl(FileUpload1.PostedFile.FileName);
int count = 0;
DataClassesDataContext conLinq = new DataClassesDataContext("Data Source=server name;Initial Catalog=Database Name;Integrated Security=true");
   DataTable dtExcel = new DataTable();
   string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
   OleDbConnection con = new OleDbConnection(SourceConstr);
   string query = "Select * from [Sheet1$]";
   OleDbDataAdapter data = new OleDbDataAdapter(query, con);
   for (int i = 0; i < dtExcel.Rows.Count; i++)
         count += conLinq.ExecuteCommand("insert into table name values(" + dtExcel.Rows[i][0] + "," + dtExcel.Rows[i][1] + ",'" + dtExcel.Rows[i][2] + "',"+dtExcel.Rows[i][3]+")");
      catch (Exception ex)
   if (count == dtExcel.Rows.Count)
      <--Success Message-->
      <--Failure Message-->
catch (Exception ex)
   throw ex;


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


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

QuestionError in OleDbConnection Pin
cdraghu15-Nov-13 4:25
membercdraghu15-Nov-13 4:25 
GeneralMy vote of 1 Pin
preetimodanwal11-Oct-12 20:17
memberpreetimodanwal11-Oct-12 20:17 
GeneralRe: My vote of 1 Pin
Suraj S Koneri12-Oct-12 2:45
memberSuraj S Koneri12-Oct-12 2:45 
QuestionThe type or namespace name 'dataclassesdatacontext' could not be found Pin
Gunasekara Perumal6-Sep-12 9:31
memberGunasekara Perumal6-Sep-12 9:31 
AnswerRe: The type or namespace name 'dataclassesdatacontext' could not be found Pin
Suraj S Koneri6-Sep-12 20:15
memberSuraj S Koneri6-Sep-12 20:15 
Questionhi Suraj Pin
Hari Chowdary13-Jun-12 9:57
groupHari Chowdary13-Jun-12 9:57 
i am use this code
i get an error like: The Microsoft Office Access database engine could not find the object 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\kolli.xlsx'. Make sure the object exists and that you spell its name and the path name correctly.

plz can u help me..
thanks in advanc

modified 13-Jun-12 15:09pm.

QuestionHow to rectify this error Pin
shwetha124-Apr-12 1:33
membershwetha124-Apr-12 1:33 
QuestionQuestions Pin
xbolslock4-Apr-12 17:59
memberxbolslock4-Apr-12 17:59 
GeneralThis part ["insert into table name values"] seems to assume ... Pin
Member 40387421-Dec-11 3:11
memberMember 40387421-Dec-11 3:11 
GeneralRe: the table already will be created in the database at back en... Pin
Suraj S Koneri21-Dec-11 5:46
memberSuraj S Koneri21-Dec-11 5:46 
GeneralReason for my vote of 2 is the code incomplete? It doesn't s... Pin
Member 40387421-Dec-11 0:54
memberMember 40387421-Dec-11 0:54 
GeneralReason for my vote of 5 5ed Pin
Pramod harithsa19-Dec-11 0:18
memberPramod harithsa19-Dec-11 0:18 
Generali have an access file which is password protected.can i do a... Pin
fairoozkk18-Dec-11 8:39
memberfairoozkk18-Dec-11 8:39 
GeneralRe: For the Access database above method is good but instead of ... Pin
Suraj S Koneri18-Dec-11 20:47
memberSuraj S Koneri18-Dec-11 20:47 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 16 Dec 2011
Article Copyright 2011 by Suraj S Koneri
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid