Vote count:
0
I am using the following code to write from a DataTable to an SQL Server database:
if (dtData.Rows.Count > 0)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName = tableName;
foreach (var column in dtData.Columns)
{
bulkCopy.ColumnMappings.Add(column.ToString(), column.ToString());
}
bulkCopy.WriteToServer(dtData);
}
}
}
I am receiving the following error: The given ColumnMapping does not match up with any column in the source or destination.
I understand what the error means - that at least one or more column in the DataTable do not match in the database. Unfortunately this involves a massive production table with hundreds of columns - I cannot go through each of them one-by-one when this error is encountered (the DataTable is dynamic so it can happen again).
How can I modify the code to tell me which columns are causing this problem? I thought of running an SQL statement on the SQL Server database which returns the columns, and compares the DataTable's columns with that of the database's. Are there any other simpler approaches though, as this might be overkill for a seemingly simple task? The error message does not seem to return the columns.
How to see which columns are giving an error
Aucun commentaire:
Enregistrer un commentaire