Click here to Skip to main content
15,867,686 members
Articles / Database Development / SQL Server
Article

Database Schema Comparison Utility

Rate me:
Please Sign up or sign in to vote.
4.74/5 (32 votes)
16 Oct 2006CPOL2 min read 147.1K   4K   133   53
An article describing a way to find the differences between two database instances
Sample Image - DatabaseCompare.jpg

Introduction

When most developers develop database code (including tables, views, stored procedures and functions, they tend to develop in a development environment and then migrate those changes later to other servers (staging, production, whatever). While some of us are good about keeping track of those changes, the majority of us are not.

Red-Gate Software offers a tool, SQL Compare to compare the schema of two databases and show them to the user. This tool is not inexpensive, but it works very well.

This article provides the basic functionality used to do the same thing as SQL Compare, at a fraction of the price.

Background

SQL 2000 and above uses a series of system tables to store the definition of all objects in a database. The important objects (for this project) are:

  • sysobjects - Stores a list of all objects in the database (MSDN Reference)
  • syscolumns - Stores a list of the columns of the objects in the database (MSDN Reference)
  • syscomments - Stores the definition of objects in the database (MSDN Reference)

Using the Code

The code is broken into two projects:

  • DatabaseCompare (the Winforms project)
  • DatabaseCompare.Domain (the database access project)

There is nothing ground breaking in the Winforms project. I did use a couple delegates to make the UI more responsive when performing long tasks.

The Domain project contains classes that actually perform the work in gathering the schema of a database and comparing the two databases.

The code is fairly self explanatory, the main entry points to the Domain code are Database.TestConnection(), Database.LoadObjects(), and Database.CompareTo().

Points of Interest

Longer function/stored procedure/view definitions are maintained in multiple rows in the syscomments database and must be retrieved like so:

C#
using( SqlCommand command = conn.CreateCommand() )
{
    command.CommandText = "select text from syscomments where id=@id";
    command.Parameters.Add( "@id", this.Id );
    using ( SqlDataReader reader = command.ExecuteReader() )
    {
        while( reader.Read() )
            textDefinition += reader.GetString( 0 ).Trim().ToLower();
    }
}

This code does not create drop/create scripts for tables, but that is a fairly trivial exercise for you to do if you need that functionality.

Using the sysdepends table, you could determine what the dependencies for the objects are, in case you are creating CREATE scripts and need to know what to create first.

License

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


Written By
Web Developer
United States United States
I'm the owner and principal consultant of a small (one man!) shop here in the Dallas, Texas area.

I mostly work with Microsoft technologies, but run a lot of Linux at home.

Comments and Discussions

 
GeneralGreat project! Pin
bvermilion10-Oct-06 12:04
bvermilion10-Oct-06 12:04 
GeneralRe: Great project! Pin
hexy12-Oct-06 16:35
hexy12-Oct-06 16:35 
GeneralRe: Great project! Pin
Sean Goodpasture13-Oct-06 5:47
Sean Goodpasture13-Oct-06 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.