Пример #1
0
        private void GenerateScript()
        {
            this._scriptBody = String.Empty;
            try
            {
                SMO.Database smoDb = this._context.SmoServer.Databases[this._dbName];
                if (smoDb == null)
                {
                    this._scriptBody = String.Format(
                        "Database {0} not found. No T-SQL script generated.",
                        this._dbName);
                    Log.Warning(this._scriptBody);
                    return;
                }

                StringCollection scripts = smoDb.Script();
                foreach (var script in scripts)
                {
                    this._scriptBody += script;
                }
            }
            catch (Exception e)
            {
                this._scriptBody = String.Format(
                    "Error generating script for database {0}\n\n{1}",
                    this._dbName, e.ToString());
                Log.Error(this._scriptBody);
            }
        }
Пример #2
0
        private void Script()
        {
            if (!this.VerifyDatabase())
            {
                return;
            }

            if (this.OutputFilePath == null)
            {
                this.Log.LogError("OutputFilePath is required");
                return;
            }

            this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting Database: {0} to: {1}", this.DatabaseItem.ItemSpec, this.OutputFilePath.GetMetadata("FullPath")));
            Microsoft.SqlServer.Management.Smo.Database db = this.sqlServer.Databases[this.DatabaseItem.ItemSpec];

            // Script the database
            ScriptingOptions opt = new ScriptingOptions {
                Bindings = true, ClusteredIndexes = true, ExtendedProperties = true, FullTextCatalogs = true, FullTextIndexes = true, IncludeDatabaseContext = true, IncludeDatabaseRoleMemberships = true, IncludeHeaders = true, Indexes = true, LoginSid = true, Permissions = true, Triggers = true, XmlIndexes = true
            };

            opt.IncludeHeaders = false;
            opt.ToFileOnly     = true;
            opt.NoCollation    = false;
            opt.FileName       = this.OutputFilePath.GetMetadata("FullPath");
            db.Script(opt);

            // now we append to file
            opt.AppendToFile = true;

            foreach (Login o in this.sqlServer.Logins)
            {
                if (!o.IsSystemObject)
                {
                    this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting Login: {0}", o.Name));
                    o.Script(opt);
                }
            }

            foreach (Table o in db.Tables)
            {
                if (!o.IsSystemObject)
                {
                    this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting Table: {0}", o.Name));
                    o.Script(opt);
                }
            }

            foreach (Rule o in db.Rules)
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting Rule: {0}", o.Name));
                o.Script(opt);
            }

            foreach (Default o in db.Defaults)
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting Default: {0}", o.Name));
                o.Script(opt);
            }

            foreach (StoredProcedure o in db.StoredProcedures)
            {
                if (!o.IsSystemObject)
                {
                    this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting StoredProcedure: {0}", o.Name));
                    o.Script(opt);
                }
            }

            foreach (View o in db.Views)
            {
                if (!o.IsSystemObject)
                {
                    this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Scripting View: {0}", o.Name));
                    o.Script(opt);
                }
            }
        }