I read the file data into dataset and trying to bulk insert using SQL bulk copy by mapping the columns. All my column names in the database are in lower case which is created from my code.
I am not sure why I get "The specified column doesn't exist in the database" exception. Although I see all the columns mapped in the bulk copy object. Please advise.
public static void BatchBulkCopy(DataTable dataTable, string DestinationTbl, List<string> columnMapping,string filename)
{
// Get the DataTable
DataTable dtInsertRows = dataTable;
using (SqlBulkCopy sbc = new SqlBulkCopy(program.connectionStr.ToString()))
{
try
{
foreach (DataColumn col in dataTable.Columns)
{
sbc.ColumnMappings.Add(col.ColumnName.ToLower(), col.ColumnName.ToLower());
// Console.WriteLine("ok\n");
}
sbc.DestinationTableName = DestinationTbl.ToLower();
sbc.BulkCopyTimeout = 8000;
sbc.DestinationTableName = "["+ DestinationTbl.ToLower() + "]";
sbc.WriteToServer(dtInsertRows);
sbc.Close();
}
catch (Exception ex)
{
}
}
}
Jonathan Magnan
11.4k2 gold badges46 silver badges65 bronze badges
asked Feb 27, 2017 at 16:52
user1046415
7894 gold badges25 silver badges44 bronze badges
1 Answer 1
You haven't open database connection. Insert sbc.Open(); before sbc.WriteToServer(dtInsertRows);
Sign up to request clarification or add additional context in comments.
Comments
default
dataTable.Column?