private void SourceScriptBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { if (this.sourceScriptBackgroundWorker.CancellationPending) { e.Cancel = true; return; } SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo(); if (!this.ValidateSource(schemaInfo)) { return; } DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text); int dataBatchSize = SettingManager.Setting.DataBatchSize; GenerateScriptOption sourceScriptOption = new GenerateScriptOption() { ScriptOutputMode = GenerateScriptOutputMode.None, DataBatchSize = dataBatchSize }; this.SetGenerateScriptOption(sourceScriptOption); GenerateScriptMode scriptMode = this.GetGenerateScriptMode(); if (scriptMode == GenerateScriptMode.None) { MessageBox.Show("Please specify the script mode."); return; } DbInterpreter dbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption); string[] tableNames = schemaInfo.Tables.Select(item => item.Name).ToArray(); schemaInfo = dbInterpreter.GetSchemaInfo(tableNames); dbInterpreter.Subscribe(this); if (scriptMode.HasFlag(GenerateScriptMode.Schema)) { dbInterpreter.GenerateSchemaScripts(schemaInfo); } if (scriptMode.HasFlag(GenerateScriptMode.Data)) { dbInterpreter.GenerateDataScripts(schemaInfo); } MessageBox.Show(DONE); }
private async void GenerateScourceDbScripts() { SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo(); if (!this.ValidateSource(schemaInfo)) { return; } this.btnGenerateSourceScripts.Enabled = false; DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text); int dataBatchSize = SettingManager.Setting.DataBatchSize; DbInterpreterOption sourceScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.WriteToFile, DataBatchSize = dataBatchSize }; this.SetGenerateScriptOption(sourceScriptOption); GenerateScriptMode scriptMode = this.GetGenerateScriptMode(); if (scriptMode == GenerateScriptMode.None) { MessageBox.Show("Please specify the script mode."); return; } DbInterpreter dbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption); SelectionInfo selectionInfo = new SelectionInfo() { UserDefinedTypeNames = schemaInfo.UserDefinedTypes.Select(item => item.Name).ToArray(), TableNames = schemaInfo.Tables.Select(item => item.Name).ToArray(), ViewNames = schemaInfo.Views.Select(item => item.Name).ToArray() }; try { schemaInfo = await dbInterpreter.GetSchemaInfoAsync(selectionInfo); dbInterpreter.Subscribe(this); GenerateScriptMode mode = GenerateScriptMode.None; if (scriptMode.HasFlag(GenerateScriptMode.Schema)) { mode = GenerateScriptMode.Schema; dbInterpreter.GenerateSchemaScripts(schemaInfo); } if (scriptMode.HasFlag(GenerateScriptMode.Data)) { mode = GenerateScriptMode.Data; await dbInterpreter.GenerateDataScriptsAsync(schemaInfo); } this.OpenInExplorer(dbInterpreter.GetScriptOutputFilePath(mode)); MessageBox.Show(DONE); } catch (Exception ex) { this.HandleException(ex); } this.btnGenerateSourceScripts.Enabled = true; }
private async void GenerateScripts() { SchemaInfo schemaInfo = this.tvDbObjects.GetSchemaInfo(); if (!this.Validate(schemaInfo)) { return; } this.isBusy = true; this.btnGenerate.Enabled = false; DatabaseType dbType = this.useConnector ? this.dbConnectionProfile.DatabaseType : this.databaseType; DbInterpreterOption option = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.WriteToFile, SortObjectsByReference = true, GetTableAllObjects = true }; if (this.chkTreatBytesAsNull.Checked) { option.TreatBytesAsNullForReading = true; option.TreatBytesAsNullForExecuting = true; } else { if (dbType == DatabaseType.Oracle) { option.TreatBytesAsNullForReading = true; option.TreatBytesAsNullForExecuting = true; } else { option.TreatBytesAsHexStringForFile = true; } } this.SetGenerateScriptOption(option); GenerateScriptMode scriptMode = this.GetGenerateScriptMode(); if (scriptMode == GenerateScriptMode.None) { MessageBox.Show("Please specify the script mode."); return; } this.dbInterpreter = DbInterpreterHelper.GetDbInterpreter(dbType, this.connectionInfo, option); SchemaInfoFilter filter = new SchemaInfoFilter(); SchemaInfoHelper.SetSchemaInfoFilterValues(filter, schemaInfo); try { schemaInfo = await this.dbInterpreter.GetSchemaInfoAsync(filter); this.dbInterpreter.Subscribe(this); GenerateScriptMode mode = GenerateScriptMode.None; DbScriptGenerator dbScriptGenerator = DbScriptGeneratorHelper.GetDbScriptGenerator(this.dbInterpreter); if (scriptMode.HasFlag(GenerateScriptMode.Schema)) { mode = GenerateScriptMode.Schema; dbScriptGenerator.GenerateSchemaScripts(schemaInfo); } if (scriptMode.HasFlag(GenerateScriptMode.Data)) { mode = GenerateScriptMode.Data; await dbScriptGenerator.GenerateDataScriptsAsync(schemaInfo); } this.isBusy = false; ManagerUtil.OpenInExplorer(dbScriptGenerator.GetScriptOutputFilePath(mode)); MessageBox.Show(DONE, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { this.HandleException(ex); } this.btnGenerate.Enabled = true; }
private async Task ConvertorBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { if (this.convertorBackgroundWorker.CancellationPending) { e.Cancel = true; return; } SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo(); if (!this.ValidateSource(schemaInfo)) { return; } if (this.targetDbConnectionInfo == null) { MessageBox.Show("Target connection info is null."); return; } if (this.sourceDbConnectionInfo.Server == this.targetDbConnectionInfo.Server && this.sourceDbConnectionInfo.Database == this.targetDbConnectionInfo.Database) { MessageBox.Show("Source database cannot be equal to the target database."); return; } DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text); DatabaseType targetDbType = this.GetDatabaseType(this.cboTargetDB.Text); int dataBatchSize = SettingManager.Setting.DataBatchSize; GenerateScriptOption sourceScriptOption = new GenerateScriptOption() { ScriptOutputMode = GenerateScriptOutputMode.None, DataBatchSize = dataBatchSize }; GenerateScriptOption targetScriptOption = new GenerateScriptOption() { ScriptOutputMode = (GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize }; this.SetGenerateScriptOption(sourceScriptOption, targetScriptOption); targetScriptOption.GenerateIdentity = this.chkGenerateIdentity.Checked; GenerateScriptMode scriptMode = this.GetGenerateScriptMode(); if (scriptMode == GenerateScriptMode.None) { MessageBox.Show("Please specify the script mode."); return; } DbConvetorInfo source = new DbConvetorInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption) }; DbConvetorInfo target = new DbConvetorInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDbType, this.targetDbConnectionInfo, targetScriptOption) }; DbConvertor dbConvertor = new DbConvertor(source, target, null); dbConvertor.Option.GenerateScriptMode = scriptMode; dbConvertor.OnFeedback += Feedback; if (sourceDbType == DatabaseType.MySql) { source.DbInterpreter.Option.InQueryItemLimitCount = 2000; } if (targetDbType == DatabaseType.SqlServer) { target.DbOwner = this.txtTargetDbOwner.Text ?? "dbo"; } else if (targetDbType == DatabaseType.MySql) { target.DbInterpreter.Option.RemoveEmoji = true; } else if (targetDbType == DatabaseType.Oracle) { dbConvertor.Option.SplitScriptsToExecute = true; dbConvertor.Option.ScriptSplitChar = ';'; } DataTransferErrorProfile dataErrorProfile = null; if (this.chkPickup.Checked && scriptMode.HasFlag(GenerateScriptMode.Data)) { dataErrorProfile = DataTransferErrorProfileManager.GetProfile(this.sourceDbConnectionInfo, this.targetDbConnectionInfo); if (dataErrorProfile != null) { dbConvertor.Option.PickupTable = new Table() { Owner = schemaInfo.Tables.FirstOrDefault()?.Owner, Name = dataErrorProfile.SourceTableName }; } } this.btnExecute.Enabled = false; this.btnCancel.Enabled = true; bool success = false; try { await dbConvertor.ConvertAsync(schemaInfo, false); success = true; if (dataErrorProfile != null) { DataTransferErrorProfileManager.Remove(dataErrorProfile); } } catch (Exception ex) { string errMsg = ex.Message; sbFeedback.AppendLine("Error:" + ex.Message); if (ex.InnerException != null) { sbFeedback.AppendLine("Innser Exception:" + ex.InnerException.Message); } if (!string.IsNullOrEmpty(ex.StackTrace)) { sbFeedback.AppendLine(ex.StackTrace); } this.AppendErrorMessage(errMsg); this.txtMessage.SelectionStart = this.txtMessage.TextLength; this.txtMessage.ScrollToCaret(); this.btnExecute.Enabled = true; this.btnCancel.Enabled = false; if (ex is TableDataTransferException dataException) { DataTransferErrorProfileManager.Save(new DataTransferErrorProfile { SourceServer = dataException.SourceServer, SourceDatabase = dataException.SourceDatabase, SourceTableName = dataException.SourceTableName, TargetServer = dataException.TargetServer, TargetDatabase = dataException.TargetDatabase, TargetTableName = dataException.TargetTableName }); } MessageBox.Show(ex.Message); } LogHelper.Log(sbFeedback.ToString()); sbFeedback.Clear(); if (success) { this.btnExecute.Enabled = true; this.btnCancel.Enabled = false; this.txtMessage.AppendText(Environment.NewLine + DONE); MessageBox.Show(DONE); } }
private async Task CopyTable() { string name = this.txtName.Text.Trim(); if (!this.ValidateInputs()) { return; } try { GenerateScriptMode scriptMode = this.GetGenerateScriptMode(); bool isTableExisted = false; if (scriptMode.HasFlag(GenerateScriptMode.Schema)) { isTableExisted = await this.IsNameExisted(); } if (isTableExisted) { name = name + "_copy"; } SchemaInfo schemaInfo = new SchemaInfo(); schemaInfo.Tables.Add(this.Table); DatabaseType targetDatabaseType = this.rbAnotherDatabase.Checked ? this.ucConnection.DatabaseType : this.DatabaseType; ConnectionInfo targetConnectionInfo = this.rbAnotherDatabase.Checked ? this.targetDbConnectionInfo : this.ConnectionInfo; DbInterpreterOption sourceOption = new DbInterpreterOption(); DbInterpreterOption targetOption = new DbInterpreterOption(); targetOption.TableScriptsGenerateOption.GenerateIdentity = this.chkGenerateIdentity.Checked; DbConveterInfo source = new DbConveterInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(this.DatabaseType, this.ConnectionInfo, sourceOption) }; DbConveterInfo target = new DbConveterInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDatabaseType, targetConnectionInfo, targetOption) }; source.TableNameMappings.Add(this.Table.Name, name); this.btnExecute.Enabled = false; using (this.dbConverter = new DbConverter(source, target)) { this.dbConverter.Option.RenameTableChildren = isTableExisted || this.rbSameDatabase.Checked; this.dbConverter.Option.GenerateScriptMode = scriptMode; this.dbConverter.Option.BulkCopy = true; this.dbConverter.Option.UseTransaction = true; this.dbConverter.Option.ConvertComputeColumnExpression = true; this.dbConverter.Option.IgnoreNotSelfForeignKey = true; this.dbConverter.Subscribe(this); if (this.DatabaseType == DatabaseType.MySql) { source.DbInterpreter.Option.InQueryItemLimitCount = 2000; } if (this.DatabaseType != targetDatabaseType) { if (targetDatabaseType == DatabaseType.SqlServer) { target.DbOwner = "dbo"; } else if (targetDatabaseType == DatabaseType.MySql) { target.DbInterpreter.Option.RemoveEmoji = true; } } else { target.DbOwner = this.Table.Owner; } this.dbConverter.Option.SplitScriptsToExecute = true; await this.dbConverter.Convert(schemaInfo); if (!this.hasError && !this.dbConverter.HasError && !source.DbInterpreter.HasError && !target.DbInterpreter.HasError) { if (!this.dbConverter.CancelRequested) { MessageBox.Show("Copy finished", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Task has been canceled."); } } } } catch (Exception ex) { this.hasError = true; this.HandleException(ex); } finally { this.btnExecute.Enabled = true; } }