首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlBulkCopy不工作

SqlBulkCopy不工作
EN

Stack Overflow用户
提问于 2009-01-13 10:46:19
回答 6查看 36.2K关注 0票数 20

我从Excel中填充了一个DataSet。我想使用SQLBulk拷贝在Lead_Hdr表中插入记录,其中LeadId是PK。

在执行以下代码时,我出现了以下错误:

给定的ColumnMapping与源或目标中的任何列不匹配。

代码语言:javascript
复制
string ConStr=ConfigurationManager.ConnectionStrings["ConStr"].ToString();

using (SqlBulkCopy s = new SqlBulkCopy(ConStr,SqlBulkCopyOptions.KeepIdentity))
{
    if (MySql.State==ConnectionState.Closed)
    {
        MySql.Open();
    }

    s.DestinationTableName = "PCRM_Lead_Hdr";
    s.NotifyAfter = 10000;

    #region Comment
    s.ColumnMappings.Clear();

    #region ColumnMapping
    s.ColumnMappings.Add("ClientID", "ClientID");
    s.ColumnMappings.Add("LeadID", "LeadID");
    s.ColumnMappings.Add("Company_Name", "Company_Name");
    s.ColumnMappings.Add("Website", "Website");
    s.ColumnMappings.Add("EmployeeCount", "EmployeeCount");
    s.ColumnMappings.Add("Revenue", "Revenue");
    s.ColumnMappings.Add("Address", "Address");
    s.ColumnMappings.Add("City", "City");

    s.ColumnMappings.Add("State", "State");
    s.ColumnMappings.Add("ZipCode", "ZipCode");
    s.ColumnMappings.Add("CountryId", "CountryId");

    s.ColumnMappings.Add("Phone", "Phone");
    s.ColumnMappings.Add("Fax", "Fax");
    s.ColumnMappings.Add("TimeZone", "TimeZone");
    s.ColumnMappings.Add("SicNo", "SicNo");
    s.ColumnMappings.Add("SicDesc", "SicDesc");

    s.ColumnMappings.Add("SourceID", "SourceID");
    s.ColumnMappings.Add("ResearchAnalysis", "ResearchAnalysis");
    s.ColumnMappings.Add("BasketID", "BasketID");
    s.ColumnMappings.Add("PipeLineStatusId", "PipeLineStatusId");

    s.ColumnMappings.Add("SurveyId", "SurveyId");
    s.ColumnMappings.Add("NextCallDate", "NextCallDate");
    s.ColumnMappings.Add("CurrentRecStatus", "CurrentRecStatus");
    s.ColumnMappings.Add("AssignedUserId", "AssignedUserId");
    s.ColumnMappings.Add("AssignedDate", "AssignedDate");
    s.ColumnMappings.Add("ToValueAmt", "ToValueAmt");
    s.ColumnMappings.Add("Remove", "Remove");
    s.ColumnMappings.Add("Release", "Release");

    s.ColumnMappings.Add("Insert_Date", "Insert_Date");
    s.ColumnMappings.Add("Insert_By", "Insert_By");
    s.ColumnMappings.Add("Updated_Date", "Updated_Date");
    s.ColumnMappings.Add("Updated_By", "Updated_By");

    #endregion
    #endregion

    s.WriteToServer(sourceTable);

    s.Close();

    MySql.Close();
}
EN

回答 6

Stack Overflow用户

发布于 2010-04-09 11:27:10

在将数据从access复制到SQLSERVER 2005时,我遇到了同样的问题,我发现无论数据库是否敏感,列映射在两个数据源上都是区分大小写的。

票数 40
EN

Stack Overflow用户

发布于 2009-01-14 01:56:23

Marc的答案将是我的推荐(关于使用暂存表)。这确保了如果您的源代码不改变,您在将来导入的问题就会减少。

然而,根据我的经验,您可以检查以下问题:

列类型匹配的源和表中的列名匹配

如果你认为你做了这件事,但仍然没有成功。您可以尝试以下方法。

1-允许表2中所有列中的空-注释掉所有列映射3-每次重新添加一个列,直到您找到问题所在。

那应该能把虫子弄出来

票数 2
EN

Stack Overflow用户

发布于 2016-08-19 11:55:02

原因之一是:SqlBukCOpy区分大小写。遵循以下步骤:

  1. --在这种情况下,首先必须在源表中使用C#.
  2. 中的“包含”方法查找列,一旦目标列与源列匹配,获取该列的索引,并在SqlBukCOpy .

中给出其列名

例如:`

代码语言:javascript
复制
//Get Column from Source table 
  string sourceTableQuery = "Select top 1 * from sourceTable";
   DataTable dtSource=SQLHelper.SqlHelper.ExecuteDataset(transaction, CommandType.Text, sourceTableQuery).Tables[0];// i use sql helper for executing query you can use corde sw

 for (int i = 0; i < destinationTable.Columns.Count; i++)
                        {    //check if destination Column Exists in Source table
                            if (dtSource.Columns.Contains(destinationTable.Columns[i].ToString()))//contain method is not case sensitive
                            {
                                int sourceColumnIndex = dtSource.Columns.IndexOf(destinationTable.Columns[i].ToString());//Once column matched get its index
                                bulkCopy.ColumnMappings.Add(dtSource.Columns[sourceColumnIndex].ToString(), dtSource.Columns[sourceColumnIndex].ToString());//give coluns name of source table rather then destination table so that it would avoid case sensitivity
                            }

                        }
                        bulkCopy.WriteToServer(destinationTable);
                        bulkCopy.Close();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/438587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档