How to import excel data tosql
how to import an Excel file into Sql C# code windows
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rahul BansalRank #143 · 15.9K reputationPosted Mar 17, 2015, 12:47 AM EDT
//-------------------------------------
string strConn = "";
switch (Path.GetExtension(openFileDialog.FileName))
{
case ".xls": //Excel 1997-2003
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFileDialog.FileName
+ ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
break;
case ".xlsx": //Excel 2007-2010
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName
+ ";Extended Properties=\"Excel 12.0 xml;HDR=Yes;IMEX=1\"";
break;
}
//Get the data from the excel sheet1 which is default
string query = "select * from [Sheet1$]";
// Add System.Data.OleDb;
OleDbConnection objConn;
OleDbDataAdapter oleDA;
DataTable dt = new DataTable();
objConn = new OleDbConnection(strConn);
objConn.Open();
oleDA = new OleDbDataAdapter(query, objConn);
oleDA.Fill(dt);
objConn.Close();
oleDA.Dispose();
objConn.Dispose();
//Save the dt value in to the database
//------------------------------------
Abhishek JaiswalRank #126 · 17.5K reputationPosted Mar 17, 2015, 2:01 AM EDT
You can use, any of these approaches
- SQL Insert Task based on SSIS
- SQL bulk query operations
- SQL Server configuration manager
- Import & Export option
- Using conversion codes at client side
For Further details go through my articlehttp://www.c-sharpcorner.com/UploadFile/2072a9/import-data-from-excel-to-sql-server-ssis/
:)