WhereExample
From RedGateWiki
This example shows how a WHERE clause can be used to filter the data that will be compared. In addition to providing granularity to the data being synchronized, it can also improve performance by cutting down the amount of data retrieval necessary to do the comparison.
C#
- using System;
- using RedGate.Shared.SQL;
- using RedGate.Shared.Utils;
- using RedGate.SQLCompare.Engine;
- using RedGate.SQLDataCompare.Engine;
- using RedGate.Shared.SQL.ExecutionBlock;
- namespace SQLDataCompareCodeSnippets
- {
- /// <summary>
- /// Summary description for WhereExample.
- /// </summary>
- public class WhereExample
- {
- private static void AddWhereClause(TableMappings mappings)
- {
- foreach (TableMapping mapping in mappings)
- {
- if (mapping.Obj1.FullyQualifiedName=="[dbo].[Widgets]")
- {
- mapping.Include=true;
- mapping.Where = new WhereClause("RecordID>=2");
- }
- else
- {
- mapping.Include = false;
- }
- }
- }
- public void RunExample()
- {
- Database db1=new Database();
- Database db2=new Database();
- db1.RegisterForDataCompare(new ConnectionProperties(".", "WidgetStaging"), Options.Default);
- db2.RegisterForDataCompare(new ConnectionProperties(".", "WidgetProduction"), Options.Default);
- // Create the mappings between the two databases
- TableMappings mappings = new TableMappings();
- mappings.CreateMappings(db1.Tables, db2.Tables);
- AddWhereClause(mappings);
- mappings.Options = new EngineDataCompareOptions(
- MappingOptions.Default,
- ComparisonOptions.TrimTrailingSpaces | ComparisonOptions.Default,
- SqlOptions.Default);
- using (ComparisonSession session=new ComparisonSession())
- {
- session.Options = mappings.Options;
- session.CompareDatabases(db1, db2, mappings);
- // now get the ExecutionBlock containing the SQL
- // we want to run this on WidgetLive so we pass on true as the second parameter
- SqlProvider provider=new SqlProvider();
- provider.Options = session.Options;
- ExecutionBlock block;
- try
- {
- block = provider.GetMigrationSQL(session, true);
- Console.WriteLine("The synchronization SQL contains {0} lines in {1} batches", block.LineCount, block.BatchCount);
- // if the ExecutionBlock was very large this could cause memory problems
- Console.WriteLine("The SQL to be run is:");
- Console.WriteLine(block.GetString());
- // we can access the SQL in a memory efficient manner by accessing the underlying stream
- // FileStream stream=block.GetFileStream();
- // run the SQL ( commented out by default )
- // BlockExecutor executor = new BlockExecutor();
- // executor.ExecuteBlock(block, ".", "WidgetLive");
- }
- finally
- {
- block = provider.Block;
- if (block != null)
- {
- block.Dispose(); // dispose of the objects to delete temporary files
- }
- }
- }
- db1.Dispose();
- db2.Dispose();
- }
- }
- }
VB
- Imports System
- Imports RedGate.Shared.SQL
- Imports RedGate.Shared.Utils
- Imports RedGate.SQLCompare.Engine
- Imports RedGate.SQLDataCompare.Engine
- Imports RedGate.Shared.SQL.ExecutionBlock
- ''' <summary>
- ''' Summary description for WhereExample.
- ''' </summary>
- Module Module1
- Private Sub AddWhereClause(ByVal mappings As TableMappings)
- For Each mapping As TableMapping In mappings
- If mapping.Obj1.FullyQualifiedName = "[dbo].[Widgets]" Then
- mapping.Include = True
- mapping.Where = New WhereClause("RecordID>=2")
- Else
- mapping.Include = False
- End If
- Next
- End Sub
- Sub Main()
- Dim db1 As New Database()
- Dim db2 As New Database()
- db1.RegisterForDataCompare(New ConnectionProperties(".", "WidgetDev"), Options.[Default])
- db2.RegisterForDataCompare(New ConnectionProperties(".", "WidgetLive"), Options.[Default])
- ' Create the mappings between the two databases
- Dim mappings As New TableMappings()
- mappings.CreateMappings(db1.Tables, db2.Tables)
- AddWhereClause(mappings)
- mappings.Options = New EngineDataCompareOptions(MappingOptions.[Default], ComparisonOptions.TrimTrailingSpaces Or ComparisonOptions.[Default], SqlOptions.[Default])
- Using session As New ComparisonSession()
- session.Options = mappings.Options
- session.CompareDatabases(db1, db2, mappings)
- ' now get the ExecutionBlock containing the SQL
- ' we want to run this on WidgetLive so we pass on true as the second parameter
- Dim provider As New SqlProvider()
- provider.Options = session.Options
- Dim block As ExecutionBlock
- Try
- block = provider.GetMigrationSQL(session, True)
- Console.WriteLine("The synchronization SQL contains {0} lines in {1} batches", block.LineCount, block.BatchCount)
- ' if the ExecutionBlock was very large this could cause memory problems
- Console.WriteLine("The SQL to be run is:")
- ' we can access the SQL in a memory efficient manner by accessing the underlying stream
- ' FileStream stream=block.GetFileStream();
- ' run the SQL ( commented out by default )
- ' BlockExecutor executor = new BlockExecutor();
- ' executor.ExecuteBlock(block, ".", "WidgetLive");
- Console.WriteLine(block.GetString())
- Finally
- block = provider.Block
- If block IsNot Nothing Then
- ' dispose of the objects to delete temporary files
- block.Dispose()
- End If
- End Try
- End Using
- db1.Dispose()
- db2.Dispose()
- End Sub
- End Module