HI
I have excel file (.xls), i want read data from excel file and save into sql server database table in c# console.
| EmpId | FirstName | LastName | Desc | Remark |
| 101 | A | B | 101 - A | AA |
HI
I have excel file (.xls), i want read data from excel file and save into sql server database table in c# console.
| EmpId | FirstName | LastName | Desc | Remark |
| 101 | A | B | 101 - A | AA |
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
Amit MohantyRank #112 · 20K reputationPosted Dec 26, 2022, 1:46 AM EST
Try this
System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\your_path\\Import_List.xls;Extended Properties=Excel 8.0;"); ExcelConnection.Open(); string expr = "SELECT * FROM [Sheet1$]"; OleDbCommand objCmdSelect = new OleDbCommand(expr, ExcelConnection); OleDbDataReader objDR = null; SqlConnection SQLconn = new SqlConnection(); string ConnString = "Data Source=.;Initial Catalog=your_dbname;Trusted_Connection=True;"; SQLconn.ConnectionString = ConnString; SQLconn.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLconn)) { bulkCopy.DestinationTableName = "your-table-name"; try { objDR = objCmdSelect.ExecuteReader(); bulkCopy.WriteToServer(objDR); ExcelConnection.Close(); SQLconn.Close(); } catch (Exception ex) MessageBox.Show(ex.ToString()); }Vishal JoshiRank #377 · 6.7K reputationPosted Dec 26, 2022, 4:19 AM EST
Hello
Please check the below article for the same.
https://www.c-sharpcorner.com/blogs/import-excel-spreadsheet-data-into-sql-server-table-using-c-sharp
Thanks
Vishal YelveRank #178 · 13.6K reputationPosted Dec 26, 2022, 12:58 PM EST
Please refer my below article, how to read data from excel file.
https://www.c-sharpcorner.com/article/how-to-read-data-from-an-excel-sheet/
Leon DRank #851 · 2.6K reputationPosted Dec 26, 2022, 8:29 PM EST
You can refer to the below article to read data from Excel to a datatable, then insert the datatable into the database.
https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Excel-Import/Export-Import-Data-from-Excel-to-Datatable.html