コード例 #1
0
        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);
        }
コード例 #2
0
        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
                });
            }
        }
コード例 #3
0
        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);
            }
        }