Exemplo n.º 1
0
        private static void IniciarTransferencia(BaseConfigurationCollection <TablesElement> tables, DataTransfer.BulkConfig bulkConfig)
        {
            var tempo = new System.Diagnostics.Stopwatch();
            int rcount;

            #region [ TRANSFERENCIA DOS DADOS ]
            foreach (TablesElement table in tables)
            {
                string msg = "IniciarTransferencia: ";
                try
                {
                    if (!string.IsNullOrEmpty(table.ToCsvFile))
                    {
                        EscreverLog($"Exportação da fonte de dados \"{table.SourceName}\"", ConsoleColor.DarkGreen);
                    }
                    else if (string.Compare(table.Name, table.SourceName, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        EscreverLog($"Transferindo dados para a tabela \"{table.Name}\"", ConsoleColor.DarkGreen);
                    }
                    else
                    {
                        EscreverLog($"Transferindo dados de \"{table.SourceName}\" para \"{table.Name}\"", ConsoleColor.DarkGreen);
                    }

                    if (!string.IsNullOrEmpty(table.ToCsvFile))
                    {
                        msg = $"Exportação da fonte de dados \"{table.SourceName}\"";
                        tempo.Restart();
                        rcount = _transfer.ExportarArquivoCsv(table.SelectAll, table.ToCsvFile, true, ";", bulkConfig.CopyTimeout);
                        tempo.Stop();
                        EscreverLog($"\t Arquivo \"{table.ToCsvFile}\" criado com {rcount:n0} linhas! {tempo.Elapsed}");
                    }
                    else
                    {
                        if (table.Truncate)
                        {
                            msg = "Exclusão de dados";
                            tempo.Restart();
                            _transfer.LimparTabelaDestino(table.Name);
                            tempo.Stop();
                            EscreverLog($"\t {msg} finalizado! {tempo.Elapsed}");
                        }

                        msg = "Copia de dados";
                        tempo.Restart();
                        EscreverLog($"\t Transferência de {_transfer.BulkCopy(table.Name, table.SelectAll, bulkConfig):n0} registros finalizado! {tempo.Elapsed}");
                        tempo.Stop();

                        msg = "Re-Sequênciamento de campo Identity";
                        tempo.Restart();
                        _transfer.Reseed(table.Name);
                        tempo.Stop();

                        EscreverLog($"\t {msg} finalizado! {tempo.Elapsed}");
                        if (table.UpdateStatistics)
                        {
                            msg = "Atualização de estatisticas";
                            tempo.Restart();
                            _transfer.UpdateStatistics(table.Name);
                            tempo.Stop();
                            EscreverLog($"\t {msg} finalizado! {tempo.Elapsed}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    EscreverLogErro(msg + " " + ex.Message);
                }
                finally
                {
                    EscreverLogLinha();
                }
            }
            #endregion
        }