How to get only differences

pr_kalepr_kale Posts: 5
edited November 9, 2005 12:55PM in SQL Toolkit Previous Versions
How can i get only differences using SQL Toolkit similar to SQLCompare report which displays differences in RED color?

I want to get only actual differences between objects scriptwise

Comments

  • Brian DonahueBrian Donahue New member Posts: 6,590 New member
    Hello,

    You can use the work object to script each database object in your databases and then use the DiffHelper class to generate a line-by-line comparison. Here is an example:
    using System;
    using RedGate.SQLCompare.Engine;
    using RedGate.SQL.Shared;
    
    namespace DiffHelperExample
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			Database db1=new Database();
    			Database db2=new Database();
    
    			db1.Register(new ConnectionProperties(".", "WidgetStaging"), Options.Default);
    			db2.Register(new ConnectionProperties(".", "WidgetProduction"), Options.Default);
    
    			Differences differences=db1.CompareWith(db2, Options.Default);
    			DiffHelper.ComparisonStrings s=null;
    			Work w = new Work();
    			foreach (Difference difference in differences)
    			{
    					s=DiffHelper.CompareStrings(w.ScriptObject(difference.ObjectIn1,Options.Default).ToString(), w.ScriptObject(difference.ObjectIn2,Options.Default).ToString(), true, false);
    								Console.WriteLine("Comparison of object {0}\r\n---------------------", difference.Name);
    				if (difference.ObjectIn1!=null && difference.ObjectIn2!=null) 
    				{
    					foreach (DiffHelper.ComparisonString str in s) 
    					{
    if (str.LeftStringPresent && str.RightStringPresent && !str.Different) Console.WriteLine(str.LeftString + "\t== " +str.RightString);			
    if (str.LeftStringPresent && str.RightStringPresent &str.Different) Console.WriteLine(str.LeftString + "\t<> " +str.RightString);
    						if (str.LeftStringPresent && !str.RightStringPresent) Console.WriteLine(str.LeftString +"\t-> ");
    						if (!str.LeftStringPresent && str.RightStringPresent) Console.WriteLine("\t<- " +str.RightString);
    					}
    				}
    					if (difference.ObjectIn1==null) Console.WriteLine("<- (This object exists only in DB2)");
    					if (difference.ObjectIn2==null) Console.WriteLine("(This object exists only in DB1) ->");
    				
    			}
    
    			Console.WriteLine("Press any key!");
    			Console.ReadLine();
    			
    			db1.Dispose();
    			db2.Dispose();
    		}
    	}
    }
    
Sign In or Register to comment.