SQLProviderExample
From RedGateWiki
This example demonstrates how the SqlProvider object can be used to create SQL migration scripts.
C#
- using System;
- using RedGate.Shared.SQL;
- using RedGate.SQLCompare.Engine;
- using RedGate.SQLDataCompare.Engine;
- using RedGate.Shared.SQL.ExecutionBlock;
- namespace SQLDataCompareCodeSnippets
- {
- public class SqlProviderExample
- {
- public static void Main()
- {
- 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);
- //
- //Additionally set up trim trailing spaces...
- //
- mappings.Options = new EngineDataCompareOptions(
- MappingOptions.Default,
- ComparisonOptions.TrimTrailingSpaces | ComparisonOptions.Default,
- SqlOptions.Default);
- using (ComparisonSession session = new ComparisonSession())
- {
- //
- // Remember to set up the session options
- //
- 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();
- //
- // Also remember to set up the provider options
- //
- 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.SQLCompare.Engine
- Imports RedGate.SQLDataCompare.Engine
- Imports RedGate.Shared.SQL.ExecutionBlock
- Module Module1
- 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)
- '
- 'Additionally set up trim trailing spaces...
- '
- mappings.Options = New EngineDataCompareOptions(MappingOptions.[Default], ComparisonOptions.TrimTrailingSpaces Or ComparisonOptions.[Default], SqlOptions.[Default])
- Using session As New ComparisonSession()
- '
- ' Remember to set up the session options
- '
- 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()
- '
- ' Also remember to set up the provider options
- '
- 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