private void HandleBcp(SqlGenerator sqlGenerator, BcpGenerator bcpGenerator, TableMappingDefinition mappingDefinition, Dictionary<string, string> bcpExportCommands, Dictionary<string, string> bcpImportCommands)
        {
            Logger.Info("Handling BCP");

            SourceTable srcTable = mappingDefinition.SourceTable;
            DestinationTable destTable = mappingDefinition.DestinationTable;
            string bcpPackagePath = Path.Combine(_packageOutputPath, CONVERSION_PACKAGE_FOLDER);

            // Generate and save format file
            Logger.Info("Generating BCP format file for EXPORT");
            string bcpExportFormatFile = bcpGenerator.GenerateFormatFile(BcpDirection.Export);
            string exportFmtFileName = string.Format("{0}_EXPORT.fmt", destTable.Name);
            string exportFmtFilePath = Path.Combine(CONVERSION_PACKAGE_FOLDER, exportFmtFileName);
            SaveToFile(bcpPackagePath, exportFmtFileName, bcpExportFormatFile);

            Logger.Info("Generating BCP format file for IMPORT");
            string bcpImportFormatFile = bcpGenerator.GenerateFormatFile(BcpDirection.Import);
            string importFmtFileName = string.Format("{0}_IMPORT.fmt", destTable.Name);
            string importFmtFilePath = Path.Combine(CONVERSION_PACKAGE_FOLDER, importFmtFileName);
            SaveToFile(bcpPackagePath, importFmtFileName, bcpImportFormatFile);

            // Generate export, import commands
            Logger.Info("Generating BCP Export/Import commands");
            string bcpSelect = sqlGenerator.GenerateSelectStatement();
            string dataFileName = string.Format("{0}-{1}_BCP.txt", srcTable.Name, destTable.Name);
            string dataFilePath = Path.Combine(CONVERSION_PACKAGE_FOLDER, dataFileName);
            File.Create(Path.Combine(_packageOutputPath, dataFilePath));
            var bcpExportCommand = bcpGenerator.GenerateExportCommand(bcpSelect, exportFmtFilePath, dataFilePath);
            bcpExportCommands.Add(dataFileName, bcpExportCommand);
            var bcpImportCommand = bcpGenerator.GenerateImportCommand(importFmtFilePath, dataFilePath);
            bcpImportCommands.Add(dataFileName, bcpImportCommand);
        }
        private void HandleMaxTextUpdate(SqlGenerator sqlGenerator, TableMappingDefinition mappingDefinition, List<string> scriptNames)
        {
            Logger.Info("Handling max text");

            SourceTable srcTable = mappingDefinition.SourceTable;
            DestinationTable destTable = mappingDefinition.DestinationTable;
            string postSqlPackagePath = Path.Combine(_packageOutputPath, CONVERSION_PACKAGE_FOLDER);

            Logger.Info("Generating script for updating max text fields");
            string script = sqlGenerator.GenerateVarCharMaxUpdateScript();
            if(!string.IsNullOrEmpty(script))
            {
                var fileName = string.Format("{0}-{1}_TEXT.sql", srcTable.Name, destTable.Name);
                SaveToFile(postSqlPackagePath, fileName, script);
                scriptNames.Add(fileName);
            }
        }
        private string DoConversion()
        {
            Logger.Info("Doing conversion");

            List<string> scriptNames = new List<string>();
            Dictionary<string, string> bcpExportCommands = new Dictionary<string, string>();
            Dictionary<string, string> bcpImportCommands = new Dictionary<string, string>();

            _destinationDatabase.Tables.ForEach(t =>
            {
                DestinationTable destinationTable = t;
                SourceTable sourceTable = null;
                try
                {
                    Logger.Info("Processing " + destinationTable.Name);

                    TableMappingConfiguration mappingConfig = GetTableMappingConfig(destinationTable.Name);
                    sourceTable = GetSourceTable(destinationTable.Name, mappingConfig);
                    var mappingDefinition = CreateTableMappingDefinition(sourceTable, destinationTable, mappingConfig);
                    SqlGenerator sqlGenerator = new SqlGenerator(mappingDefinition, _options.CheckExistData);
                    BcpGenerator bcpGenerator = new BcpGenerator(_options.ServerName, _options.InstanceName, mappingDefinition, _options.BcpMode);

                    HandleBcp(sqlGenerator, bcpGenerator, mappingDefinition, bcpExportCommands, bcpImportCommands);
                    HandleMaxTextUpdate(sqlGenerator, mappingDefinition, scriptNames);
                    if(_options.BlobHandling)
                    {
                        HandleBlobUpdate(sqlGenerator, mappingDefinition, scriptNames);
                    }
                }
                catch (AppException ex)
                {
                    if (ex.ErrorCode == AppExceptionCodes.DATABASE_ERROR_TABLE_NOT_FOUND)
                    {
                        Logger.Warn(destinationTable.Name + " not mapped");
                    }
                }
            });

            // Generate bat file
            string exportScript = BatGenerator.GenerateBcpExecuteBat(bcpExportCommands);
            SaveToFile(_packageOutputPath, "BCP_Export.bat", exportScript);
            Logger.Info("BCP Exporting data");
            Process.Start(new ProcessStartInfo { WorkingDirectory = _packageOutputPath, FileName = "BCP_Export.bat" });

            string importScript = BatGenerator.GenerateBcpExecuteBat(bcpImportCommands);
            //SaveToFile(_packageOutputPath, "BCP_Import.bat", importScript);

            List<string> scriptPaths = scriptNames.Select(s => Path.Combine(CONVERSION_PACKAGE_FOLDER, s)).ToList();
            string updateScript = BatGenerator.GenerateSqlExecuteBat(scriptPaths, _options.ServerName, _options.InstanceName, _options.Username, _options.Password);
            //SaveToFile(_packageOutputPath, "Update_Blob_Text.bat", updateScript);

            string batScript = importScript + Environment.NewLine + updateScript;
            return batScript;
        }