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

Library for scripting SQL Server database objects with examples

Rate me:
Please Sign up or sign in to vote.
4.93/5 (138 votes)
8 Nov 2011CPOL13 min read 233.8K   13.3K   252  
This article is about library for scripting SQL Server database objects and examples representing how this library can be used.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using ObjectHelper;

namespace DBCompare
{
    public partial class Login : Form
    {

        Server server1 = null;
        Server server2 = null;

        public Login()
        {
            InitializeComponent();
        }

        private void lblDatabase1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Login_Resize(object sender, EventArgs e)
        {
            pnlSettings.Location = new Point(
                this.ClientSize.Width / 2 - pnlSettings.Size.Width / 2,
                this.ClientSize.Height / 2 - pnlSettings.Size.Height / 2);
            pnlSettings.Anchor = AnchorStyles.None;
        }

        private void Login_Load(object sender, EventArgs e)
        {
            DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {

                    cboServer1.Items.Add(dr["Name"].ToString());
                    cboServer2.Items.Add(dr["Name"].ToString());

                }
            }
        }


        private void cmdRefreshServer1_Click(object sender, EventArgs e)
        {
            RefreshServerList(cboServer1);
        }

        private void cmdRefreshServer2_Click(object sender, EventArgs e)
        {
            RefreshServerList(cboServer1);
        }

        private void rbWindowsAuthentication1_Click(object sender, EventArgs e)
        {
            txtUser1.Enabled = false;
            lblUserName1.Enabled = false;
            txtPassword1.Enabled = false;
            lblPassword1.Enabled = false;
        }

        private void rbSQLServerAuthentication1_CheckedChanged(object sender, EventArgs e)
        {
            txtUser1.Enabled = true;
            lblUserName1.Enabled = true;
            txtPassword1.Enabled = true;
            lblPassword1.Enabled = true;
        }

        private void rbWindowsAuthentication2_CheckedChanged(object sender, EventArgs e)
        {
            txtUser2.Enabled = false;
            lblUserName2.Enabled = false;
            txtPassword2.Enabled = false;
            lblPassword2.Enabled = false;
        }

        private void rbSQLServerAuthentication2_CheckedChanged(object sender, EventArgs e)
        {
            txtUser2.Enabled = true;
            lblUserName2.Enabled = true;
            txtPassword2.Enabled = true;
            lblPassword2.Enabled = true;
        }

        private void cboDatabase1_Click(object sender, EventArgs e)
        {
            if (cboDatabase1.Items.Count == 0)
            {
                if (rbWindowsAuthentication1.Checked)
                {
                    RefreshDatabaseList(cboDatabase1, cboServer1.Text);
                }
                else
                {
                    RefreshDatabaseList(cboDatabase1, cboServer1.Text, txtUser1.Text, txtPassword1.Text);
                }
            }
        }






