Click here to Skip to main content
Licence CPOL
First Posted 29 Dec 2008
Views 29,007
Downloads 518
Bookmarked 68 times

Database Comparator

By Amir Mehrabi-Jorshary | 8 Jan 2009
With this tool, you can compare two databases and generate a difference report.
 
Part of The SQL Zone sponsored by
See Also
1 vote, 5.6%
1
1 vote, 5.6%
2
4 votes, 22.2%
3
3 votes, 16.7%
4
9 votes, 50.0%
5
4.11/5 - 18 votes
1 removed
μ 3.95, σa 2.16 [?]

Introduction 

Nowadays, Microsoft SQL Server is one of the most popular DBMS in the world and most companies and software engineers use it as a database. During development and maintenance of a program, it is unclear whether a database has been changed. For example, when you install your program on the customer's site, after some time if you want to upgrade this software / database, you must have more significant data because if you can't upgrade the database properly, then your program doesn't work properly. For solving this problem, I think that if there is a tool that can compare the main database (on the site of the developer) with the slave database (on the customer site) and find the difference between them, it can help us to maintain a program properly.

Design

For design and implementation of this tool, I used C# .NET and specified my tool to Microsoft SQL Server 2000. The user interface of my program has 2 sections:

  1. Master section 
  2. Slave section

In the Master section, the program retrieves information from the main site on developer side and in the Slave section, it retrieves information from the customer site. Information that is retrieved from the databases involves Table List, Table Names, Column Names, Column Types, etc. For the first step for retrieving Tables List, in the SQL Server 2000, all information about tables and views is stored in a table with name "sysobjects". For retrieving information from this table, I use a query as follows:

SELECT     name AS TableName, id FROM         dbo.sysobjects WHERE     (xtype = 'u')

After getting a list of tables in the two databases, now I can compare the list of tables and find out which one of the tables is a new table. Each table that exists in the master list and does not exist in the slave list is a new table. But for comparing two databases, this is not enough because may be some table's columns have been changed and by this method, we can't find these changes. For completing my method, I must compare each table one by one and retrieve information of each table. For retrieving information of tables, I use a query as follows:

SELECT     dbo.syscolumns.id, dbo.syscolumns.name AS Col, dbo.systypes.name AS TypeName,
dbo.sysobjects.name AS TableName, dbo.syscolumns.length AS Len FROM         dbo.syscolumn
s INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xusertype INNER JOIN dbo
.sysobjects ON dbo.syscolumns.id = dbo.sysobjects.id WHERE     (dbo.sysobjects.xtype = 'u
') AND dbo.sysobjects.name like  [TABLENAME] 

After retrieving information such as column name, column type and column size, now I can compare and find a better result.

User Interface

For using my program, on the left side of my program you can see the master section and on the right side you see the slave section. For each section you must type server name, user id and password for connecting. After connecting to the databases, now you must just click on compare! 

For better virtual comparing, I use a tree for each database. At the first level of tree, you can see the list of tables and by expanding each node, you can see its information such as column name, columns type, etc.

SQL1.JPG

SQL2.JPG

In this version of my program, the software just displays the difference but for future work, I want to use this information software to produce a SQL script file that can save into a file and customer can execute this file for upgrading his/her database.

Next Version (1.1) 

In this version , You can save the schema of your database into a file (*.schema)
and in the later you can fetch the schema of your database offline.
in the next picture you can see what ever you expected:

SQL3.JPG

Future Work 

In future, this may be able to generate a SQL report.  

History 

  • 29th December, 2008: Initial post 
  • 8th Jan, 2009 : Version 1.1 Released

License

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

About the Author

Amir Mehrabi-Jorshary

Software Developer (Senior)
Omid Farda IT Company
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member
Master of Science in Computer Engineering

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood work. Simple tool just what lots of us need. Pinmemberronp1:02 18 Jan '09  
GeneralRe: Good work. Simple tool just what lots of us need. PinmemberAmir Mehrabi J.23:00 18 Jan '09  
GeneralGood Work - but PinmemberCharles K. Kincaid7:34 5 Jan '09  
GeneralRe: Good Work - but Pinmemberbgs26423:29 5 Jan '09  
NewsRe: Good Work - but PinmemberAmir Mehrabi J.5:40 8 Jan '09  
NewsRe: Good Work - but PinmemberAmir Mehrabi J.7:12 8 Jan '09  
GeneralCheck out OpenDBDiff Pinmembergrundt13:49 30 Dec '08  
GeneralRe: Check out OpenDBDiff PinmemberAmir Mehrabi J.19:44 30 Dec '08  
Thanks for your comment
I saw DBDiff. It look like a good program.
My Program is in Version 1.0 and I try in next version complete my program to work better.
 
Amir Mehrabi Jorshary

GeneralRe: Check out OpenDBDiff Pinmemberglittle211:29 4 Jan '09  
AnswerRe: Check out OpenDBDiff PinmemberAmir Mehrabi J.0:43 5 Jan '09  
GeneralRe: Check out OpenDBDiff Pinmemberronp0:59 18 Jan '09  
GeneralRe: Check out OpenDBDiff Pinmemberdojohansen1:31 20 Jul '09  
GeneralGood but needs lot of improvements PinmemberKant8:41 29 Dec '08  
GeneralRe: Good but needs lot of improvements PinmemberAmir Mehrabi J.19:34 29 Dec '08  
GeneralMy vote of 2 PinmemberSrinath G Nath4:39 29 Dec '08  
GeneralRe: My vote of 2 PinmemberAmir Mehrabi J.19:37 29 Dec '08  
Generalyour idea is good but... PinmemberGuillaume Leparmentier4:36 29 Dec '08  
GeneralRe: your idea is good but... PinmemberAmir Mehrabi J.19:39 29 Dec '08  
GeneralMy vote of 1 PinmemberPaul E. Bible4:32 29 Dec '08  
GeneralRe: My vote of 1 PinmemberAmir Mehrabi J.4:34 29 Dec '08  
GeneralRe: My vote of 1 Pinmemberjszczur23:23 6 Jan '09  
GeneralRe: My vote of 1 Pinmembersupercat98:46 8 Jan '09  
GeneralGood Article! PinmemberMember 44328044:21 29 Dec '08  
AnswerRe: Good Article! PinmemberAmir Mehrabi J.4:25 29 Dec '08  
GeneralRe: Good Article! PinmemberSyed M Hussain8:05 29 Dec '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 8 Jan 2009
Article Copyright 2008 by Amir Mehrabi-Jorshary
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid