Progress feedback

Automate and integrate using the SQL comparison API

Progress feedback

Postby mnelson » Tue Aug 12, 2008 1:07 pm

I am working with sdk 7 and it works like a charm. I am building some .Net components that will snapshot various database and running them as a windows service.

I am also creating a console utility to do the same and would like to provide feedback to the console. Is there a way to call CompareDatabases async and update the console on progress, i.e., which table is being compared as in the Redgate windows apps.
Posts: 2
Joined: Tue Aug 12, 2008 12:58 pm

Postby Brian Donahue » Tue Aug 12, 2008 3:31 pm


Thanks for your post! You can update the console window by assigning a Status event to the Session object and then running the CompareDatabases method in a new thread. I hope that you can use the following sample code:
Code: Select all
using System;
using RedGate.Shared.SQL.ExecutionBlock;
using RedGate.SQLCompare.Engine;
using RedGate.SQLDataCompare.Engine;
using RedGate.Shared.Utils;
using System.Threading;

namespace SQLDataCompareCodeSnippets
   public class SqlProviderExample
        private Database db1 = null;
        private Database db2 = null;
        ComparisonSession session = null;
        TableMappings mappings = null;
      public void RunExample()
         db1=new Database();
         db2=new Database();
         db1.RegisterForDataCompare(new ConnectionProperties(\".\", \"WidgetDev\"), Options.Default);
         db2.RegisterForDataCompare(new ConnectionProperties(\".\", \"WidgetLive\"), Options.Default);
         // Create the mappings between the two databases
         mappings = new TableMappings();
         mappings.CreateMappings(db1.Tables, db2.Tables);
         //Additionally set up trim trailing spaces...
         mappings.Options = new EngineDataCompareOptions(
            ComparisonOptions.TrimTrailingSpaces | ComparisonOptions.Default,

         session=new ComparisonSession();
            // Remember to set up the session options
            session.Options = mappings.Options;
                // When the status changes, run StatusUpdate
                session.Status += new StatusEventHandler(StatusUpdate);
                // Compare the databases in a second thread
                Thread t = new Thread(new ThreadStart(CompareDatabases));
                // When the thread is started, pause processing on this thread until it completes

            // now get the ExecutionBlock containing the SQL
            // we want to run this on WidgetLive so we pass on true as the second parameter
            SqlProvider provider=new SqlProvider();           
            // Also rememeber to set up the provider options
            provider.Options = session.Options;
            ExecutionBlock block;
               block = provider.GetMigrationSQL(session, true);
               Console.WriteLine(\"The synchronization SQL contains {0} lines in {1} batches\", block.LineCount, block.BatchCount);
               // if the ExecutionBlock was very large this could cause memory problems
               Console.WriteLine(\"The SQL to be run is:\");
               // we can access the SQL in a memory efficient manner by accessing the underlying stream
               // FileStream stream=block.GetFileStream();
               // run the SQL ( commented out by default )
               // BlockExecutor executor = new BlockExecutor();
               // executor.ExecuteBlock(block, \".\", \"WidgetLive\");
               block = provider.Block;
               if (block != null)
                  block.Dispose();    // dispose of the objects to delete temporary files
        /// <summary>
        /// Dummy method because threadstart methods don't accept arguments
        /// </summary>
        private void CompareDatabases()
            session.CompareDatabases(db1, db2, mappings);
        private void StatusUpdate(object o, StatusEventArgs e)
            if (e.Percentage == -1)
                Console.Write(\"    \\r\" + e.Percentage.ToString()+\"%\");
Brian Donahue
Posts: 6590
Joined: Mon Aug 23, 2004 9:48 am

Postby mnelson » Thu Aug 14, 2008 7:27 pm

Thanks. That is what I was looking for.
Posts: 2
Joined: Tue Aug 12, 2008 12:58 pm

Return to SQL Comparison SDK Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests