块拷贝
/// <summary>
/// 块拷贝
/// </summary>
/// <param name="source"></param>
/// <param name="connectionString"></param>
/// <param name="destinationTableName"></param>
public static void SaveToSqlServer(DataTable source, string connectionString, string destinationTableName)
{
try
{
using (SqlBulkCopy bcp = new SqlBulkCopy(connectionString))
{
bcp.DestinationTableName = destinationTableName;
bcp.BatchSize = 20000;
bcp.BulkCopyTimeout = 1200;
int columnIdex = 0;
foreach (DataColumn column in source.Columns)
{
bcp.ColumnMappings.Add(columnIdex, columnIdex);
columnIdex++;
}
bcp.WriteToServer(source);
}
}
catch (Exception ex)
{
throw ex;
}
}
public static void SaveToSqlServer(DataTable source, string connectionString, string destinationTableName, System.Collections.Generic.Dictionary<string, string> mappingColumns)
{
using (SqlBulkCopy bcp = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
{
bcp.DestinationTableName = destinationTableName;
bcp.BatchSize = 20000;
bcp.BulkCopyTimeout = 1200;
try
{
foreach (string columnName in mappingColumns.Keys)
{
SqlBulkCopyColumnMapping columnMapID = new SqlBulkCopyColumnMapping(columnName, mappingColumns[columnName]);
bcp.ColumnMappings.Add(columnMapID);
}
bcp.WriteToServer(source);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}