        private void RefreshServerList(ComboBox cbo)
        {
            cbo.Items.Clear();
            DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    cbo.Items.Add(dr["Name"].ToString());
                }
            }
        }

        private void RefreshDatabaseList(ComboBox cbo, string server)
        {
            try
            {
                cbo.Items.Clear();
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = server;
                Server srv = new Server(conn);

                foreach (Database db in srv.Databases)
                {
                    cbo.Items.Add(db.Name);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void RefreshDatabaseList(ComboBox cbo, string server, string login, string password)
        {
            try
            {
                cbo.Items.Clear();
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = server;
                conn.LoginSecure = false;
                conn.Login = login;
                conn.Password = password;
                Server srv = new Server(conn);

                foreach (Database db in srv.Databases)
                {
                    cbo.Items.Add(db.Name);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboDatabase2_Click(object sender, EventArgs e)
        {
            if (cboDatabase2.Items.Count == 0)
            {
                if (rbWindowsAuthentication2.Checked)
                {
                    RefreshDatabaseList(cboDatabase2, cboServer2.Text);
                }
                else
                {
                    RefreshDatabaseList(cboDatabase2, cboServer2.Text, txtUser2.Text, txtPassword2.Text);
                }
            }
        }

        private void cboServer1_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboDatabase1.Items.Clear();
        }

        private void cboServer2_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboDatabase2.Items.Clear();
        }

        private void cmdCompare_Click(object sender, EventArgs e)
        {


            if (rbSQLServerAuthentication1.Checked == true)
            {
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = cboServer1.Text;
                conn.LoginSecure = false;
                conn.Login = txtUser1.Text;
                conn.Password = txtPassword1.Text;
                server1 = new Server(conn);
            }
            else
            {
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = cboServer1.Text;
                server1 = new Server(conn);
            }


            if (rbSQLServerAuthentication2.Checked == true)
            {
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = cboServer2.Text;
                conn.LoginSecure = false;
                conn.Login = txtUser2.Text;
                conn.Password = txtPassword2.Text;
                server2 = new Server(conn);
            }
            else
            {
                ServerConnection conn = new ServerConnection();
                conn.ServerInstance = cboServer2.Text;
                server2 = new Server(conn);
            }
            try
            {
                string srv1 = server1.Information.Version.ToString();
                string srv2 = server2.Information.Version.ToString();
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            /*
            ObjectFetch objFetch = new ObjectFetch(server1.Name, cboDatabase1.Text, txtUser1.Text, txtPassword1.Text,server2.Name, cboDatabase2.Text,txtUser2.Text,txtPassword2.Text);
            //objFetch.Parent = this.Parent;
            objFetch.Show();
            this.Hide();
             */
        }

        public ObjectHelper.ScriptingOptions GetScriptiongOptions()
        {
            ObjectHelper.ScriptingOptions so = new ObjectHelper.ScriptingOptions();
            so.Aggregates = chkAggregates.Checked;
            so.ApplicationRoles = chkApplicationRoles.Checked;
            so.Assemblies = chkAssemblies.Checked;
            so.BrokerPriorities = chkBrokerPriorities.Checked;
            so.ClusteredIndexes = chkClusteredIndexes.Checked;
            so.ClrTriggers = chkCLRTriggers.Checked;
            so.ClrUserDefinedFunctions = chkCLRUserDefinedFunctions.Checked;
            so.CheckConstraints = chkCheckConstraints.Checked;
            so.Collation = chkCollation.Checked;
            so.Contracts = chkContracts.Checked;
            so.DatabaseRoles = chkDatabaseRoles.Checked;
            so.DataCompression = chkDataCompression.Checked;
            so.Defaults = chkDefaults.Checked;
            so.DdlTriggers = chkDDLTriggers.Checked;
            so.DefaultConstraints = chkDefaultConstraints.Checked;
            so.DmlTriggers = chkDMLTriggers.Checked;
            so.ForeignKeys = chkForeignKeys.Checked;
            so.FullTextCatalogPath = chkFullTextCatalogPath.Checked;
            so.FullTextCatalogs = chkFullTextCatalogs.Checked;
            so.FullTextIndexes = chkFullTextIndexes.Checked;
            so.FullTextStopLists = chkFullTextStopLists.Checked;
            so.MessageTypes = chkMessageTypes.Checked;
            so.NoFileStream = chkNoFileStream.Checked;
            so.NoIdentities = chkNoIdentities.Checked;
            so.NonClusteredIndexes = chkNonClusteredIndexes.Checked;
            so.PartitionFunctions = chkPartitionFunctions.Checked;
            so.PartitionSchemes = chkPartitionSchemes.Checked;
            so.PrimaryKeys = chkPrimaryKeys.Checked;
            so.RemoteServiceBindings = chkRemoteServiceBindings.Checked;
            so.Routes = chkRoutes.Checked;
            so.Rules = chkRules.Checked;
            so.Schemas = chkSchemas.Checked;
            so.ServiceQueues = chkServiceQueues.Checked;
            so.Services = chkServices.Checked;
            so.SqlUserDefinedFunctions = chkSQLUserDefinedFunctions.Checked;
            so.StoredProcedures = chkStoredProcedures.Checked;
            so.Synonyms = chkSynonyms.Checked;
            so.Tables = chkTables.Checked;            
            so.UniqueConstraints = chkUniqueConstraints.Checked;
            so.UserDefinedDataTypes = chkUserDefinedDataTypes.Checked;
            so.UserDefinedTableTypes = chkUserDefinedTableTypes.Checked;
            so.UserDefinedTypes = chkUserDefinedTypes.Checked;
            so.Users = chkUsers.Checked;
            so.Views = chkViews.Checked;
            so.XmlSchemaCollections = chkXMLSchemaCollections.Checked;

            return so;
        }


        public Server GetServer1()
        {
            return server1;
        }

        public Server GetServer2()
        {
            return server2;
        }

        public string GetDatabase1()
        {
            return cboDatabase1.Text;
        }

        public string GetDatabase2()
        {
            return cboDatabase2.Text;
        }

        private void cboDatabase1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}

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