Hello All,
I am doing POC where .xls data insert into SQL Server DB.
But behavior of the excel its taking records getting inserted from third rows.
Example excel file contails header as columns first row and second row starts records but insertion start from third rows its ignores the first record.
Please help me on this issue to resolve.


Sachin SinghRank #66 · 26.2K reputationPosted Jan 13, 2023, 10:47 AM EST
what method have you used, with sqlbulkcopy you can do it like this
OleDbConnection Econ; SqlConnection con; string constr,Query,sqlconn; constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", YourFilePath); Econ = new OleDbConnection(constr); sqlconn = ConfigurationManager.ConnectionStrings["SqlCom"].ConnectionString; con = new SqlConnection(sqlconn); Query = string.Format("Select [Name],[City],[Address],[Designation] FROM [{0}]", "Sheet1$"); OleDbCommand Ecom = new OleDbCommand(Query, Econ); Econ.Open(); DataSet ds=new DataSet(); OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ); Econ.Close(); oda.Fill(ds); DataTable Exceldt = ds.Tables[0]; connection(); //creating object of SqlBulkCopy SqlBulkCopy objbulk = new SqlBulkCopy(con); //assigning Destination table name objbulk.DestinationTableName = "YourSqlTableName"; //Mapping Table column objbulk.ColumnMappings.Add("Request", "Request"); ---similarly all columns---- //inserting Datatable Records to DataBase con.Open(); objbulk.WriteToServer(Exceldt); con.Close();Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 13, 2023, 11:10 AM EST
May be considering the Second row also as a header. Can you create a new xls file with only two columns with a header and check if are you facing the same issue?
Vishal JoshiRank #377 · 6.7K reputationPosted Jan 16, 2023, 1:09 AM EST
Hello
You should check Connection String for Excel 2003 and Excel 2007 or higher formats.
Set OLEDB Driver accordingly and try to import data.
Check the below article to import data from excel to SQL using c# using SqlBulkCopy.
https://www.aspsnippets.com/Articles/Import-Excel-data-to-SQL-Server-Database-in-ASPNet-using-C-and-VBNet.aspx
Thank you