SQL Comparison SDK 10

Using SQL Data Compare mappings in projects using the API

A powerful feature of SQL Data Compare is the ability to use the object mappings set in a saved SQL Data Compare project file programmatically.

Line 65 shows the method ReplayUserActions, which allows the mappings to be accessed from the project file.

Visual Basic code

Imports RedGate.SQL.Shared
Imports RedGate.SQLCompare.Engine
Imports RedGate.SQLDataCompare.Engine
Imports RedGate.SQLDataCompare.Engine.ResultsStore


Module Test
Sub test()
Dim e As New SyncNow( "D:\TLHORT\Misc Files\SF- Sync.sdc")
e.Synchronize()
End Sub
End Module
 
Public Class SyncNow
Dim pathToProjectFile As String

Dim project As RedGate.SQLDataCompare.Engine.Project
Dim db1, db2 As Database
Dim mappings As SchemaMappings
Dim session As ComparisonSession
Dim livedb As LiveDatabaseSource

Dim provider As SqlProvider
Dim block As ExecutionBlock
 
Dim executor As BlockExecutor
Public Sub New( ByVal PhysicalPathToProjectFile As String)
Me.pathToProjectFile = PhysicalPathToProjectFile


End Sub

Public Sub Synchronize()
Try
Compare()
GetScript()
ExecuteScript()
Catch ex As Exception
Throw
Finally
session.Dispose()
block.Dispose()
End Try
End Sub

Private Sub Compare()
project = project.LoadFromDisk(pathToProjectFile)

Dim db1 As New Database
Dim db2 As New Database

livedb = DirectCast(project.DataSource1, LiveDatabaseSource)
db1.RegisterForDataCompare(livedb.ToConnectionProperties(), Options.Default)


livedb = DirectCast(project.DataSource2, LiveDatabaseSource)
db2.RegisterForDataCompare (livedb.ToConnectionProperties(), Options.Default)


mappings = New SchemaMappings
mappings.Options = project.Options

'The table mappings are NOT imported from the project file
mappings.CreateMappings(db1, db2)
project.ReplayUserActions(mappings)
session = New ComparisonSession
session.Options = project.Options
session.CompareDatabases(db1, db2, mappings)
End Sub

Private Sub GetScript()
provider = New SqlProvider
block = provider.GetMigrationSQL(session, True)
End Sub

Private Sub ExecuteScript()
executor = New BlockExecutor
executor.ExecuteBlock(block, livedb.ServerName, livedb.DatabaseName, livedb.IntegratedSecurity , livedb.UserName, livedb.Password)

End Sub

End Class

Didn't find what you were looking for?