Click here to monitor SSC

Richard Mitchell

Project Manager - Red Gate Software - http://cloudservices.red-gate.com
Development Factotum at Red Gate Software Ltd. Now working on a hosted system for maintaining cloud applications http://cloudservices.red-gate.com

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
Latest articles
A first look at SQL Server 2012 Availability Group Wait Statistics
 If you are trouble-shooting an AlwaysOn Availability Group topology, a study of the wait statistics... Read more...

SQL Server Prefetch and Query Performance
 Prefetching can make a surprising difference to SQL Server query execution times where there is a high... Read more...

SSIS Basics: Setting Up Your Initial Package
 When working with databases, the use of SQL Server Integration Services (SSIS) is a skill that often... Read more...

Checking Out SQL Backup Pro 7’s New Automatic Backup Verification
 Wouldn't it be great to offload the daily chore of checking the integrity of your production... Read more...

Chuck Lathrope: DBA of the Day
 Chuck Lathrope was a finalist for the Exceptional DBA of the Year award in 2009. We contacted him to... Read more...