SQL Comparison SDK 10

Help for older versions available.

These pages cover SQL Comparison SDK 10, which is not the latest version. Help for other versions is also available.

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 Toggle source code

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

Didn't find what you were looking for?