Click here to Skip to main content
15,885,853 members
Articles / Desktop Programming / Windows Forms

SQL Server Database Comparison Tool

Rate me:
Please Sign up or sign in to vote.
4.95/5 (163 votes)
3 Jun 2011CPOL12 min read 337.7K   25.7K   281  
In this article, I will show you how to create a basic DB schema comparison tool for SQL Server 2005 and SQL Server 2008.
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;

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 ScriptingOptions GetScriptiongOptions()
{
    ScriptingOptions so = new ScriptingOptions();

    so.ClusteredIndexes = chkClusteredIndexes.Checked;
    so.DriChecks = chkDriChecks.Checked;
    so.DriClustered = chkDriClustered.Checked;
    so.DriDefaults = chkDriDefaults.Checked;
    so.DriForeignKeys = chkDriForeignKeys.Checked;
    so.Indexes = chkIndexes.Checked;
    so.DriIndexes = chkDriIndexes.Checked;
    so.DriNonClustered = chkDriNonClustered.Checked;
    so.DriPrimaryKey = chkDriPrimaryKeys.Checked;
    so.DriUniqueKeys = chkDriUniqueKeys.Checked;
    so.DriWithNoCheck = chkDriWithNoCheck.Checked;
    so.ExtendedProperties = chkExtendedProperties.Checked;
    so.FullTextCatalogs = chkFullTextCatalogs.Checked;
    so.FullTextIndexes = chkFullTextIndexes.Checked;
    so.FullTextStopLists = chkFullTextStopLists.Checked;
    so.NoAssemblies = chkNoAssemblies.Checked;
    so.NoCollation = chkNoCollation.Checked;
    so.NoIdentities = chkNoIdentities.Checked;
    so.NoFileStream = chkNoFileStream.Checked;
    so.NoFileGroup = chkNoFileGroup.Checked;
    so.NoFileStreamColumn = chkNoFileStreamColumn.Checked;
    so.NoIndexPartitioningSchemes = chkNoIndexPartitionSchemes.Checked;
    so.NoTablePartitioningSchemes = chkNoTablePartitionSchemes.Checked;
    so.Permissions = false;
    so.Triggers = chkTriggers.Checked;
    so.NonClusteredIndexes = chkNonClusteredIndexes.Checked;
    so.XmlIndexes = chkXMLIndexes.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)
        {

        }
      
    }
}

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