How To Get The Databases To Synchronize.

lc6529lc6529 Posts: 30
Following the example in your tool kit, I believe I have figured out how to load a project within VB.NET and COMPARE the databases...now how to I get the databases to synchronize?
 '
        ' Copied From Synchronizing,Data Example In Help Fule
        '
        Dim objProject As Project = Project.LoadFromDisk("c:\FooBar.sdc")

        'get the two databases
        Dim db1 As New Database
        Dim db2 As New Database
        Dim mappings As New SchemaMappings

        'Should check if this is true
        Dim livedb As LiveDatabaseSource = DirectCast(objProject.DataSource1, LiveDatabaseSource)
        db1.RegisterForDataCompare(livedb.ToConnectionProperties())

        'Should check if this is true
        livedb = DirectCast(objProject.DataSource2, LiveDatabaseSource)
        db2.RegisterForDataCompare(livedb.ToConnectionProperties())

        mappings.Options = objProject.Options
        mappings.CreateMappings(db1, db2)

        'Disable any mappings here that you may want....
        Dim session As New ComparisonSession
        session.Options = objProject.Options
        '
        ' The Databases Are Now Compared
        '
        session.CompareDatabases(db1, db2, mappings)
        '
        ' How Do I get them to be synced?
        '

Comments

  • Eddie DEddie D Posts: 1,733 Rose Gold 5
    Hi

    Please take a look at the sample code below which is from the Toolkit sample files located in the following folder:

    Program Files\Red Gate SQL Bundle 5\Toolkit Sample Files.

    To run the synchronization script, create a BlockExecutor object and call ExecuteBlock, passing in the ExecutionBlock object.
    You can receive feedback events about the progress of the comparison and synchronization by using the ICancellable interface.

    Dispose of the objects when you have finished using them.


    Option Explicit On

    'These code snippets rely on the WidgetProduction and WidgetStaging databases which can be created
    'from the toolkit.chm help file.
    '
    'These snippets are provided "AS IS".

    Imports RedGate.SQL.Shared
    Imports RedGate.SQLCompare.Engine

    Module Module1

    Sub Main()

    'uncomment one of the lines below to run the sample

    'SynchronizeDatabases()
    'ScriptObject()
    'RegionsDemonstration()
    'LoadAndSaveAProject()
    'CompareTwoDatabases()
    'LoadAndSaveASnapshot()
    'GetFeedback()

    Console.WriteLine("Press [Enter]")
    Console.ReadLine()

    End Sub

    Sub SynchronizeDatabases()

    Dim db1 As Database = New Database()
    Dim db2 As Database = New Database()


    db1.Register(New ConnectionProperties(".", "WidgetStaging"), Options.Default)
    db2.Register(New ConnectionProperties(".", "WidgetProduction"), Options.Default)

    Dim differences As Differences = db1.CompareWith(db2, Options.Default)

    Dim difference As Difference

    For Each difference In differences
    'make sure the difference is selected so it is included in the synchronization
    difference.Selected = True
    Next

    Dim work As Work = New Work()

    'calculate the work to do using sensible default options
    'the script is to be run on WidgetProduction so the runOnTwo parameter is true

    work.BuildFromDifferences(differences, Options.Default, True)

    'we can now access the messages and warnings

    Console.WriteLine("Messages:")

    Dim message As Message

    For Each message In work.Messages
    Console.WriteLine(message.Text)
    Next

    Console.WriteLine("Warnings:")

    For Each message In work.Warnings
    Console.WriteLine(message.Text)
    Next


    'print out the SQL used to synchronize

    Dim block As ExecutionBlock = work.ExecutionBlock

    Console.WriteLine("SQL to synchronize:")

    Console.WriteLine(block.GetString())

    'and run the SQL

    Dim executor As BlockExecutor = New BlockExecutor

    executor.ExecuteBlock(block, ".", "WidgetProduction")

    'dispose of the objects
    block.Dispose()
    db1.Dispose()
    db2.Dispose()

    End Sub

    Eddie Davis
    Red Gate Software Ltd
    e-mail: [email protected]
    Eddie Davis
    Senior Product Support Engineer
    Redgate Software Ltd
    Email: [email protected]
Sign In or Register to comment.