我正在使用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'不匹配。
我在互联网上搜索过很多但没有任何解决方案的运气。如果有人帮助我,我会非常感激。
答案 0 :(得分:2)
试试这个
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();
答案 1 :(得分:2)
源varchar列的排序规则与目标varchar列的排序规则不同。
首先加载到数据表可以解决此问题,但是如果要加载大量数据,则可能会遇到System.OutOfMemoryException。
答案 2 :(得分:0)
虽然您声称源表和目标表的列Collations相同,但显然它们是不同的,至少对于这一个特定列而言。
一种更简单的方法既不会消除将DataReader
直接传递给SqlBulkCopy
,也不会使用任何额外内存的性能优势,只需强制将该列的排序规则作为目标排序规则。您可以通过指定SELECT
的列而不是使用*
(在任何情况下都是一种好的做法)来执行此操作,然后允许您将COLLATE
关键字添加到您获得的任何字符串列中这个错误。例如:
@"SELECT RequisitionId, CreatedBy COLLATE {destination_collation_name}, DateCreated,
AircraftTailNum, JobNumber, ShopCode, RequestedByName, RequestedById,
Status, IsCancelled, IsProcessed
FROM Requisitions;"