public async Task Convert() { DatabaseType sourceDbType = this.sourceInterpreter.DatabaseType; DatabaseType targetDbType = this.targetInterpreter.DatabaseType; int dataBatchSize = 500; DbInterpreterOption sourceScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.WriteToString, DataBatchSize = dataBatchSize }; DbInterpreterOption targetScriptOption = new DbInterpreterOption() { ScriptOutputMode = (GenerateScriptOutputMode.WriteToFile | GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize }; this.sourceInterpreter.Option = sourceScriptOption; this.targetInterpreter.Option = targetScriptOption; GenerateScriptMode scriptMode = GenerateScriptMode.Schema | GenerateScriptMode.Data; DbConvetorInfo source = new DbConvetorInfo() { DbInterpreter = sourceInterpreter }; DbConvetorInfo target = new DbConvetorInfo() { DbInterpreter = targetInterpreter }; try { using (DbConvertor dbConvertor = new DbConvertor(source, target)) { dbConvertor.Option.GenerateScriptMode = scriptMode; dbConvertor.Subscribe(this); if (sourceDbType == DatabaseType.MySql) { source.DbInterpreter.Option.InQueryItemLimitCount = 2000; } if (targetDbType == DatabaseType.SqlServer) { target.DbOwner = "dbo"; } else if (targetDbType == DatabaseType.MySql) { target.DbInterpreter.Option.RemoveEmoji = true; } dbConvertor.Option.SplitScriptsToExecute = true; FeedbackHelper.EnableLog = true; await dbConvertor.Convert(); } } catch (Exception ex) { string msg = ExceptionHelper.GetExceptionDetails(ex); this.Feedback(new FeedbackInfo() { InfoType = FeedbackInfoType.Error, Message = msg }); } }
static void TestConvertor(DbInterpreter sourceInterpreter, DbInterpreter targetInterpreter) { DatabaseType sourceDbType = sourceInterpreter.DatabaseType; DatabaseType targetDbType = targetInterpreter.DatabaseType; int dataBatchSize = 500; GenerateScriptOption sourceScriptOption = new GenerateScriptOption() { ScriptOutputMode = GenerateScriptOutputMode.WriteToString, DataBatchSize = dataBatchSize }; GenerateScriptOption targetScriptOption = new GenerateScriptOption() { ScriptOutputMode = (GenerateScriptOutputMode.WriteToFile | GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize }; sourceInterpreter.Option = sourceScriptOption; targetInterpreter.Option = targetScriptOption; GenerateScriptMode scriptMode = GenerateScriptMode.Schema | GenerateScriptMode.Data; DbConvetorInfo source = new DbConvetorInfo() { DbInterpreter = sourceInterpreter }; DbConvetorInfo target = new DbConvetorInfo() { DbInterpreter = targetInterpreter }; 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 = "dbo"; } else if (targetDbType == DatabaseType.MySql) { target.DbInterpreter.Option.RemoveEmoji = true; } else if (targetDbType == DatabaseType.Oracle) { dbConvertor.Option.SplitScriptsToExecute = true; dbConvertor.Option.ScriptSplitChar = ';'; } try { dbConvertor.Convert(); } catch (Exception ex) { string msg = ex.Message; if (ex is TableDataTransferException) { TableDataTransferException dataException = ex as TableDataTransferException; msg = $"Error occurs when sync data of table {dataException.TargetTableName}:{msg}"; } msg += Environment.NewLine + "StackTrace:" + Environment.NewLine + ex.StackTrace; Feedback(new FeedbackInfo() { InfoType = FeedbackInfoType.Error, Message = msg }); } }
private async Task Convert() { 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; DbInterpreterOption sourceScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.None, DataBatchSize = dataBatchSize }; DbInterpreterOption targetScriptOption = new DbInterpreterOption() { ScriptOutputMode = (GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize }; this.SetGenerateScriptOption(sourceScriptOption, targetScriptOption); if (this.chkGenerateSourceScripts.Checked) { sourceScriptOption.ScriptOutputMode = sourceScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile; } if (this.chkOutputScripts.Checked) { targetScriptOption.ScriptOutputMode = targetScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile; } 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) }; try { using (dbConvertor = new DbConvertor(source, target)) { dbConvertor.Option.GenerateScriptMode = scriptMode; dbConvertor.Option.BulkCopy = this.chkBulkCopy.Checked; dbConvertor.Option.ExecuteScriptOnTargetServer = this.chkExecuteOnTarget.Checked; dbConvertor.Option.UseTransaction = this.chkUseTransaction.Checked; dbConvertor.Subscribe(this); 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; } dbConvertor.Option.SplitScriptsToExecute = true; this.btnExecute.Enabled = false; this.btnCancel.Enabled = true; await dbConvertor.Convert(schemaInfo); } } catch (Exception ex) { this.hasError = true; this.HandleException(ex); } if (!this.hasError) { this.btnExecute.Enabled = true; this.btnCancel.Enabled = false; if (!this.dbConvertor.CancelRequested) { this.txtMessage.AppendText(Environment.NewLine + DONE); MessageBox.Show(DONE); } else { MessageBox.Show("Task has been canceled."); } } }
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); } }