你们不需要再造一个新例子,即SqlBulkCopy阶级拥有一种财产,是你可以使用的地图集:
public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize)
{
// Get the DataTable
DataTable dtInsertRows = dataTable;
using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
{
sbc.DestinationTableName = DestinationTbl;
// Number of records to be processed in one go
sbc.BatchSize = batchSize;
// Add your column mappings here
sbc.ColumnMappings.Add("field1","field3");
sbc.ColumnMappings.Add("foo","bar");
// Finally write to server
sbc.WriteToServer(dtInsertRows);
}
}
http://www.un.org。
根据这些评论,目标是发挥通用功能,例如不必在职能中明确编码制图。 由于ColumnMappingCollection无法即时进行,我建议通过一个<代码>List<string>或包含该职能栏制图定义的类似条目。 例如:
var columnMapping = new List<string>();
columnMapping.Add("field1,field3");
columnMapping.Add("foo,bar");
然后将职能重新确定为
public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize, List<string> columnMapping)
{
// Get the DataTable
DataTable dtInsertRows = dataTable;
using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
{
sbc.DestinationTableName = DestinationTbl;
// Number of records to be processed in one go
sbc.BatchSize = batchSize;
// Add your column mappings here
foreach(var mapping in columnMapping)
{
var split = mapping.Split(new[] { , });
sbc.ColumnMappings.Add(split.First(), split.Last());
}
// Finally write to server
sbc.WriteToServer(dtInsertRows);
}
}