本文整理汇总了C#中System.Data.SqlClient.SqlBulkCopy.?.Close方法的典型用法代码示例。如果您正苦于以下问题:C# SqlBulkCopy.?.Close方法的具体用法?C# SqlBulkCopy.?.Close怎么用?C# SqlBulkCopy.?.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlBulkCopy
的用法示例。
在下文中一共展示了SqlBulkCopy.?.Close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BulkCopy_DoWork
private void BulkCopy_DoWork(object sender, DoWorkEventArgs e)
{
//TODO: use more usings!
var sw = Stopwatch.StartNew();
SqlConnection connSource = null;
SqlConnection connDest = null;
var result = new BulkCopyResult();
e.Result = result;
var parameters = (BulkCopyParameters)e.Argument;
try {
connSource = new SqlConnection(parameters.Source.ConnectionString);
connDest = new SqlConnection(parameters.Destination.ConnectionString);
connSource.Open();
connDest.Open();
SqlDataReader reader = null;
foreach (var table in parameters.Tables.Values) {
if (worker.CancellationPending) {
e.Cancel = true;
return;
}
Log.Information("Copying rows to {0}", "[" + connDest.DataSource + "].[" + connDest.Database + "]." + table.FullName);
SqlTransaction transaction = null;
SqlBulkCopy bulkCopy = null;
try {
//Use Dictionary<,> so that destination column names can be case-insensitively located in their proper case because SqlBulkCopy mappings are case-sensitive!
var destSchema = connDest.GetSchema("Columns", new[] {connDest.Database, table.Schema, table.Name});
var destColumnsMap = destSchema.AsEnumerable().Select(x => x.Field<string>("COLUMN_NAME")).ToDictionary(k => k, v => v, StringComparer.OrdinalIgnoreCase);
transaction = connDest.BeginTransaction();
string query = String.Format(parameters.Query, table.FullName);
reader = new SqlCommand(query, connSource) { CommandTimeout = 9000 }.ExecuteReader();
//TODO: any FKs should be dropped and then recreated after truncating
try {
new SqlCommand(String.Format(Query_TruncateTable, table.FullName), connDest, transaction) { CommandTimeout = 120 }.ExecuteNonQuery();
} catch {
new SqlCommand(String.Format(Query_DeleteAllInTable, table.FullName), connDest, transaction) { CommandTimeout = 120 }.ExecuteNonQuery();
}
bulkCopy = new SqlBulkCopy(connDest, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.KeepNulls, transaction) {
BulkCopyTimeout = 9000,
BatchSize = 10000,
NotifyAfter = 10000,
DestinationTableName = table.FullName
};
bulkCopy.SqlRowsCopied += sbc_SqlRowsCopied;
//Iterate over the Reader to get source column names because they may be defined by query results rather than a table-schema
var sourceColumns = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
for (int i = 0; i < reader.FieldCount; i++) {
sourceColumns.Add(reader.GetName(i));
}
var mapColumns = bulkCopy.ColumnMappings;
foreach (var column in sourceColumns) {
if (destColumnsMap.ContainsKey(column)) {
mapColumns.Add(column, destColumnsMap[column]);
}
}
rowsInCurrentTable = table.RowCount;
//Make sure the progress indicators are updated immediately, so the correct progress details are shown
sbc_SqlRowsCopied(bulkCopy, new SqlRowsCopiedEventArgs(0));
bulkCopy.WriteToServer(reader);
transaction.Commit();
Log.Information("Copied approximately {0} rows to {1}", table.RowCount, "[" + connDest.DataSource + "].[" + connDest.Database + "]." + table.FullName);
} catch (Exception ex) {
result.FailedTables[table.FullName] = ex;
transaction?.Rollback();
} finally {
bulkCopy?.Close();
reader?.Close();
}
if (worker.CancellationPending) {
e.Cancel = true;
return;
}
}
} finally {
connDest?.Close();
connSource?.Close();
sw.Stop();
result.Elapsed = sw.Elapsed;
}
//.........这里部分代码省略.........