What are the challenges you face when working across database platforms? Take the survey
Options

Progress feedback

mnelsonmnelson Posts: 3
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.

Comments

  • Options
    Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    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:
    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()+"%");
                }
            }
    	}
    }
    
  • Options
    Thanks. That is what I was looking for.
Sign In or Register to comment.