Loading Projects, Script Folders and Snapshots

From RedGateWiki
Jump to: navigation, search

Back to SQL Comparison SDK

These examples show how to load a SQL Compare project file from disk, allowing you to set up a project in the SQL Compare program and re-use the settings from the API, how to synchronize to a script folder, and how to load an existing schema file (snapshot) saved to disk using SQL Compare.


Assembly references:

  • RedGate.SQLCompare.Engine.dll
  • RedGate.Shared.SQL.dll
  • RedGate.Shared.Utils.dll

Loading a Project

  1. using System;
  2. using RedGate.SQLCompare.Engine;
  3.  
  4. namespace LoadAndSaveAProject
  5. {
  6.         /// <summary>
  7.         /// Demonstrates how to create a SQL Compare project file comparing WidgetProduction to
  8.         /// WidgetStaging.
  9.         /// </summary>
  10.         class Program
  11.         {
  12.                 static void Main(string[] args)
  13.                 {
  14.                         // Construct a project comparing WidgetProduction to WidgetStaging
  15.                         Project productionVsStaging = new Project();
  16.  
  17.                         // By default the two data sources are created as LiveDatabaseSources with
  18.                         // the server name set to "(local)". You could also assign SnapshotSource
  19.                         // or FolderDataSource objects here.
  20.                         productionVsStaging.DataSource1.DatabaseName = "WidgetProduction";
  21.                         productionVsStaging.DataSource2.DatabaseName = "WidgetStaging";
  22.  
  23.                         // Save a project file that can be opened by the SQL Compare UI
  24.                         productionVsStaging.SaveToDisk(@"c:\A sample project.scp");
  25.  
  26.                         // Load the project we just saved back from disk
  27.                         Project productionVsStagingLoaded = Project.LoadFromDisk(@"c:\A sample project.scp");
  28.  
  29.                         Console.WriteLine(productionVsStagingLoaded.FileName);
  30.  
  31.                         Console.WriteLine("Press [Enter]");
  32.                         Console.ReadLine();
  33.                 }
  34.         }
  35. }

Loading a Snapshot

  1. using System;
  2. using RedGate.SQLCompare.Engine;
  3.  
  4. namespace LoadAndSaveASnapshot
  5. {
  6.         /// <summary>
  7.         /// Demonstration that saves a snapshot of the WidgetStaging database to disk and
  8.         /// loads it back in again.
  9.         /// </summary>
  10.         class Program
  11.         {
  12.                 static void Main(string[] args)
  13.                 {
  14.                         using (Database widgetStaging = new Database())
  15.                         {
  16.                                 // Connect to the WidgetStaging database and read the schema
  17.                                 widgetStaging.Register(new ConnectionProperties(".", "WidgetLive"), Options.Default);
  18.  
  19.                                 // Save a snapshot of the database to c:\WidgetStaging.snp
  20.                                 widgetStaging.SaveToDisk(@"c:\WidgetStaging.snp");
  21.                         }
  22.  
  23.                         using (Database widgetStagingSnapshot = new Database())
  24.                         {
  25.                                 // Load the database schema previously stored in the snapshot of WidgetStaging
  26.                                 widgetStagingSnapshot.LoadFromDisk(@"c:\WidgetStaging.snp");
  27.  
  28.                                 // Display the script for an object from the snapshot
  29.                                 Work work = new Work();
  30.                                 Regions regions = work.ScriptObject(widgetStagingSnapshot.Tables[0], Options.Default);
  31.                                 Console.WriteLine("SQL script from snapshot:\n{0}", regions);
  32.                         }
  33.  
  34.                         Console.WriteLine("Press [Enter]");
  35.                         Console.ReadLine();
  36.                 }
  37.         }
  38. }




Saving a Scripts folder

  1. using System;
  2. using System.IO;
  3. using RedGate.SQLCompare.Engine;
  4. using RedGate.SQLCompare.Engine.ReadFromFolder;
  5.  
  6. namespace SaveScriptsFolder
  7. {
  8.         /// <summary>
  9.         /// Demonstration that saves WidgetProduction to a folder of scripts at
  10.         /// C:\WidgetProductionScripts.
  11.         /// </summary>
  12.         /// <remarks>
  13.         /// The WidgetProductionScripts folder must not exist, and you must have
  14.         /// permission to create a new folder on the C dri
  15.         /// ve before running this
  16.         /// demonstration.
  17.         /// </remarks>
  18.         class Program
  19.         {
  20.                 /// <summary>
  21.                 /// The folder that this demonstration will create in order to save the scripts from WidgetProduction.
  22.                 /// </summary>
  23.                 private const string c_WidgetProductionFolder = @"C:\WidgetProductionScripts";
  24.  
  25.                 static void Main(string[] args)
  26.                 {
  27.                         // SQL Compare can be instructed to save scripts to an existing folder, but this
  28.                         // will not take account of any existing files, so it is safer to create a new
  29.                         // folder before proceeding.
  30.                         if (Directory.Exists(c_WidgetProductionFolder))
  31.                         {
  32.                                 Console.WriteLine("The folder {0} already exists", c_WidgetProductionFolder);
  33.                                 return;
  34.                         }
  35.  
  36.                         // Create a new folder for the scripts
  37.                         Directory.CreateDirectory(c_WidgetProductionFolder);
  38.  
  39.                         // Connect to the live WidgetProduction database
  40.                         using (Database widgetProduction = new Database())
  41.                         {
  42.                                 // Read the schema for WidgetProduction
  43.                                 widgetProduction.Register(new ConnectionProperties(".", "WidgetProduction"), Options.Default);
  44.  
  45.                                 // Use the default folder structure for the files that will be created
  46.                                 WriteToFileOptions folderOptions = new WriteToFileOptions();
  47.  
  48.                                 // Write the scripts for the WidgetProduction database to the folder we created earlier
  49.                                 widgetProduction.SaveToFolder(c_WidgetProductionFolder, folderOptions);
  50.  
  51.                                 Console.WriteLine("Scripts saved to {0}", c_WidgetProductionFolder);
  52.                         }
  53.  
  54.                         Console.WriteLine("Press [Enter]");
  55.                         Console.ReadLine();
  56.                 }
  57.         }
  58. }
Personal tools