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


Provides access to the schema synchronization script.
Object Model
ScriptManager Class
Syntax
'Declaration
 
Public NotInheritable Class ScriptManager 
public sealed class ScriptManager 
public __gc __sealed class ScriptManager 
Remarks
The ScriptManager class provides access to the synchronization script and methods for executing it. The class cannot be instantiated directly, you should call instead the SqlSchemaCompare.GetLeftDatabaseScript or SqlSchemaCompare.GetRightDatabaseScript method to get the synchronization script intended for the left or right database.

Once the script has been generated, you can execute it by calling one of the overloaded Execute methods. The script can be executed against the original database for which it has been created, or against any other database by specifying a new connection string.

The script uses by default the options selected during the comparison, but you can specify new ones. The comparison options that effect the script execution are the followings:

You can get progress information during the script execution by subscribing to the following events:

To get the status of the script execution, whether it succeeded, failed or canceled, use the ScriptExecutionStatusEnum status returned by the Execute method. When it comes to determine the status of the execution, consider the followings:

Example
The following example demonstrates how to compare two databases, generate a synchronization script and execute it in the Target Database.
using xSQL.Schema.Core;
using xSQL.Schema.SqlServer;
using xSQL.SchemaCompare.SqlServer;
            
namespace xSQL.Sdk.SchemaCompare.Samples
{
    class Test
    {
        public static void SyncDatabases()    
        {
            SqlServer server;
            SqlDatabase xDatabase, yDatabase;
            SqlSchemaCompare comparer;
            ScriptManager sqlScript;
            ScriptExecutionStatusEnum status;
            
            try
            {
                // create the SQL Server object
                server = new SqlServer(@"(local)");
                
                // create the left database
                xDatabase = server.GetDatabase("Source");
                
                // create the right database
                yDatabase = server.GetDatabase("Target");
                
                // create the comparer
                comparer = new SqlSchemaCompare(xDatabase, yDatabase);
                
                // step 1: read the schema 
                comparer.ReadSchema();
                
                // step 2: pair the database objects
                comparer.PairObjects();
                
                // step 3: compare the schema
                comparer.Compare();
                
                // 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 Target database in order to make it the same as the Source database
                sqlScript = comparer.GetRightDatabaseScript();
                
                if (!sqlScript.IsEmpty())
                {
                    //--use the event to get some progress during the script execution
                    sqlScript.SchemaScriptExecuting += new EventHandler<SchemaScriptEventArgs>(sqlScript_SchemaScriptExecuting);
                    
                    // execute the sync script
                    status = sqlScript.Execute();
                    
                    // check the execution and print any errors
                    if (status == ScriptExecutionStatusEnum.Succeeded)
                    {
                        Console.WriteLine("Database synchronization finished successfully");
                    }
                    else if (status == ScriptExecutionStatusEnum.Canceled)
                    {
                        Console.WriteLine("Database synchronization was canceled");
                    }
                    else
                    {
                        //--check for silent errors
                        if (ErrorRepository.Instance.HasErrors())
                        {
                            Console.WriteLine("Some errors occurred during the script execution.");
                            Console.Write(ErrorRepository.Instance.GetErrors());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Database synchronization failed.");
                Console.Write(ex.ToString());
            }
        }
        
        private static void sqlScript_SchemaScriptExecuting(object sender, SchemaScriptEventArgs e)
        {
            Console.WriteLine("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), e.Script);
        }
    }
}
Inheritance Hierarchy

System.Object
   xSQL.SchemaCompare.SqlServer.ScriptManager

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

ScriptManager Members
xSQL.SchemaCompare.SqlServer Namespace

 

 


©Copyright 2022 xSQL Software. All Rights Reserved.

Send Feedback