The reason for the error is that some column names are inconsistent even though all the columns are displayed as true.
The following piece of code causes this error:
foreach (DataColumn column in dataTable.Columns)
sqlBulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
The data of sqlBulkCopy.ColumnMappings comes from Datatable, and then you use it to compare with the column name in Datatable, it will always be equal.
I think you should compare the column names of the Datatable with the column names of the tables in the database.
I suggest you use the following code to check it:
using (SqlCommand command = new SqlCommand("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'scoretable'", sqlConnection))
{
DataTable columnNames = new DataTable();
columnNames.Load(command.ExecuteReader());
int i = 0;
foreach (DataColumn column in dataTable.Columns)
{
string columnNameInDB = columnNames.Rows[i++]["COLUMN_NAME"].ToString();
Console.WriteLine("{0}\t{1}\t{2}", column.ColumnName, columnNameInDB, columnNameInDB == column.ColumnName);
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.