列名称与数据源中的任何列都不匹配

时间:2018-03-10 13:06:42

标签: c# .net

我有以下数据表: -

enter image description here

我在SQL表下面有Product

enter image description here

数据表中的ITEM CODE在SQL中为ProductCode

数据表中的CLOSING(图像的最后一列)在SQL中为Quantity

我想在SQL中针对CLOSING(Quantity)更新ITEM CODE。

我写了这个批量更新功能:

using (SqlBulkCopy bcSQL = new SqlBulkCopy(con))
{
    bcSQL.DestinationTableName = "Product";

    // Map Source column name to Destination Column name
    // Src Column is in your DataTable
    // Dest Column is column name available in your SQL Table
    bcSQL.ColumnMappings.Add(" ITEM CODE", "ProductCode");
    bcSQL.ColumnMappings.Add(" Closing", "Quantity");

    bcSQL.WriteToServer(dt);
}

但是我收到了这个错误:

  

给定的ColumnName'项目代码'与数据源中的任何列都不匹配。

请帮忙 - 我该如何解决?

1 个答案:

答案 0 :(得分:0)

由于我从Excel填充数据表,我没有注意到列名中的空格。

列名后有2个空格

当我修改它时: -

bcSQL.ColumnMappings.Add("ITEM CODE  ", "ProductCode"); //2 spaces

它得到了解决。

关键问题: - 评论部分

中提到的列名