You execute the method WriteToServer, and the following error is thrown:
The given ColumnMapping does not match up with any column in the source or destination.
using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
connection.Open();
using (var sqlBulk = new SqlBulkCopy(connection))
{
// Oops! The destination column is case sensitive, it should be instead "Name"
sqlBulk.ColumnMappings.Add("Name", "name");
sqlBulk.ColumnMappings.Add("Country", "Country");
sqlBulk.ColumnMappings.Add("City", "City");
sqlBulk.DestinationTableName = "Customers";
sqlBulk.WriteToServer(dt);
}
}
Try it (More column in the source)
using (var sqlBulk = new SqlBulkCopy(connection))
{
// SET ColumnMappings values.
sqlBulk.ColumnMappings.Add("Name", "Name");
sqlBulk.ColumnMappings.Add("Country", "Country");
sqlBulk.ColumnMappings.Add("City", "City");
sqlBulk.DestinationTableName = "Customers";
sqlBulk.WriteToServer(dt);
}
using (var sqlBulk = new SqlBulkCopy(connection))
{
// SET ColumnMappings values.
sqlBulk.ColumnMappings.Add("Name", "Name");
sqlBulk.ColumnMappings.Add("Country", "Country");
sqlBulk.ColumnMappings.Add("City", "City");
sqlBulk.DestinationTableName = "Customers";
sqlBulk.WriteToServer(dt);
}
You cannot make the destination column name case insensitive.
var dt = new DataTable();
dt.CaseSensitive = false;
dt.Columns.Add("TheColumnInt", typeof(int));
dt.Columns.Add("TheColumnString");