小编Por*_*ges的帖子

SqlBulkCopy - 给定的ColumnName与源或目标中的任何列都不匹配

我正在尝试使用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)

c# sql-server sqlbulkcopy

5
推荐指数
1
解决办法
8449
查看次数

标签 统计

c# ×1

sql-server ×1

sqlbulkcopy ×1