Richard Mitchell

Software Engineer - Red Gate Software
Engine programmer and factotum.

SQL Data Compare 6 API

Published Monday, November 12, 2007 8:42 AM

OK I admit it. During the development of SQL Data Compare 6 in order to get the product out on time we had to sacrifice doing the SQL Toolkit 6 help ( it didn't involve chickens). So I thought I'd write a little bit to get you started on the v6 stuff as it seems to be coming up.

Those of you who've played with the latest version will appreciate how thoroughly cool comparing to a backup of a database is. So what we've done is given you that coolness in the latest version of the SQL Toolkit API. All you have to do is include a vast selection of dll references into your project and put a couple in the executable directory and you can make SQL Data Compare sit up and beg.

Dll References
RedGate.BackupReader (Native backup files)
RedGate.BackupReader.SqbReader (Red Gate SQL Backup files only)
RedGate.Licensing.Client (has to be available for compile)
RedGate.SQL.Shared
RedGate.SQLCompare.ASTParser
RedGate.SQLCompare.BackupReader (Backup SQL Compare engine)
RedGate.SQLCompare.Engine
RedGate.SQLCOmpare.Rewriter
RedGate.SQLDataCompare.Engine (Your friend and mine)

Dlls with the executable
rglz.dll (for compressed Red Gate SQL Backup files)
zlib1.dll (for compressed Red Gate SQL Backup files)
RedGate.BackupReader.CryptoHelper.dll (for encrypted Red Gate SQL Backup files)
I know it looks like a scary amount of files to include in your project but really you can experiment and if you're not using Red Gate SQL Backup files you can ommit quite a few of the dependencies and is may still work - I've not really tested it.

So we add all those lovely new files to our project. Make sure when you add them that they all come from the SQL Data Compare 6 directory only. This is because the version of the SQL Compare engine that shipped with SQL Compare 6 isn't the same as is shipped with SQL Data Compare.

The code is actually remarkably simple to write once you've got the references set up. It goes something like this...
// First register the backup file
BackupDatabase backupDB = new BackupDatabase();
backupDB.RegisterForDataCompare(new string[] { "c:\\widgetdev.bak" }null);
/*
* Use this code to enumerate the backup sets in the file - you can pass one of
* these objects in the call to RegisterForDataCompare() above

IList<BackupSet> backupSets = backupDB.GetBackupSets(
new string[] { "c:\\multiwidgetdev.bak" });
foreach (BackupSet backupSet in backupSets)
{
   Console.WriteLine("{0} {1} {2} {3} {4}", backupSet.DatabaseName,
                                            backupSet.ServerName,
                                            backupSet.Size,
                                            backupSet.BackupType,
                                            backupSet.StartDate);
}
*/
// Secondly register the live database

Database liveDB = new Database();
liveDB.RegisterForDataCompare(
new 
ConnectionProperties("localhost""WidgetLive""sa"""));
// Create the mappings between the two schemas
SchemaMappings mappings = new SchemaMappings();
mappings.CreateMappings(backupDBliveDB);
// Compare the database
ComparisonSession session = new ComparisonSession();
session.CompareDatabases(backupDBliveDBmappings);
Job done. You can see all of the overrides for BackupDatabase.RegisterForDataCompare() which allow you to specify files, passwords, backup sets and data sources.

The other main thing that we've added to the API is on the Project class. You've always been able to load a project via the API but not all the settings were applied, now however you have the lovely call you can make which is Project.ReplayUserActions(ref SchemaMappings). What this will do is set all the custom comparison keys, WHERE clauses and all the rest of the settings that make up the rest of the project not just which database you connect to. So you can set your project up using the UI and then run it via the API without issue.

Mainly the rest of the API remains unaffected and there is really no issue in migrating your projects from v5 to v6. Hope this has given you a taste of what is available now, go forth and code most fruitfully.

Comments

No Comments
You need to sign in to comment on this blog

















<November 2007>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
Virtual Exchange Servers
 Microsoft now supports running Exchange Server 2007 in server virtualization environments, not just on... Read more...

Virtualizing Exchange: points for discussion
 With the increasing acceptance of the use of Virtualization as a means of providing server... Read more...

Encouraging .NET Reflector Add-ins
 Jason Haley is well-known for the resources he's provided to developers who wish to extend Reflector's... Read more...

Using .NET Reflector Add-ins
 .NET Reflector by itself is great, but it really comes into its own with the help of some add-ins. Here... Read more...

Unique Experiences!
 You'd have thought that a unique constraint was an easy concept - Not a bit of it; it can cause a lot... Read more...