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

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

By , 16 Dec 2011
 
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);
data.Fill(dtExcel);
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");
try
{
   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);
   data.Fill(dtExcel);
   for (int i = 0; i < dtExcel.Rows.Count; i++)
   {
      try
      {
         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)
      {
         continue;
      }
   }
   if (count == dtExcel.Rows.Count)
   {
      <--Success Message-->
   }
   else
   {
      <--Failure Message-->
   }
}
catch (Exception ex)
{
   throw ex;
}
finally
{
   conLinq.Dispose();
}

License

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

About the Author

Suraj S Koneri
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberpreetimodanwal11 Oct '12 - 19:17 
GeneralRe: My vote of 1memberSuraj S Koneri12 Oct '12 - 1:45 
QuestionThe type or namespace name 'dataclassesdatacontext' could not be foundmemberGunasekara Perumal6 Sep '12 - 8:31 
AnswerRe: The type or namespace name 'dataclassesdatacontext' could not be foundmemberSuraj S Koneri6 Sep '12 - 19:15 
Questionhi Suraj [modified]groupHari Chowdary13 Jun '12 - 8:57 
QuestionHow to rectify this errormembershwetha124 Apr '12 - 0:33 
QuestionQuestionsmemberxbolslock4 Apr '12 - 16:59 
GeneralThis part ["insert into table name values"] seems to assume ...memberMember 40387421 Dec '11 - 2:11 
GeneralRe: the table already will be created in the database at back en...memberSuraj S Koneri21 Dec '11 - 4:46 
GeneralReason for my vote of 2 is the code incomplete? It doesn't s...memberMember 40387420 Dec '11 - 23:54 
GeneralReason for my vote of 5 5edmemberPramod harithsa18 Dec '11 - 23:18 
Generali have an access file which is password protected.can i do a...memberfairoozkk18 Dec '11 - 7:39 
GeneralRe: For the Access database above method is good but instead of ...memberSuraj S Koneri18 Dec '11 - 19:47 

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

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