Red Gate forums :: View topic - Progress feedback
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SQL Comparison SDK Previous Versions
SQL Comparison SDK Previous Versions forum

Progress feedback

Search in SQL Comparison SDK Previous Versions forum
Post new topic   Reply to topic
Jump to:  
Author Message
mnelson



Joined: 12 Aug 2008
Posts: 2

PostPosted: Tue Aug 12, 2008 2:07 pm    Post subject: Progress feedback Reply with quote

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.
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6577

PostPosted: Tue Aug 12, 2008 4:31 pm    Post subject: Reply with quote

Hi,

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:
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(
            MappingOptions.Default,
            ComparisonOptions.TrimTrailingSpaces | ComparisonOptions.Default,
            SqlOptions.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));
                t.Start();
                // When the thread is started, pause processing on this thread until it completes
                t.Join();

            // 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;
            try
            {
               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:");
               Console.WriteLine(block.GetString());
   
               // 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");
            }
            finally
            {
               block = provider.Block;
               if (block != null)
               {
                  block.Dispose();    // dispose of the objects to delete temporary files
               }
            }
            session.Dispose();
         db1.Dispose();
         db2.Dispose();
      }
        /// <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.WriteLine("\r"+e.Message);
            }
            else
            {
                Console.Write("    \r" + e.Percentage.ToString()+"%");
            }
        }
   }
}
Back to top
View user's profile Send private message
mnelson



Joined: 12 Aug 2008
Posts: 2

PostPosted: Thu Aug 14, 2008 8:27 pm    Post subject: Reply with quote

Thanks. That is what I was looking for.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group