FilterSQLExample
From RedGateWiki
This example displays the selection process for extracting certain records from a database table – it operates by using SelectionDelegate
Alternatively selection can achieved by using the Where clause listed in the WhereExample.cs
C#
- using System;
- using RedGate.Shared.SQL;
- using RedGate.SQLCompare.Engine;
- using RedGate.SQLDataCompare.Engine;
- using RedGate.SQLDataCompare.Engine.ResultsStore;
- using RedGate.Shared.SQL.ExecutionBlock;
- using System.Data.SqlClient;
- namespace SQLDataCompareCodeSnippets
- {
- public class FilterSQLExample
- {
- static TableDifferences m_TableDifferences;
- public static void Main(string[] args)
- {
- Database db1 = new Database();
- Database db2 = new Database();
- db1.RegisterForDataCompare(new ConnectionProperties(".", "WidgetDev"), Options.Default);
- db2.RegisterForDataCompare(new ConnectionProperties(".", "WidgetLive"), Options.Default);
- // Create the mappings between the two databases
- TableMappings mappings = new TableMappings();
- mappings.CreateMappings(db1.Tables, db2.Tables);
- 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);
- m_TableDifferences = session.TableDifferences;
- // 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, new SelectionDelegate(SyncRecord), 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:");
- string sqlstuff = block.GetString();
- Console.WriteLine(sqlstuff);
- }
- finally
- {
- block = provider.Block;
- if (block != null)
- {
- block.Dispose(); // dispose of the objects to delete temporary files
- }
- }
- }
- db1.Dispose();
- db2.Dispose();
- }
- static bool SyncRecord(SynchronizationRecord syncRecordObject)
- {
- // Check only the Widgets table
- if (syncRecordObject.TableName == "[dbo].[Widgets]")
- {
- // Create a results reader for this table
- Reader resultsReader = m_TableDifferences[syncRecordObject.TableName].ResultsStore.GetReader();
- // Get a reference for the row to check. Bookmark is the row number submitted for this table
- Row myRow = resultsReader.GetRow(syncRecordObject.Bookmark);
- // Work out which column of the ResultsReader corresponds to the "RecordID" column in database1
- int idOrdinal=m_TableDifferences[syncRecordObject.TableName].ResultsStore.Fields["RecordID"].OrdinalInResults1;
- if ((Int64)myRow.Values[idOrdinal] > 3)
- {
- // Select record for synchronization
- return true;
- }
- }
- // If table != Widgets or Widgets.RecordID < 4, do not select record for synchronization
- return false;
- }
- }
- }
VB
- Imports System
- Imports RedGate.Shared.SQL
- Imports RedGate.SQLCompare.Engine
- Imports RedGate.SQLDataCompare.Engine
- Imports RedGate.SQLDataCompare.Engine.ResultsStore
- Imports RedGate.Shared.SQL.ExecutionBlock
- Imports System.Data.SqlClient
- Module Module1
- Dim m_TableDifferences As TableDifferences
- Sub Main(ByVal args As String())
- 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)
- 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)
- m_TableDifferences = session.TableDifferences
- ' 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, New SelectionDelegate(AddressOf SyncRecord), 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:")
- Dim sqlstuff As String = block.GetString()
- Console.WriteLine(sqlstuff)
- 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
- Private Function SyncRecord(ByVal syncRecordObject As SynchronizationRecord) As Boolean
- ' Check only the Widgets table
- If syncRecordObject.TableName = "[dbo].[Widgets]" Then
- ' Create a results reader for this table
- Dim resultsReader As Reader = m_TableDifferences(syncRecordObject.TableName).ResultsStore.GetReader()
- ' Get a reference for the row to check. Bookmark is the row number submitted for this table
- Dim myRow As Row = resultsReader.GetRow(syncRecordObject.Bookmark)
- ' Work out which column of the ResultsReader corresponds to the "RecordID" column in database1
- Dim idOrdinal As Integer = m_TableDifferences(syncRecordObject.TableName).ResultsStore.Fields("RecordID").OrdinalInResults1
- If DirectCast(myRow.Values(idOrdinal), Int64) > 3 Then
- ' Select record for synchronization
- Return True
- End If
- End If
- ' If table != Widgets or Widgets.RecordID < 4, do not select record for synchronization
- Return False
- End Function
- End Module