BulkCopy错误:源列“CreatedBy”的区域设置ID“1025”和目标列“CreatedBy”的区域设置ID“1033”不匹配

asp.net c# sqlbulkcopy sql-server

我正在使用SqlBulkCopy类执行SQL批量复制。我创建的目标表与源表完全相同。它们都具有相同的表名,列名,数据类型甚至相同的排序规则。我也做了sql映射以确保准确性。

我的代码如下:

SqlConnection SourceConnection =  new SqlConnection(SourceConnectionString);
SqlConnection DestinationConnection =  new SqlConnection(DestinationConnectionString);

DestinationConnection.Open();
SourceConnection.Open();

SqlCommand commandSourceData = new SqlCommand("SELECT * FROM Requisitions;", SourceConnection);

SqlDataReader reader = commandSourceData.ExecuteReader();    

    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(DestinationConnection))

    {
           bulkCopy.DestinationTableName = "Requisitions";


           bulkCopy.ColumnMappings.Add("RequisitionId", "RequisitionId");
           bulkCopy.ColumnMappings.Add("CreatedBy", "CreatedBy");
           bulkCopy.ColumnMappings.Add("DateCreated", "DateCreated");
           bulkCopy.ColumnMappings.Add("AircraftTailNum", "AircraftTailNum");
           bulkCopy.ColumnMappings.Add("JobNumber", "JobNumber");
           bulkCopy.ColumnMappings.Add("ShopCode", "ShopCode");
           bulkCopy.ColumnMappings.Add("RequestedByName", "RequestedByName");
           bulkCopy.ColumnMappings.Add("RequestedById", "RequestedById");
           bulkCopy.ColumnMappings.Add("Status", "Status");
           bulkCopy.ColumnMappings.Add("IsCancelled", "IsCancelled");
           bulkCopy.ColumnMappings.Add("IsProcessed", "IsProcessed");


           try
               {
                  // Write from the source to the destination.
                  bulkCopy.WriteToServer(reader);
                }
          catch (Exception ex)
                {
                     Console.WriteLine(ex.Message);
                 }
          finally
                 {

                    reader.Close();
                  }
          }

但是,每次执行代码时都会出现以下错误:

源列“CreatedBy”的区域设置ID“1025”和目标列“CreatedBy”的区域设置ID“1033”不匹配。

我在互联网上搜索了很多,但没有任何解决方案的运气。如果有人帮助我,我会非常感激。

一般承认的答案

试试这样吧

  public DataTable fetchValue()
    {
        SqlDataAdapter dap=new SqlDataAdapter("SELECT RequisitionId,CreatedBy,DateCreated,AircraftTailNum,JobNumber,ShopCode,RequestedByName,RequestedById,Status,IsCancelled,IsProcessed FROM Requisitions;", cn);
        DataSet ds=new();
        dap.Fill(ds);
        return ds.Tables[0];
    }

        DataTable dtgenerate=new DataTable();
        dtgenerate=fetchValue();

       cn.open();
       using (SqlBulkCopy bulkCopy = new SqlBulkCopy(n))
       {
           bulkCopy.DestinationTableName = "Requisitions";//DestionTableName
           // bulkCopy.ColumnMappings.Add("SourceColumnIndex", "DestinationColumnIndex");  
           bulkCopy.ColumnMappings.Add("RequisitionId", "RequisitionId");
           bulkCopy.ColumnMappings.Add("CreatedBy", "CreatedBy");
           bulkCopy.ColumnMappings.Add("DateCreated", "DateCreated");
           bulkCopy.ColumnMappings.Add("AircraftTailNum", "AircraftTailNum");
           bulkCopy.ColumnMappings.Add("JobNumber", "JobNumber");
           bulkCopy.ColumnMappings.Add("ShopCode", "ShopCode");
           bulkCopy.ColumnMappings.Add("RequestedByName", "RequestedByName");
           bulkCopy.ColumnMappings.Add("RequestedById", "RequestedById");
           bulkCopy.ColumnMappings.Add("Status", "Status");
           bulkCopy.ColumnMappings.Add("IsCancelled", "IsCancelled");
           bulkCopy.ColumnMappings.Add("IsProcessed", "IsProcessed");

           bulkCopy.WriteToServer(dtgenerate);
       }
       cn.close();

热门答案

源varchar列的排序规则与目标varchar列的排序规则不同。

首先加载到数据表可以解决此问题,但是如果要加载大量数据,则可能会遇到System.OutOfMemoryException。



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因