Example #1
0
        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;
            }
        }