XML and SDK

When I was evaluating the Command line tool, I could use an XML file to set my options, servers, databases, etc. Is there a way to use the same XML file to pass in this information to the SDK?

Thanks in advance...
MarkA

Comments

  • Brian DonahueBrian Donahue Posts: 6,590 New member
    Hi Mark,

    I'm sure that you can write some code that will translate an XML command file into mappings, options, ConnectionProperties, and the like, but you would have to write it, as the bit of code that does this in the command-line application is not part of the SDK.

    You could more easily create and save project files to make re-usable comparisons. It's easy to load a project from disk and then use the settings to compare your schema or data. Here is some code that you can get started with: http://labs.red-gate.com/index.php/Load ... _Snapshots
  • Thanks Brian,
    I've been struggling for a couple of days trying to use the project file. Is there a way to choose which tables get synced in the data compare? I see in the project file where there's a node called 'TableActions' that contains the tables I've selected via the GUI. The trouble is I end up syncing every table in the database, not just the one's I've selected.

    Any help would be appreciated.
    Thanks again for your help!
    MarkA
  • Brian DonahueBrian Donahue Posts: 6,590 New member
    Hi Mark,

    When you read in a project file, you can apply your column and table selections using the ReplayUserActions method:
    using System;
    using System.Data.SqlClient;
    using RedGate.Shared.SQL;
    using RedGate.SQLCompare.Engine;
    using RedGate.SQLDataCompare.Engine;
    using Project = RedGate.SQLDataCompare.Engine.Project;
    
    namespace SQLDataCompareCodeSnippets
    {
    	public class ProjectExample
    	{
    	    private const string projectName = @"testproject.sdc";
    		
            public void RunExample()
    		{
    			Project project=new Project();
    
                project.DataSource1.ServerName = Program.DevServerName;
                project.DataSource2.ServerName = Program.LiveServerName;
    			project.DataSource1.DatabaseName = Program.DevDatabaseName;            
    			project.DataSource2.DatabaseName = Program.LiveDatabaseName;            
    			project.SessionSettings = SessionSettings.Default;
    			project.Options = new EngineDataCompareOptions(MappingOptions.Default,ComparisonOptions.Default, SqlOptions.Default);
    			Console.WriteLine("Saving project");
    		    project.SaveToDisk(projectName);
        
    			//load up the project
    			Project project2=Project.LoadFromDisk(projectName);
    			Console.WriteLine("Project loaded");
        
    			//get the two databases
    			using (Database db1= new Database())
                using (Database db2 = new Database())
                {
                    SchemaMappings mappings = new SchemaMappings();
    
                    //Should check if this is true
                    LiveDatabaseSource liveDb1 = project2.DataSource1 as LiveDatabaseSource;
                    ConnectionProperties sourceConnectionProperties = liveDb1.ToConnectionProperties();
                    
                    //Should check if this is true
                    LiveDatabaseSource liveDb2 = project2.DataSource2 as LiveDatabaseSource;
                    ConnectionProperties targetConnectionProperties = liveDb2.ToConnectionProperties();
                    
                    try
                    {
                        Console.WriteLine("Registering database " + sourceConnectionProperties.DatabaseName);
                        db1.RegisterForDataCompare(sourceConnectionProperties, Options.Default);
                    }
                    catch (SqlException e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine(@"
    Cannot connect to database '{0}' on server '{1}'. The most common causes of this error are:
            o The sample databases are not installed
            o ServerName not set to the location of the target database
            o For sql server authentication, username and password incorrect or not supplied in ConnectionProperties constructor
            o Remote connections not enabled", sourceConnectionProperties.DatabaseName, sourceConnectionProperties.ServerName);
                        return;
                    }
                    try
                    {
                        Console.WriteLine("Registering database " + targetConnectionProperties.DatabaseName);
                        db2.RegisterForDataCompare(targetConnectionProperties, Options.Default);
                    }
                    catch (SqlException e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine(@"
    Cannot connect to database '{0}' on server '{1}'. The most common causes of this error are:
            o The sample databases are not installed
            o ServerName not set to the location of the target database
            o For sql server authentication, username and password incorrect or not supplied in ConnectionProperties constructor
            o Remote connections not enabled", targetConnectionProperties.DatabaseName, targetConnectionProperties.ServerName);
                        return;
                    }
    
                    mappings.Options = project2.Options;
                    mappings.CreateMappings(db1, db2);
                    project.ReplayUserActions(ref mappings);               
    
                    //Disable any mappings here that you may want....
                    using (ComparisonSession session = new ComparisonSession())
                    {
                        session.Options = project2.Options;
                        session.CompareDatabases(db1, db2, mappings);
    
                        Console.WriteLine("Comparison run");
                    }
                }
    		}
    	}
    }
    
  • Thanks Brian!
    That worked perfectly...

    MarkA
  • markaok1 wrote:
    Thanks Brian!
    That worked perfectly...

    MarkA
    Hi: MarkA / Brian

    I have same issue with MarkA, however, I am not so lucky here, I also used project.ReplayUserActions(ref mappings), but seems not working, can you please let me know how to add some code to prove that is working after below lines?
    mappings.Options = project2.Options;
    mappings.CreateMappings(db1, db2);
    project.ReplayUserActions(ref mappings);//i only want to update I selected table in .sdc file
    
     //Disable any mappings here that you may want....
    using (ComparisonSession session = new ComparisonSession())
        {
            session.Options = project2.Options;
            session.CompareDatabases(db1, db2, mappings);
            //how to check if only comparing the selected table here?
            
                     
    

    Thanks a lot.

    (by the way, from this post, http://www.red-gate.com/messageboard/vi ... seractions, Chris Auckland says " the ReplayUserActions method is no longer exposed to the user", I am using RedGate.SQLDataCompare.Engine.dll 8.1.0.7).

    Jt[/code]
  • Brian DonahueBrian Donahue Posts: 6,590 New member
    It's probably quickest to compare a script generated by SQL Data Compare with a script generated by SDK.
Sign In or Register to comment.