2

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
3
  • 1
    Can you add your table definition / all values from dataTable.Column? Commented Feb 27, 2017 at 17:35
  • 1
    are you SURE you don't have casing issues? Why are to calling toLower on your source table? the mapping should match case exactly on both sides. stackoverflow.com/questions/438587/sqlbulkcopy-not-working Commented Feb 27, 2017 at 17:57
  • my datatable has the column data from excel file which I first create a table in DB from my code with all lower case columns.So they should match..Can some one provide a sample please,not sure where I am doing it wrong.. Commented Feb 27, 2017 at 22:38

1 Answer 1

1

You haven't open database connection. Insert sbc.Open(); before sbc.WriteToServer(dtInsertRows);

answered Apr 18, 2018 at 18:54
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.