Red Gate forums :: View topic - Sql comparison from backups
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SQL Comparison SDK 10
SQL Comparison SDK 10 forum

Sql comparison from backups

Search in SQL Comparison SDK 10 forum
Post new topic   Reply to topic
Jump to:  
Author Message
zaliis



Joined: 18 Mar 2014
Posts: 8

PostPosted: Wed Mar 19, 2014 5:45 pm    Post subject: Sql comparison from backups Reply with quote

I am trying to perform a data compare with both the source and target databases both be backups. When I enumerate the TableDifferences collection it is empty. Can anyone see what I am doing wrong in the code? I am using latest version of the SDK.

Code:

        static void Test2()
        {
            Database sourceDatabase = GetBackupDatabaseSource(@"D:\Database Compare Project\DataBases\Source\FPM-FSE6-UserData_Db_201403122016_full.BAK");
            //Database targetDatabase = GetBackupDatabaseSource(@"D:\Database Compare Project\DataBases\Target\FPM-Production-UserData_Db_201403122016_full.BAK");
            Database targetDatabase = GetBackupDatabaseSource(@"D:\Database Compare Project\DataBases\Target\FPM-Production-RateSynch_Db_201403122016_full.BAK");
            // Create the schema mappings used to compare the database
            SchemaMappings mappings = new SchemaMappings();
            mappings.CreateMappings(sourceDatabase, targetDatabase);
            using (ComparisonSession session = new ComparisonSession())
            {
                session.Status += new StatusEventHandler(StatusCallback);
                session.CompareDatabases(sourceDatabase, targetDatabase, mappings);

                foreach (TableMapping mapping in mappings.TableMappings)
                {
                    TableDifference difference = session.TableDifferences[mapping.Obj1.FullyQualifiedName];
                    if (difference != null)
                    {
                        using (Reader different = difference.ResultsStore.GetReader(Row.RowType.All))
                        {
                            while (different.GetEnumerator().MoveNext())
                            {
                                Row currentRow = different.GetRow(0);
                            }
                        }
                    }

                }
            }
        }
        static BackupSetDatabase GetBackupDatabaseSource(params String[] backupFiles)
        {
            List<string> files = new List<string>();
            files.AddRange(backupFiles);
            BackupSetDatabase backupDatabase = new BackupSetDatabase();
            IList<RedGate.Shared.SQL.BackupReaderInterfaces.IBackupSet> backupSets = backupDatabase.GetBackupSets(files, null);
            String[] passwords = new string[0];
            BackupDatabaseSource backupDatabaseSource = new BackupDatabaseSource(files, passwords, backupSets[0]);
            //backupDatabaseSource.Files = files;

            backupDatabase.Status += new StatusEventHandler(StatusCallback);
            Console.WriteLine("Registering backup " + files[0]);
            try
            {
                //backupDatabase.Register(backupDatabaseSource.ToConnectionProperties(), Options.Default);
                backupDatabase.RegisterForDataCompare(backupDatabaseSource.ToConnectionProperties(), Options.Default);
            }
            catch (RedGate.BackupReader.SqbReader.PasswordProtectedException)
            {
                /* If we have reached this block, the backup is an encrypted
                  * SQL Backup file and we must ask for the password!
                  * NB the decryption requires RedGate.BackupReader.CryptoHelper.dll
                  * and zlib1.dll to be copied into your output directory */
                Console.WriteLine("This SQL Backup file is password protected.\r\nPlease enter the password:");
                string sqbPassword = Console.ReadLine();
                backupDatabaseSource.Passwords.Add(sqbPassword);
                backupDatabase.Register(backupDatabaseSource.ToConnectionProperties(), Options.Default);
            }
            catch (RedGate.BackupReader.BackupReaderException brx)
            {
                // If we have reached this block, something generally bad has happened.
                Console.WriteLine("Could not register backup: " + brx.Message);
            }
            return backupDatabase;
        }
Back to top
View user's profile Send private message
zaliis



Joined: 18 Mar 2014
Posts: 8

PostPosted: Wed Mar 19, 2014 6:25 pm    Post subject: I have it working Reply with quote

Modified the code as follows and I am now getting differences.


Code:

        static void Test2()
        {
            Database sourceDatabase = GetBackupDatabaseSource(@"D:\Database Compare Project\DataBases\Source\FPM-FSE6-UserData_Db_201403122016_full.BAK");
            Database targetDatabase = GetBackupDatabaseSource(@"D:\Database Compare Project\DataBases\Target\FPM-Production-RateSynch_Db_201403122016_full.BAK");
            Differences diffs = sourceDatabase.CompareWith(targetDatabase, Options.Default);
            System.Diagnostics.Debugger.Break();
        }
        static BackupSetDatabase GetBackupDatabaseSource(params String[] backupFiles)
        {
            List<string> files = new List<string>();
            files.AddRange(backupFiles);
            BackupSetDatabase backupDatabase = new BackupSetDatabase();
            IList<RedGate.Shared.SQL.BackupReaderInterfaces.IBackupSet> backupSets = backupDatabase.GetBackupSets(files, null);
            String[] passwords = new string[0];
            BackupDatabaseSource backupDatabaseSource = new BackupDatabaseSource(files, passwords, backupSets[0]);
            //backupDatabaseSource.Files = files;

            backupDatabase.Status += new StatusEventHandler(StatusCallback);
            Console.WriteLine("Registering backup " + files[0]);
            try
            {
                //backupDatabase.Register(backupDatabaseSource.ToConnectionProperties(), Options.Default);
                backupDatabase.RegisterForDataCompare(backupDatabaseSource.ToConnectionProperties(), Options.Default);
            }
            catch (RedGate.BackupReader.SqbReader.PasswordProtectedException)
            {
                /* If we have reached this block, the backup is an encrypted
                  * SQL Backup file and we must ask for the password!
                  * NB the decryption requires RedGate.BackupReader.CryptoHelper.dll
                  * and zlib1.dll to be copied into your output directory */
                Console.WriteLine("This SQL Backup file is password protected.\r\nPlease enter the password:");
                string sqbPassword = Console.ReadLine();
                backupDatabaseSource.Passwords.Add(sqbPassword);
                backupDatabase.Register(backupDatabaseSource.ToConnectionProperties(), Options.Default);
            }
            catch (RedGate.BackupReader.BackupReaderException brx)
            {
                // If we have reached this block, something generally bad has happened.
                Console.WriteLine("Could not register backup: " + brx.Message);
            }
            return backupDatabase;
        }
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group