public async Task Convert() { DatabaseType sourceDbType = this.sourceInterpreter.DatabaseType; DatabaseType targetDbType = this.targetInterpreter.DatabaseType; int dataBatchSize = 500; DbInterpreterOption sourceScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.WriteToString }; DbInterpreterOption targetScriptOption = new DbInterpreterOption() { ScriptOutputMode = (GenerateScriptOutputMode.WriteToFile | GenerateScriptOutputMode.WriteToString) }; this.sourceInterpreter.Option = sourceScriptOption; this.targetInterpreter.Option = targetScriptOption; GenerateScriptMode scriptMode = GenerateScriptMode.Schema | GenerateScriptMode.Data; DbConveterInfo source = new DbConveterInfo() { DbInterpreter = sourceInterpreter }; DbConveterInfo target = new DbConveterInfo() { DbInterpreter = targetInterpreter }; try { using (DbConverter dbConverter = new DbConverter(source, target)) { dbConverter.Option.GenerateScriptMode = scriptMode; dbConverter.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; } dbConverter.Option.SplitScriptsToExecute = true; FeedbackHelper.EnableLog = true; await dbConverter.Convert(); } } catch (Exception ex) { string msg = ExceptionHelper.GetExceptionDetails(ex); this.Feedback(new FeedbackInfo() { InfoType = FeedbackInfoType.Error, Message = msg }); } }
private async Task Convert() { SchemaInfo schemaInfo = this.tvDbObjects.GetSchemaInfo(); 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.useSourceConnector ? this.sourceDbProfile.DatabaseType : this.sourceDatabaseType; DatabaseType targetDbType = this.targetDbProfile.DatabaseType; DbInterpreterOption sourceScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.None, SortObjectsByReference = true, GetTableAllObjects = true }; DbInterpreterOption targetScriptOption = new DbInterpreterOption() { ScriptOutputMode = (GenerateScriptOutputMode.WriteToString) }; this.SetGenerateScriptOption(sourceScriptOption, targetScriptOption); if (this.chkGenerateSourceScripts.Checked) { sourceScriptOption.ScriptOutputMode = sourceScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile; } if (this.chkOutputScripts.Checked) { targetScriptOption.ScriptOutputMode = targetScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile; } if (this.chkTreatBytesAsNull.Checked) { sourceScriptOption.TreatBytesAsNullForReading = true; targetScriptOption.TreatBytesAsNullForExecuting = true; } targetScriptOption.TableScriptsGenerateOption.GenerateIdentity = this.chkGenerateIdentity.Checked; GenerateScriptMode scriptMode = this.GetGenerateScriptMode(); if (scriptMode == GenerateScriptMode.None) { MessageBox.Show("Please specify the script mode."); return; } DbConveterInfo source = new DbConveterInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption) }; DbConveterInfo target = new DbConveterInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDbType, this.targetDbConnectionInfo, targetScriptOption) }; try { using (this.dbConverter = new DbConverter(source, target)) { this.dbConverter.Option.GenerateScriptMode = scriptMode; this.dbConverter.Option.BulkCopy = this.chkBulkCopy.Checked; this.dbConverter.Option.ExecuteScriptOnTargetServer = this.chkExecuteOnTarget.Checked; this.dbConverter.Option.UseTransaction = this.chkUseTransaction.Checked; this.dbConverter.Option.SkipScriptError = this.chkSkipScriptError.Checked; this.dbConverter.Option.PickupTable = this.chkPickup.Checked; this.dbConverter.Option.ConvertComputeColumnExpression = this.chkComputeColumn.Checked; this.dbConverter.Option.OnlyCommentComputeColumnExpressionInScript = this.chkOnlyCommentComputeExpression.Checked; this.dbConverter.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; } this.dbConverter.Option.SplitScriptsToExecute = true; this.btnExecute.Enabled = false; this.btnCancel.Enabled = true; await this.dbConverter.Convert(schemaInfo); if (!this.hasError && !this.dbConverter.HasError && !source.DbInterpreter.HasError && !target.DbInterpreter.HasError) { this.btnExecute.Enabled = true; this.btnCancel.Enabled = false; if (!this.dbConverter.CancelRequested) { this.txtMessage.AppendText(Environment.NewLine + DONE); MessageBox.Show(DONE, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Task has been canceled."); } } } } catch (Exception ex) { this.hasError = true; this.HandleException(ex); } }
public async Task Translate(DatabaseType sourceDbType, DatabaseType targetDbType, DatabaseObject dbObject, ConnectionInfo connectionInfo, TranslateHandler translateHandler = null) { DbInterpreterOption sourceScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.None }; DbInterpreterOption targetScriptOption = new DbInterpreterOption() { ScriptOutputMode = GenerateScriptOutputMode.WriteToString }; DbConveterInfo source = new DbConveterInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, connectionInfo, sourceScriptOption) }; DbConveterInfo target = new DbConveterInfo() { DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDbType, new ConnectionInfo(), sourceScriptOption) }; using (DbConverter dbConverter = new DbConverter(source, target)) { dbConverter.Option.OnlyForTranslate = true; dbConverter.Option.GenerateScriptMode = GenerateScriptMode.Schema; dbConverter.Option.ExecuteScriptOnTargetServer = false; dbConverter.Option.ConvertComputeColumnExpression = true; dbConverter.Subscribe(this.observer); if (translateHandler != null) { dbConverter.OnTranslated += translateHandler; } if (targetDbType == DatabaseType.SqlServer) { target.DbOwner = "dbo"; } SchemaInfo schemaInfo = new SchemaInfo(); if (dbObject is Table) { schemaInfo.Tables.Add(dbObject as Table); } else if (dbObject is View) { schemaInfo.Views.Add(dbObject as DatabaseInterpreter.Model.View); } else if (dbObject is Function) { schemaInfo.Functions.Add(dbObject as Function); } else if (dbObject is Procedure) { schemaInfo.Procedures.Add(dbObject as Procedure); } else if (dbObject is TableTrigger) { schemaInfo.TableTriggers.Add(dbObject as TableTrigger); } await dbConverter.Convert(schemaInfo); } }
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; } }