Comparing backup to live database
From RedGateWiki
<--- Back to SQL Comparison SDK
- using System;
- using System.Collections.Generic;
- using System.Text;
- using RedGate.Shared.SQL;
- using RedGate.SQLCompare.Engine;
- using RedGate.SQLCompare.Engine.ReadFromBackup;
- using RedGate.BackupReader;
- using RedGate.SQLDataCompare.Engine;
- using RedGate.SQLDataCompare.Engine.ResultsStore;
- using RedGate.Shared.Utils;
- namespace SDCCompareToBackup
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- // First register the backup file
- Console.WriteLine("Registering backup");
- BackupDatabaseSource backupDS = new BackupDatabaseSource();
- backupDS.Files.Add(@"C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008R2\MSSQL\Backup\WidgetDev.bak");
- BackupSetDatabase backupDB = new BackupSetDatabase();
- backupDB.Status += new StatusEventHandler(StatusCallBack);
- backupDB.RegisterForDataCompare(backupDS.ToConnectionProperties(),Options.Default);
- // Secondly register the live database
- Console.WriteLine("Registering live database");
- Database liveDB = new Database();
- liveDB.Status += new StatusEventHandler(StatusCallBack);
- liveDB.RegisterForDataCompare(new ConnectionProperties("PS-BRIAND\\SQL2008R2", "WidgetLive"));
- // Create the mappings between the two schemas
- SchemaMappings mappings = new SchemaMappings();
- mappings.CreateMappings(backupDB, liveDB);
- // Compare the database
- ComparisonSession session = new ComparisonSession();
- session.Status += new StatusEventHandler(StatusCallBack);
- session.CompareDatabases(backupDB, liveDB, mappings,SessionSettings.Default);
- // Show the results in visual form
- foreach (TableMapping mapping in mappings.TableMappings)
- {
- TableDifference difference = session.TableDifferences[mapping.Obj1.FullyQualifiedName];
- //display the different records
- // Any tables that couldn't be compared we don't output the results
- if (difference == null)
- continue;
- //loop through all the rows
- foreach (Row row in difference.ResultsStore)
- {
- //go through the non same records
- if (row.Type != Row.RowType.Same)
- {
- Console.WriteLine("{0} Row {1} type {2}", mapping.Obj1.FullyQualifiedName, row.Index, row.Type.ToString());
- int i = 0;
- foreach (FieldPair field in difference.ResultsStore.Fields)
- {
- int field1 = field.OrdinalInResults1;
- int field2 = field.OrdinalInResults2;
- if (field1 != field2)
- {
- //get the values
- object value1 = row.Values[field1];
- object value2 = row.Values[field2];
- if (value1 == null)
- value1 = "NULL";
- if (value2 == null)
- value2 = "NULL";
- Console.WriteLine("{0}\t{1}\t{2}\t{3}", field.Field(false).Name, value1.ToString(), row.FieldDifferent(i) ? "<>" : "==", value2.ToString());
- }
- else
- {
- //this is part of the unique index we are comparing on
- object value = row.Values[field1];
- Console.WriteLine("*{0}\t{1}", field.Field(false).Name, value.ToString());
- }
- i++;
- }
- }
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("ERROR\n{0}", e.Message);
- }
- }
- private static void StatusCallBack(object sender, StatusEventArgs e)
- {
- //fired by the SqlProvider to indicate events
- if (e.Message != null)
- {
- Console.WriteLine(e.Message);
- }
- if (e.Percentage != -1)
- {
- Console.WriteLine("{0}%", e.Percentage);
- }
- }
- }
- }