private DbObjectScripterResult ScriptObjects(DbObjectScripterArgs args)
        {
            DbObjectScripterResult result = new DbObjectScripterResult();

            _scripter = new DbObjectScripter(args.cp);
            _scripter.ScriptingProgress += new ScriptingProgressDelegate(OnScriptingProgress);
            _scripter.ObjectTypes        = args.objectTypes;
            _scripter.DbObjectSearchType = args.searchType;
            _scripter.SearchText         = args.searchText;

            string script = String.Empty;

            switch (args.destination)
            {
            case ScriptDestination.Window:
                result.errors = _scripter.ScriptObjects(out script);
                result.script = script;
                break;

            case ScriptDestination.File:
                result.errors = _scripter.ScriptObjects(out script);
                result.script = script;
                break;

            case ScriptDestination.Folder:
                result.errors = _scripter.ScriptObjectsToFolder(args.path);
                break;
            }
            return(result);
        }
        private void PostAction(DbObjectScripterResult result, bool isCancelled)
        {
            string errors = String.Empty;

            if (result.errors != null && result.errors.Count > 0)
            {
                if (!isCancelled)
                {
                    lblStatus.Text = "Completed with errors!";
                }

                foreach (Exception ex in result.errors)
                {
                    errors += "- " + ex.Message.Replace("\n", " ").Replace("\r", " ") + "\r\n";
                }
            }
            else
            {
                if (!isCancelled)
                {
                    lblStatus.Text = "Completed. Press \"Start\" to script objects.";
                }
            }

            switch (_destination)
            {
            case ScriptDestination.Window:
                string          caption = "Database Objects [" + _connParams.Server + " {" + _connParams.Database + "} ]";
                frmScriptEditor frm     = ScriptEditorFactory.Create(caption, result.script, _connParams);
                ScriptEditorFactory.ShowScriptEditor(frm);
                break;

            case ScriptDestination.File:
                File.AppendAllText(_destPath, result.script);
                break;

            case ScriptDestination.Folder:
                break;
            }

            if (!String.IsNullOrEmpty(errors))
            {
                GenericErrorDialog.ShowError("Error", "Scripting completed with errors! See details below.", errors);
            }
        }
        private void BackgroundWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                DisposeScripter();
                _isInProgress = false;

                btnStart.Enabled  = true;
                btnClose.Enabled  = true;
                btnStop.Enabled   = false;
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                groupBox3.Enabled = true;

                bool isCancelled = false;

                if (e.Cancelled)
                {
                    lblStatus.Text = "Scripting cancelled!";
                    isCancelled    = true;
                    return;
                }

                if (e.Error != null)
                {
                    lblStatus.Text = "Worker thread error occured!";
                    GenericErrorDialog.ShowError("Error", "Worker thread error occured.See details below.", e.Error.Message);
                    return;
                }

                DbObjectScripterResult result = e.Result as DbObjectScripterResult;
                if (result == null)
                {
                    lblStatus.Text = "Nothing was scripted!";
                    return;
                }

                PostAction(result, isCancelled);
            }
            finally
            {
                System.Media.SystemSounds.Exclamation.Play();
                this.BringToFront();
            }
        }