1

I am trying to do a columnMappings for a bulk insert but i cant get the Columnmapping to work in a for loop but when i do it manually it works just fine.

I get this error when trying a dynamic ColumnMapping even though it adds the ColumnMapping to the "objbulk" with the same columns name as columns in the manual ColumnMapping:

Exception thrown: 'System.InvalidOperationException' in System.Data.SqlClient.dll

using (var objbulk = new SqlBulkCopy(connConfig, SqlBulkCopyOptions.Default, objTrans))
{                                 
    DataColumn colDateTime = new DataColumn("UpdateTimestamp");
    dt.Columns.Add(colDateTime);
    foreach (DataRow row in dt.Rows)
    {
        row.SetField("UpdateTimestamp", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
    }
    objbulk.DestinationTableName = tableName;
    foreach (DataColumn column in dt.Columns)
    {
        objbulk.ColumnMappings.Add(column.ColumnName, column.ColumnName);
    }
                                       
    /*         objbulk.AutoMapColumns(dt);*/
    /* objbulk.ColumnMappings.Add("ColumnA", "ColumnA");
     objbulk.ColumnMappings.Add("ColumnB", "ColumnB");
     objbulk.ColumnMappings.Add("ColumnC", "ColumnC");
     objbulk.ColumnMappings.Add("ColumnD", "ColumnD");
     objbulk.ColumnMappings.Add("UpdateTimestamp", "UpdateTimestamp");*/
    objbulk.WriteToServer(dt);                              
}
objTrans.Commit();
return 1;

what am I doing wrong? its so strange that its basically the same thing. Could anyone tell me what i am missing here?

Thanks!

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
E S
  • 105
  • 2
  • 8
  • 1
    Can you please provide a full error text? – Guru Stron Jan 10 '23 at 19:10
  • 'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Remote Debugger\x64\Runtime\Microsoft.VisualStudio.Debugger.Runtime.NetCoreApp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. Exception thrown: 'System.InvalidOperationException' in System.Data.SqlClient.dll – E S Jan 11 '23 at 15:44

0 Answers0