Click here to Skip to main content
Email Password   helpLost your password?

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 

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGood work. Simple tool just what lots of us need.
ronp
1:02 18 Jan '09  
Keep up the good work.
Like lots of us I know how to do this and never get around to doing what you have done with this little app.
Just the ticket as they say.
I changed the tables select sql to sort in alpha order of the table name but other than that is is good.
GeneralRe: Good work. Simple tool just what lots of us need.
Amir Mehrabi J.
23:00 18 Jan '09  
Thanks for your comment

Amir Mehrabi Jorshary

GeneralGood Work - but
Charles K. Kincaid
7:34 5 Jan '09  
Like so many SQL compares it requires me to connect to both servers at the same time. I want to be able to connect to my master server, save the current schemas to a file, send the file to the production site, use my tool to compare the production server to the file.

I have production sites that I can only connect to via VPN. Once connected I can't attach to SQL servers on my side of the VPN. I have some clients that their firewall blocks me from connecting out to SQL servers so even if I do the old trick of VPN to the client and use remote desktop I can't connect back to my master box.

--
ATB

Charles Kincaid

GeneralRe: Good Work - but
bgs264
23:29 5 Jan '09  
I know Red Gate's SQL Compare will let you save a snapshot of a database, then use the snapshot file to compare against... But for your example you'd need SQL Compare installed on both machines, once to generate the snapshop then once to do the comparison, and that would prove expensive.

I had a problem similar to yours, when connected through a VPN, I could not see SQL servers on my side. It was because it was using the remote DNS. Try to connect to your SQL server by IP address not name; alternatively if you dig around in the VPN settings, you may find "Use default gateway from the remote system" or something more appropriately named, try to uncheck that, that solved my problem.

Good luck

Regards,
Ben

NewsRe: Good Work - but
Amir Mehrabi J.
5:40 8 Jan '09  
This idea sounds good
I Do it in the next generation! Wink
thanks for yoyr idea.

Amir Mehrabi Jorshary

NewsRe: Good Work - but
Amir Mehrabi J.
7:12 8 Jan '09  
Now you can save the schema of your database and then retrieve it.
Good Luck!

Amir Mehrabi Jorshary

GeneralCheck out OpenDBDiff
grundt
13:49 30 Dec '08  
Sorry to rain on your parade, but I think OpenDBDiff[^] does everything you're wanting, and perhaps more.
I've been using it for about a year and it works nicely.
But, I like the looks of your UI ... perhaps the two of you could cooperate on an "OpenDBDiff 2.0"
GeneralRe: Check out OpenDBDiff
Amir 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
glittle2
11:29 4 Jan '09  
AdeptSQL Diff at http://www.adeptsql.com/ is also a great tool. It generates the SQL to sync the two database structures, but lets you review and modify the script as you wish.
AnswerRe: Check out OpenDBDiff
Amir Mehrabi J.
0:43 5 Jan '09  
AdeptSQLDiff , It's Cost is $240 for personal usage to $1499 WTF for Site...
WOW My program is useful and when will be upgrade it must be a great Program!.. Big Grin

Thanks for your attention

Amir Mehrabi Jorshary

GeneralRe: Check out OpenDBDiff
ronp
0:59 18 Jan '09  
Your last comment summs it up. For what yours does it is very good and very usefull
Keep up the good work.
Like lots of us I know how to do this and never get around to doing what you have done with this little app.
Just the ticket as they say.
GeneralRe: Check out OpenDBDiff
dojohansen
1:31 20 Jul '09  
OpenDBDiff does appear to do quite a bit more. It's licensing is more restrictive (GPL) though, and I'm a little unsure what one can and cannot do with it. I only read part of the license agreement but it seems to me that it is possible to use the program as part of the dev process with commercial software and distribute the scripts generated from that process (in other words, such scripts are not "derivative works"). Do you know if my interpretation is correct?

I wish people who write licenses would sometimes include explicitly some things you *can* do without violating their rights in a separate section...
GeneralGood but needs lot of improvements
Kant
8:41 29 Dec '08  
Here are my suggestions.

1. Populate the databases (instead entering manually)
2. Compare the views, stored procedures, triggers
3. Remove the arabic stuff (or translate that to English) for the taskbar.
4. I couldn't export the differences. ('Export SQL' button is disabled even though there are differences in the databases)

I haven't read the source code yet so I couldn't provide any suggestions there.

రవికాంత్

GeneralRe: Good but needs lot of improvements
Amir Mehrabi J.
19:34 29 Dec '08  
thanks for your comment
in this version of my program i want to show difference only.
in the future version i want to export difference.
in the taskbar i write some PERSIAN notes , that is my company name.

i'm very glad to see your comment about my source code.

Amir Mehrabi Jorshary

GeneralMy vote of 2
Srinath G Nath
4:39 29 Dec '08  
The exe attached with this article is not working. Not able to fetch the database details or comparsion.
GeneralRe: My vote of 2
Amir Mehrabi J.
19:37 29 Dec '08  
I think other can run my EXE successfully.
Do you install .NET 2.0 in your computer?

Amir Mehrabi Jorshary

Generalyour idea is good but...
Guillaume Leparmentier
4:36 29 Dec '08  
Why not using ADO.net features to get table and/or column informations?


using(SqlConnection cnx = SqlConnection("" string="">))
{
using (DataTable fullSchema = cnx.GetSchema("Tables"))
{
foreach (DataRow row in fullSchema.Rows)
{
Console.WriteLine("Table owner : {0}", row[1]);
Console.WriteLine("Table name : {0}", row[2]);

// something more usefull

DataTable tableStructure = null;
using (SqlCommand cmd = new SqlCommand("SELECT * FROM " + row[2], cnx))
{
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
{
tableStructure = reader.GetSchemaTable();
}
}

// and so on...

}
}
}


instead of querying SQLServer management tables?
GeneralRe: your idea is good but...
Amir Mehrabi J.
19:39 29 Dec '08  
Thanks for your Idea.
I goal in this article was using Tables of SQL Server.
I wanna say there is alternative for fetching TABLE's Detail.
but thanks for your comment

Amir Mehrabi Jorshary

GeneralMy vote of 1
Paul E. Bible
4:32 29 Dec '08  
What about other data objects?
GeneralRe: My vote of 1
Amir Mehrabi J.
4:34 29 Dec '08  
What is your means about "other data objects"?

Amir Mehrabi Jorshary

GeneralRe: My vote of 1
jszczur
23:23 6 Jan '09  
e.g. trigger, function, procedure, view ...
GeneralRe: My vote of 1
supercat9
8:46 8 Jan '09  
jszczur wrote:
e.g. trigger, function, procedure, view ...


In most situations, there will be no reason for any of those other things to change. While it may be useful to have a utility to ensure that indeed they have not, normally all changes of interest will have occurred to the records, rather than to the database structure.

I'll admit that the opening paragraph does not make things clear, but I think part of the idea of this application is to aid in validation of code to update a database. If there isn't any code that alters any triggers, functions, procedures, etc., there probably isn't much need to ensure that those things haven't been altered.
GeneralGood Article!
Member 4432804
4:21 29 Dec '08  
Amir,
Good job. Check out Sql Management Objects (SMO) when you get a chance, using them can definitely help with projects like this!
AnswerRe: Good Article!
Amir Mehrabi J.
4:25 29 Dec '08  
Thanks for your comment

Amir Mehrabi Jorshary

GeneralRe: Good Article!
Syed M Hussain
8:05 29 Dec '08  
I like your idea. Great article, keep up the updates.



Last Updated 8 Jan 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010