I am using this code to import data from an Excel tab sheet into SQL Server.
In one Excel tab, we have 100,000 records, but the code only imports around 49,000 records.
In a second tab, we have around 50,000 records, but the code imports only around 23,000 records.
No error occurs, and it just move to next tab to import, as we have around 18 Excel tab sheets to import.
Does anyone have any idea? What's causing this and what could be the issue in my code?
Thanks guys for reading the post.
using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
{
// Create OleDbCommand to fetch data from Excel
using (OleDbCommand cmd = new OleDbCommand("Select * FROM [Landing Page$] where [Landing Page Unique ID] <> ''", excelConnection))
{
excelConnection.Open();
using (OleDbDataReader dReader = cmd.ExecuteReader())
{
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))
{
SqlCommand sqlcmd = new SqlCommand("TRUNCATE TABLE tblName", con);
con.Open();
sqlcmd.ExecuteNonQuery();
bulkCopy.DestinationTableName = "tblName";
bulkCopy.ColumnMappings.Add("[field names]", "field names");
bulkCopy.ColumnMappings.Add("[field names]", "field names");
// etc
bulkCopy.BatchSize = 300;
// Wait
bulkCopy.BulkCopyTimeout = 1200;
bulkCopy.WriteToServer(dReader);
con.Close();
}
}
}
}
}