Click here to Skip to main content
15,898,020 members
Articles / Database Development / SQL Server / SQL Server 2008

SMO Tutorial 3 of n - Scripting

Rate me:
Please Sign up or sign in to vote.
4.93/5 (50 votes)
27 Feb 2011CPOL6 min read 106.1K   3.8K   74  
In this article, I will show you how to use the scripting functions of SMOs.
namespace SMOScripting
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextScript = new System.Windows.Forms.RichTextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.chkScriptHeaders = new System.Windows.Forms.CheckBox();
            this.chkScriptPermissions = new System.Windows.Forms.CheckBox();
            this.chkScriptExtendedProperties = new System.Windows.Forms.CheckBox();
            this.chkScriptIfNotExists = new System.Windows.Forms.CheckBox();
            this.chkScriptDrop = new System.Windows.Forms.CheckBox();
            this.chkDBContext = new System.Windows.Forms.CheckBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.chkStoredProcedures = new System.Windows.Forms.CheckBox();
            this.chkViews = new System.Windows.Forms.CheckBox();
            this.chkUserDefinedFunctions = new System.Windows.Forms.CheckBox();
            this.chkTables = new System.Windows.Forms.CheckBox();
            this.btnScript = new System.Windows.Forms.Button();
            this.cboDatabase = new System.Windows.Forms.ComboBox();
            this.cboServerVersion = new System.Windows.Forms.ComboBox();
            this.lblDatabase = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.chkScriptDatabase = new System.Windows.Forms.CheckBox();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.txtProgress = new System.Windows.Forms.TextBox();
            this.chkNoCollation = new System.Windows.Forms.CheckBox();
            this.chkNoFileGroups = new System.Windows.Forms.CheckBox();
            this.chkNoIdentities = new System.Windows.Forms.CheckBox();
            this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.SuspendLayout();
            // 
            // richTextScript
            // 
            this.richTextScript.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextScript.Location = new System.Drawing.Point(3, 3);
            this.richTextScript.Name = "richTextScript";
            this.richTextScript.Size = new System.Drawing.Size(631, 263);
            this.richTextScript.TabIndex = 0;
            this.richTextScript.Text = "";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.chkNoIdentities);
            this.groupBox1.Controls.Add(this.chkNoFileGroups);
            this.groupBox1.Controls.Add(this.chkNoCollation);
            this.groupBox1.Controls.Add(this.chkScriptDatabase);
            this.groupBox1.Controls.Add(this.groupBox2);
            this.groupBox1.Controls.Add(this.chkDBContext);
            this.groupBox1.Controls.Add(this.chkScriptDrop);
            this.groupBox1.Controls.Add(this.chkScriptIfNotExists);
            this.groupBox1.Controls.Add(this.chkScriptExtendedProperties);
            this.groupBox1.Controls.Add(this.chkScriptPermissions);
            this.groupBox1.Controls.Add(this.chkScriptHeaders);
            this.groupBox1.Location = new System.Drawing.Point(13, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(645, 143);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Scripting Options";
            // 
            // chkScriptHeaders
            // 
            this.chkScriptHeaders.AutoSize = true;
            this.chkScriptHeaders.Location = new System.Drawing.Point(7, 20);
            this.chkScriptHeaders.Name = "chkScriptHeaders";
            this.chkScriptHeaders.Size = new System.Drawing.Size(96, 17);
            this.chkScriptHeaders.TabIndex = 0;
            this.chkScriptHeaders.Text = "Script Headers";
            this.chkScriptHeaders.UseVisualStyleBackColor = true;
            // 
            // chkScriptPermissions
            // 
            this.chkScriptPermissions.AutoSize = true;
            this.chkScriptPermissions.Location = new System.Drawing.Point(7, 43);
            this.chkScriptPermissions.Name = "chkScriptPermissions";
            this.chkScriptPermissions.Size = new System.Drawing.Size(111, 17);
            this.chkScriptPermissions.TabIndex = 1;
            this.chkScriptPermissions.Text = "Script Permissions";
            this.chkScriptPermissions.UseVisualStyleBackColor = true;
            // 
            // chkScriptExtendedProperties
            // 
            this.chkScriptExtendedProperties.AutoSize = true;
            this.chkScriptExtendedProperties.Location = new System.Drawing.Point(122, 19);
            this.chkScriptExtendedProperties.Name = "chkScriptExtendedProperties";
            this.chkScriptExtendedProperties.Size = new System.Drawing.Size(151, 17);
            this.chkScriptExtendedProperties.TabIndex = 2;
            this.chkScriptExtendedProperties.Text = "Script Extended Properties";
            this.chkScriptExtendedProperties.UseVisualStyleBackColor = true;
            // 
            // chkScriptIfNotExists
            // 
            this.chkScriptIfNotExists.AutoSize = true;
            this.chkScriptIfNotExists.Location = new System.Drawing.Point(122, 43);
            this.chkScriptIfNotExists.Name = "chkScriptIfNotExists";
            this.chkScriptIfNotExists.Size = new System.Drawing.Size(132, 17);
            this.chkScriptIfNotExists.TabIndex = 3;
            this.chkScriptIfNotExists.Text = "Script IF NOT EXISTS";
            this.chkScriptIfNotExists.UseVisualStyleBackColor = true;
            // 
            // chkScriptDrop
            // 
            this.chkScriptDrop.AutoSize = true;
            this.chkScriptDrop.Location = new System.Drawing.Point(279, 19);
            this.chkScriptDrop.Name = "chkScriptDrop";
            this.chkScriptDrop.Size = new System.Drawing.Size(87, 17);
            this.chkScriptDrop.TabIndex = 4;
            this.chkScriptDrop.Text = "Script DROP";
            this.chkScriptDrop.UseVisualStyleBackColor = true;
            // 
            // chkDBContext
            // 
            this.chkDBContext.AutoSize = true;
            this.chkDBContext.Location = new System.Drawing.Point(279, 43);
            this.chkDBContext.Name = "chkDBContext";
            this.chkDBContext.Size = new System.Drawing.Size(110, 17);
            this.chkDBContext.TabIndex = 5;
            this.chkDBContext.Text = "Script DB Context";
            this.chkDBContext.UseVisualStyleBackColor = true;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.cboServerVersion);
            this.groupBox2.Controls.Add(this.cboDatabase);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.lblDatabase);
            this.groupBox2.Controls.Add(this.chkTables);
            this.groupBox2.Controls.Add(this.chkUserDefinedFunctions);
            this.groupBox2.Controls.Add(this.chkViews);
            this.groupBox2.Controls.Add(this.chkStoredProcedures);
            this.groupBox2.Location = new System.Drawing.Point(4, 66);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(633, 73);
            this.groupBox2.TabIndex = 6;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Objects";
            // 
            // chkStoredProcedures
            // 
            this.chkStoredProcedures.AutoSize = true;
            this.chkStoredProcedures.Location = new System.Drawing.Point(6, 19);
            this.chkStoredProcedures.Name = "chkStoredProcedures";
            this.chkStoredProcedures.Size = new System.Drawing.Size(114, 17);
            this.chkStoredProcedures.TabIndex = 1;
            this.chkStoredProcedures.Text = "Stored Procedures";
            this.chkStoredProcedures.UseVisualStyleBackColor = true;
            // 
            // chkViews
            // 
            this.chkViews.AutoSize = true;
            this.chkViews.Location = new System.Drawing.Point(6, 42);
            this.chkViews.Name = "chkViews";
            this.chkViews.Size = new System.Drawing.Size(54, 17);
            this.chkViews.TabIndex = 2;
            this.chkViews.Text = "Views";
            this.chkViews.UseVisualStyleBackColor = true;
            // 
            // chkUserDefinedFunctions
            // 
            this.chkUserDefinedFunctions.AutoSize = true;
            this.chkUserDefinedFunctions.Location = new System.Drawing.Point(152, 19);
            this.chkUserDefinedFunctions.Name = "chkUserDefinedFunctions";
            this.chkUserDefinedFunctions.Size = new System.Drawing.Size(137, 17);
            this.chkUserDefinedFunctions.TabIndex = 3;
            this.chkUserDefinedFunctions.Text = "User Defined Functions";
            this.chkUserDefinedFunctions.UseVisualStyleBackColor = true;
            // 
            // chkTables
            // 
            this.chkTables.AutoSize = true;
            this.chkTables.Location = new System.Drawing.Point(152, 42);
            this.chkTables.Name = "chkTables";
            this.chkTables.Size = new System.Drawing.Size(58, 17);
            this.chkTables.TabIndex = 4;
            this.chkTables.Text = "Tables";
            this.chkTables.UseVisualStyleBackColor = true;
            // 
            // btnScript
            // 
            this.btnScript.Location = new System.Drawing.Point(583, 486);
            this.btnScript.Name = "btnScript";
            this.btnScript.Size = new System.Drawing.Size(75, 23);
            this.btnScript.TabIndex = 3;
            this.btnScript.Text = "Script";
            this.btnScript.UseVisualStyleBackColor = true;
            this.btnScript.Click += new System.EventHandler(this.btnScript_Click);
            // 
            // cboDatabase
            // 
            this.cboDatabase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboDatabase.FormattingEnabled = true;
            this.cboDatabase.Location = new System.Drawing.Point(359, 17);
            this.cboDatabase.Name = "cboDatabase";
            this.cboDatabase.Size = new System.Drawing.Size(180, 21);
            this.cboDatabase.TabIndex = 5;
            // 
            // cboServerVersion
            // 
            this.cboServerVersion.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboServerVersion.FormattingEnabled = true;
            this.cboServerVersion.Location = new System.Drawing.Point(359, 40);
            this.cboServerVersion.Name = "cboServerVersion";
            this.cboServerVersion.Size = new System.Drawing.Size(180, 21);
            this.cboServerVersion.TabIndex = 6;
            // 
            // lblDatabase
            // 
            this.lblDatabase.AutoSize = true;
            this.lblDatabase.Location = new System.Drawing.Point(304, 20);
            this.lblDatabase.Name = "lblDatabase";
            this.lblDatabase.Size = new System.Drawing.Size(56, 13);
            this.lblDatabase.TabIndex = 7;
            this.lblDatabase.Text = "Database:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(315, 43);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(45, 13);
            this.label1.TabIndex = 8;
            this.label1.Text = "Version:";
            // 
            // chkScriptDatabase
            // 
            this.chkScriptDatabase.AutoSize = true;
            this.chkScriptDatabase.Location = new System.Drawing.Point(398, 19);
            this.chkScriptDatabase.Name = "chkScriptDatabase";
            this.chkScriptDatabase.Size = new System.Drawing.Size(102, 17);
            this.chkScriptDatabase.TabIndex = 7;
            this.chkScriptDatabase.Text = "Script Database";
            this.chkScriptDatabase.UseVisualStyleBackColor = true;
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(13, 157);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(645, 323);
            this.tabControl1.TabIndex = 4;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.txtProgress);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(637, 297);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Progress";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.richTextScript);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(637, 269);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Script";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // txtProgress
            // 
            this.txtProgress.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtProgress.Location = new System.Drawing.Point(3, 3);
            this.txtProgress.Multiline = true;
            this.txtProgress.Name = "txtProgress";
            this.txtProgress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtProgress.Size = new System.Drawing.Size(631, 291);
            this.txtProgress.TabIndex = 0;
            // 
            // chkNoCollation
            // 
            this.chkNoCollation.AutoSize = true;
            this.chkNoCollation.Location = new System.Drawing.Point(398, 43);
            this.chkNoCollation.Name = "chkNoCollation";
            this.chkNoCollation.Size = new System.Drawing.Size(113, 17);
            this.chkNoCollation.TabIndex = 8;
            this.chkNoCollation.Text = "Script No Collation";
            this.chkNoCollation.UseVisualStyleBackColor = true;
            // 
            // chkNoFileGroups
            // 
            this.chkNoFileGroups.AutoSize = true;
            this.chkNoFileGroups.Location = new System.Drawing.Point(518, 19);
            this.chkNoFileGroups.Name = "chkNoFileGroups";
            this.chkNoFileGroups.Size = new System.Drawing.Size(118, 17);
            this.chkNoFileGroups.TabIndex = 9;
            this.chkNoFileGroups.Text = "Script No FileGroup";
            this.chkNoFileGroups.UseVisualStyleBackColor = true;
            // 
            // chkNoIdentities
            // 
            this.chkNoIdentities.AutoSize = true;
            this.chkNoIdentities.Location = new System.Drawing.Point(518, 43);
            this.chkNoIdentities.Name = "chkNoIdentities";
            this.chkNoIdentities.Size = new System.Drawing.Size(115, 17);
            this.chkNoIdentities.TabIndex = 10;
            this.chkNoIdentities.Text = "Script No Identities";
            this.chkNoIdentities.UseVisualStyleBackColor = true;
            // 
            // backgroundWorker1
            // 
            this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(667, 521);
            this.Controls.Add(this.tabControl1);
            this.Controls.Add(this.btnScript);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.RichTextBox richTextScript;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.CheckBox chkScriptPermissions;
        private System.Windows.Forms.CheckBox chkScriptHeaders;
        private System.Windows.Forms.CheckBox chkDBContext;
        private System.Windows.Forms.CheckBox chkScriptDrop;
        private System.Windows.Forms.CheckBox chkScriptIfNotExists;
        private System.Windows.Forms.CheckBox chkScriptExtendedProperties;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.CheckBox chkTables;
        private System.Windows.Forms.CheckBox chkUserDefinedFunctions;
        private System.Windows.Forms.CheckBox chkViews;
        private System.Windows.Forms.CheckBox chkStoredProcedures;
        private System.Windows.Forms.Button btnScript;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lblDatabase;
        private System.Windows.Forms.ComboBox cboServerVersion;
        private System.Windows.Forms.ComboBox cboDatabase;
        private System.Windows.Forms.CheckBox chkScriptDatabase;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TextBox txtProgress;
        private System.Windows.Forms.CheckBox chkNoCollation;
        private System.Windows.Forms.CheckBox chkNoFileGroups;
        private System.Windows.Forms.CheckBox chkNoIdentities;
        private System.ComponentModel.BackgroundWorker backgroundWorker1;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Marwin Cassovia Soft
Slovakia Slovakia
My name is Robert Kanasz and I have been working with ASP.NET, WinForms and C# for several years.
MCSD - Web Applications
MCSE - Data Platform
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
MCSA - SQL Server 2012
MCTS - .NET Framework 3.5, ASP.NET Applications
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
- .NET Framework 4, Data Access
- .NET Framework 4, Service Communication Applications
- .NET Framework 4, Web Applications
MS - Programming in HTML5 with JavaScript and CSS3 Specialist

Open source projects: DBScripter - Library for scripting SQL Server database objects


Please, do not forget vote

Comments and Discussions