Пример #1
0
        private void CopyTable(object o)
        {
            Table table = (Table)o;

            if (!String.IsNullOrEmpty(table.Name))
            {
                long          rows             = 0;
                SqlConnection connectionSource = new SqlConnection(Source);
                try
                {
                    connectionSource.Open();
                    Stopwatch stopwatch = Stopwatch.StartNew();
                    Log.Write(String.Format("Starting copy of {0}.", table.Name));

                    SqlCommand source = new SqlCommand(table.Query, connectionSource);
                    source.CommandTimeout = 0;
                    SqlDataReader reader = source.ExecuteReader();

                    if (Destination.ToLowerInvariant().Contains("data source") || Destination.ToLowerInvariant().Contains("server"))
                    {
                        SqlBulkCopy bulkCopy = new SqlBulkCopy(Destination, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.KeepNulls)
                        {
                            DestinationTableName = table.Name,
                            EnableStreaming      = true,
                            BatchSize            = 100000,
                            BulkCopyTimeout      = 0
                        };

                        bulkCopy.WriteToServer(reader);
                        rows = bulkCopy.RowsAffected();
                    }
                    else if (Destination.ToLowerInvariant() == "null")
                    {
                        while (reader.Read())
                        {
                            rows++;
                        }
                    }
                    else
                    {
                        if (!Directory.Exists(Destination + "\\"))
                        {
                            Directory.CreateDirectory(Destination + "\\");
                        }

                        using (FileStream outFile = File.Create(Destination + "\\" + table.Name.Replace("[", "").Replace("]", "") + ".gz"))
                            using (GZipStream compress = new GZipStream(outFile, CompressionMode.Compress))
                                using (StreamWriter streamWriter = new StreamWriter(compress))
                                {
                                    while (reader.Read())
                                    {
                                        streamWriter.WriteLine(reader.ToCsv());
                                        rows++;
                                    }
                                    streamWriter.Close();
                                }
                    }
                    reader.Close();
                    connectionSource.Close();
                    stopwatch.Stop();
                    Log.Write(string.Format("Finished copying {0} rows to {1} in {2}ms", rows, table.Name, stopwatch.ElapsedMilliseconds));
                }
                catch (Exception ex)
                {
                    Log.Write(ex.Message);
                }
                finally
                {
                    _currentThreadCount--;
                }
            }
        }