SQL Comparison SDK

Latest version: 10.0

SQL Comparison SDK

Knowledge Base

Using SQL Data Compare mappings in projects using the API

Category: SDK, API & Command line
Date: 03 Feb 2011
Product: SQL Comparison SDK
Versions: 6,7

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.

  1. [VB]
  2.       1 Imports RedGate.SQL.Shared
  3.       2 Imports RedGate.SQLCompare.Engine
  4.       3 Imports RedGate.SQLDataCompare.Engine
  5.       4 Imports RedGate.SQLDataCompare.Engine.ResultsStore
  6.       5
  7.       6
  8.       7 Module Test
  9.       8 Sub test()
  10.       9 Dim e As New SyncNow( "D:\TLHORT\Misc Files\SF- Sync.sdc")
  11.     10 e.Synchronize()
  12.     11 End Sub
  13.     12 End Module
  14.     13
  15.     14 Public Class SyncNow
  16.     15 Dim pathToProjectFile As String
  17.     16
  18.     17 Dim project As RedGate.SQLDataCompare.Engine.Project
  19.     18 Dim db1, db2 As Database
  20.     19 Dim mappings As SchemaMappings
  21.     20 Dim session As ComparisonSession
  22.     21 Dim livedb As LiveDatabaseSource
  23.     22
  24.     23 Dim provider As SqlProvider
  25.     24 Dim block As ExecutionBlock
  26.     25
  27.     26 Dim executor As BlockExecutor
  28.     27 Public Sub New( ByVal PhysicalPathToProjectFile As String)
  29.     28 Me.pathToProjectFile = PhysicalPathToProjectFile
  30.     29
  31.     30
  32.     31 End Sub
  33.     32
  34.     33 Public Sub Synchronize()
  35.     34 Try
  36.     35 Compare()
  37.     36 GetScript()
  38.     37 ExecuteScript()
  39.     38 Catch ex As Exception
  40.     39 Throw
  41.     40 Finally
  42.     41 session.Dispose()
  43.     42 block.Dispose()
  44.     43 End Try
  45.     44 End Sub
  46.     45
  47.     46 Private Sub Compare()
  48.     47 project = project.LoadFromDisk(pathToProjectFile)
  49.     48
  50.     49 Dim db1 As New Database
  51.     50 Dim db2 As New Database
  52.     51
  53.     52 livedb = DirectCast(project.DataSource1, LiveDatabaseSource)
  54.     53 db1.RegisterForDataCompare(livedb.ToConnectionProperties(), Options.Default)
  55.     54
  56.     55
  57.     56 livedb = DirectCast(project.DataSource2, LiveDatabaseSource)
  58.     57 db2.RegisterForDataCompare (livedb.ToConnectionProperties(), Options.Default)
  59.     58
  60.     59
  61.     60 mappings = New SchemaMappings
  62.     61 mappings.Options = project.Options
  63.     62
  64.     63 'The table mappings are NOT imported from the project file :(
  65.     64 mappings.CreateMappings(db1, db2)
  66.  
  67.     65 project.ReplayUserActions(mappings)
  68.  
  69.     66 session = New ComparisonSession
  70.     67 session.Options = project.Options
  71.     68 session.CompareDatabases(db1, db2, mappings)
  72.     69 End Sub
  73.     70
  74.     71 Private Sub GetScript()
  75.     72 provider = New SqlProvider
  76.     73 block = provider.GetMigrationSQL(session, True)
  77.     74 End Sub
  78.     75
  79.     76 Private Sub ExecuteScript()
  80.     77 executor = New BlockExecutor
  81.     78 executor.ExecuteBlock(block, livedb.ServerName, livedb.DatabaseName, livedb.IntegratedSecurity , livedb.UserName, livedb.Password)
  82.     79
  83.     80 End Sub
  84.     81
  85.     82 End Class
  86. [/VB]

Document ID: KB200712000211 Keywords: SQL,Data,Compare,API,mapping,replayuseractions

Was this article helpful?

Search support
Forums

SQL Comparison SDK

all SQL products

all products