我正在尝试使用SqlBulkCopy将数据复制到SQL数据库表中,但它(错误地)说列不匹配.他们匹配.如果我使用断点来查看要映射的列的名称,它们是正确的.错误消息显示列的名称,并且它是正确的.
这是我的方法.我有一个相同的方法做工作,唯一的区别是哪里得到的列名.但是,包含列名的字符串完全相同.
public static bool ManualMapImport(DataTable dataTable, string table)
{
if(dataTable != null)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlBulkCopy import = new SqlBulkCopy(connection);
import.DestinationTableName = "[" + table + "]";
foreach (string s in Global.SelectedColumns)
{
/* The s string variable here is the EXACT same as
the c.ToString() in the other method below */
if (ColumnExists(table, s))
import.ColumnMappings.Add(s, s);
else
return false;
}
connection.Open();
import.WriteToServer(dataTable); //Error happens on this line
connection.Close();
return true;
}
else
{ …
Run Code Online (Sandbox Code Playgroud)