SQL Data Compare project
From RedGateWiki
This example shows how you can create and load SQL Data Compare project files programmatically. The projects saved by the full commercial SQL Data Compare project can be loaded by the SDK to specify the data sources as well as preset table, view, and column mappings.
C#
- using System;
- using System.Data.SqlClient;
- using RedGate.SQLCompare.Engine;
- using RedGate.SQLDataCompare.Engine;
- namespace SQLDataCompareCodeSnippets
- {
- public class ProjectExample
- {
- private const string projectName = @"testproject.sdc";
- public static void Main()
- {
- //load up the project
- Project project = Project.LoadFromDisk(projectName);
- Console.WriteLine("Project loaded");
- //get the two databases
- using (Database db1 = new Database())
- using (Database db2 = new Database())
- {
- SchemaMappings mappings = new SchemaMappings();
- //Should check if this is true
- LiveDatabaseSource liveDb1 = project.DataSource1 as LiveDatabaseSource;
- ConnectionProperties sourceConnectionProperties = liveDb1.ToConnectionProperties();
- //Should check if this is true
- LiveDatabaseSource liveDb2 = project.DataSource2 as LiveDatabaseSource;
- ConnectionProperties targetConnectionProperties = liveDb2.ToConnectionProperties();
- try
- {
- Console.WriteLine("Registering database " + sourceConnectionProperties.DatabaseName);
- db1.RegisterForDataCompare(sourceConnectionProperties, Options.Default);
- }
- catch (SqlException e)
- {
- Console.WriteLine(e.Message);
- Console.WriteLine(@"</li>
- Cannot connect to database '{0}' on server '{1}'. The most common causes of this error are:</li>
- o The sample databases are not installed</li>
- o ServerName not set to the location of the target database</li>
- o For sql server authentication, username and password incorrect or not supplied in ConnectionProperties constructor</li>
- o Remote connections not enabled"
, sourceConnectionProperties.DatabaseName, sourceConnectionProperties.ServerName); - return;
- }
- try
- {
- Console.WriteLine("Registering database " + targetConnectionProperties.DatabaseName);
- db2.RegisterForDataCompare(targetConnectionProperties, Options.Default);
- }
- catch (SqlException e)
- {
- Console.WriteLine(e.Message);
- Console.WriteLine(@"</li>
- Cannot connect to database '{0}' on server '{1}'. The most common causes of this error are:</li>
- o The sample databases are not installed</li>
- o ServerName not set to the location of the target database</li>
- o For sql server authentication, username and password incorrect or not supplied in ConnectionProperties constructor</li>
- o Remote connections not enabled"
, targetConnectionProperties.DatabaseName, targetConnectionProperties.ServerName); - return;
- }
- mappings.Options = project.DCOptions;
- mappings.CreateMappings(db1, db2); // Create a set of base mappings
- //Modify the default mappings using what has been saved in the project
- RedGate.SQLDataCompare.Engine.DataCompareUserActions.ReplayUserActions(
- project.SelectTableActions,
- project.DataSource1,
- project.DataSource2,
- ref mappings);
- using (ComparisonSession session = new ComparisonSession())
- {
- session.Options = project.DCOptions;
- session.CompareDatabases(db1, db2, mappings);
- SqlProvider sp = new SqlProvider();
- Console.WriteLine(sp.GetMigrationSQL(session, true));
- Console.WriteLine("Comparison run");
- Console.ReadLine();
- }
- }
- }
- }
- }
VB
- Imports System
- Imports System.Data.SqlClient
- Imports RedGate.SQLCompare.Engine
- Imports RedGate.SQLDataCompare.Engine
- Namespace SQLDataCompareCodeSnippets
- Public Class ProjectExample
- Private Const projectName As String = "testproject.sdc"
- Public Shared Sub Main()
- 'load up the project
- Dim project__1 As Project = Project.LoadFromDisk(projectName)
- Console.WriteLine("Project loaded")
- 'get the two databases
- Using db1 As New Database()
- Using db2 As New Database()
- Dim mappings As New SchemaMappings()
- 'Should check if this is true
- Dim liveDb1 As LiveDatabaseSource = TryCast(project__1.DataSource1, LiveDatabaseSource)
- Dim sourceConnectionProperties As ConnectionProperties = liveDb1.ToConnectionProperties()
- 'Should check if this is true
- Dim liveDb2 As LiveDatabaseSource = TryCast(project__1.DataSource2, LiveDatabaseSource)
- Dim targetConnectionProperties As ConnectionProperties = liveDb2.ToConnectionProperties()
- Try
- Console.WriteLine("Registering database " & sourceConnectionProperties.DatabaseName)
- db1.RegisterForDataCompare(sourceConnectionProperties, Options.[Default])
- Catch e As SqlException
- Console.WriteLine(e.Message)
- Console.WriteLine(vbCr & vbLf & "Cannot connect to database '{0}' on server '{1}'. The most common causes of this error are:" & vbCr & vbLf & " o The sample databases are not installed" & vbCr & vbLf & " o ServerName not set to the location of the target database" & vbCr & vbLf & " o For sql server authentication, username and password incorrect or not supplied in ConnectionProperties constructor" & vbCr & vbLf & " o Remote connections not enabled", sourceConnectionProperties.DatabaseName, sourceConnectionProperties.ServerName)
- Exit Sub
- End Try
- Try
- Console.WriteLine("Registering database " & targetConnectionProperties.DatabaseName)
- db2.RegisterForDataCompare(targetConnectionProperties, Options.[Default])
- Catch e As SqlException
- Console.WriteLine(e.Message)
- Console.WriteLine(vbCr & vbLf & "Cannot connect to database '{0}' on server '{1}'. The most common causes of this error are:" & vbCr & vbLf & " o The sample databases are not installed" & vbCr & vbLf & " o ServerName not set to the location of the target database" & vbCr & vbLf & " o For sql server authentication, username and password incorrect or not supplied in ConnectionProperties constructor" & vbCr & vbLf & " o Remote connections not enabled", targetConnectionProperties.DatabaseName, targetConnectionProperties.ServerName)
- Exit Sub
- End Try
- mappings.Options = project__1.DCOptions
- mappings.CreateMappings(db1, db2)
- ' Create a set of base mappings
- 'Modify the default mappings using what has been saved in the project
- RedGate.SQLDataCompare.Engine.DataCompareUserActions.ReplayUserActions(project__1.SelectTableActions, project__1.DataSource1, project__1.DataSource2, mappings)
- Using session As New ComparisonSession()
- session.Options = project__1.DCOptions
- session.CompareDatabases(db1, db2, mappings)
- Dim sp As New SqlProvider()
- Console.WriteLine(sp.GetMigrationSQL(session, True))
- Console.WriteLine("Comparison run")
- Console.ReadLine()
- End Using
- End Using
- End Using
- End Sub
- End Class
- End Namespace