XML and SDK

Automate and integrate using the SQL comparison API

XML and SDK

Postby markaok1 » Tue Apr 28, 2009 7:28 pm

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
markaok1
 
Posts: 11
Joined: Fri Mar 20, 2009 7:54 pm
Location: Omaha, Nebraska

Postby Brian Donahue » Mon May 04, 2009 5:46 pm

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
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

XML and SDK

Postby markaok1 » Tue May 05, 2009 12:05 am

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
markaok1
 
Posts: 11
Joined: Fri Mar 20, 2009 7:54 pm
Location: Omaha, Nebraska

Postby Brian Donahue » Tue May 05, 2009 11:04 am

Hi Mark,

When you read in a project file, you can apply your column and table selections using the ReplayUserActions method:
Code: Select all
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\");
                }
            }
      }
   }
}
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby markaok1 » Tue May 05, 2009 4:42 pm

Thanks Brian!
That worked perfectly...

MarkA
markaok1
 
Posts: 11
Joined: Fri Mar 20, 2009 7:54 pm
Location: Omaha, Nebraska

Re:

Postby jamest85 » Fri Apr 30, 2010 7:30 pm

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?

Code: Select all
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]
jamest85
 
Posts: 4
Joined: Wed Apr 21, 2010 10:30 pm

Postby Brian Donahue » Mon May 03, 2010 11:22 am

It's probably quickest to compare a script generated by SQL Data Compare with a script generated by SDK.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am


Return to SQL Comparison SDK Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests