xSQL Schema Compare SDK for SQL Server version 12
ComparisonOptions Class
Members  Example 


Provides options that determine how that database schema is compared and synchronized.
Object Model
ComparisonOptions Class
Syntax
'Declaration
 
Public NotInheritable Class ComparisonOptions 
public sealed class ComparisonOptions 
public __gc __sealed class ComparisonOptions 
Example
The following code example demonstrates how to change the options and compare two databases.
using xSQL.Schema.Core;
using xSQL.Schema.SqlServer;
using xSQL.SchemaCompare.SqlServer;
            
namespace xSQL.Sdk.SchemaCompare.Samples
{
    class Test
    {
    
        public static void CompareDatabases()
        {
            SqlServer xServer, yServer;
            SqlDatabase xDatabase, yDatabase;
            SqlSchemaCompare comparer;
            ScriptManager sqlScript;
            
            try
            {
                // create the left SQL Server object using Windows authentication
                xServer = new SqlServer(@"(local)");
                
                // create the right SQL Server using SQL Server authentication
                yServer = new SqlServer(@"(local)", "<user>", "<pwd>");
                
                // create the left database: AdventureWorks
                xDatabase = xServer.GetDatabase("AdventureWorks");
                
                // create the right database: AdventureWorks2008
                yDatabase = yServer.GetDatabase("AdventureWorks2008");
                
                // create the schema comparer
                comparer = new SqlSchemaCompare(xDatabase, yDatabase);
                
                // exclude some database objects
                comparer.Options.CompareUsers = false;
                comparer.Options.CompareSchemas = false;
                comparer.Options.CompareDatabaseRoles = false;
                comparer.Options.CompareApplicationRoles = false;
                
                // attach event handlers to these events in order to get some progress information during the schema read and compare
                comparer.LeftDatabase.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation);
                comparer.RightDatabase.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation);
                comparer.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation);
                
                // step 1: read the schema 
                comparer.ReadSchema();
                
                // step 2: pair the database objects
                comparer.PairObjects();
                
                // step 3: compare the database schema
                comparer.Compare();
                
                // check for errors that could have occurred during the schema compare; 
                // some errors are handled quietly and do not stop the process, others that are critical throw exceptions
                // quiet errors are collected and stored into the ErrorRepository object
                if (ErrorRepository.Instance.HasErrors())
                {
                    Console.WriteLine("Some errors occurred during the database compare");
                    Console.Write(ErrorRepository.Instance.GetErrors());
                }
                
                // check the database status; exit if no schema differences are found.
                if (comparer.SqlDatabasePair.ComparisonStatus == ComparisonStatusEnum.Equal)
                    return;
                    
                // step 4: get the T-SQL script intended for the right database; that is the script that should be executed 
                // on AdventureWorks2008 in order to make it the same as the AdventureWorks database
                sqlScript = comparer.GetRightDatabaseScript();
                
                // print the synchronization log
                Console.Write(sqlScript.GetLog());
                
                // print the synchronization script
                Console.Write(sqlScript.GetScript());
                
            }
            catch (ConnectionException ex)
            {
                // a connection exception
                Console.Write(ex.ToString());            
            }
            catch (SchemaException ex)
            {
                // a schema-read exception
                Console.Write(ex.ToString());
            }
            catch (SchemaCompareException ex)
            {
                // an exception that is thrown during the schema compare
                Console.Write(ex.ToString());
            }
            catch (ScriptExecutionException ex)
            {
                // an exception that is thrown during the script execution
                Console.Write(ex.ToString());
            }
            catch (Exception ex)
            {
                // the fallback exception
                Console.WriteLine("An unexpected error occurred.");
                Console.Write(ex.Message);
            }
        }
        
        private static void database_SchemaOperation(object sender, SchemaOperationEventArgs e)
        { 
            //--exclude verbose messages
            if (e.Message.MessageType != OperationMessageTypeEnum.Verbose)
                Console.WriteLine(e.Message.Text);
        }
        
        private static void database_SnapshotOperation(object sender, SnapshotOperationEventArgs e)
        {
            //--exclude verbose messages
            if (e.Message.MessageType != OperationMessageTypeEnum.Verbose)
                Console.WriteLine(e.Message.Text);
        }
    }
}
Inheritance Hierarchy

System.Object
   xSQL.SchemaCompare.SqlServer.ComparisonOptions

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

ComparisonOptions Members
xSQL.SchemaCompare.SqlServer Namespace

 

 


©Copyright 2022 xSQL Software. All Rights Reserved.

Send Feedback