Red Gate forums :: View topic - XML and SDK
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SQL Comparison SDK Previous Versions
SQL Comparison SDK Previous Versions forum

XML and SDK

Search in SQL Comparison SDK Previous Versions forum
Post new topic   Reply to topic
Jump to:  
Author Message
markaok1



Joined: 20 Mar 2009
Posts: 11
Location: Omaha, Nebraska

PostPosted: Tue Apr 28, 2009 7:28 pm    Post subject: XML and SDK Reply with quote

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
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6645

PostPosted: Mon May 04, 2009 5:46 pm    Post subject: Reply with quote

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/Loading_Projects%2C_Script_Folders_and_Snapshots
Back to top
View user's profile Send private message
markaok1



Joined: 20 Mar 2009
Posts: 11
Location: Omaha, Nebraska

PostPosted: Tue May 05, 2009 12:05 am    Post subject: XML and SDK Reply with quote

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
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6645

PostPosted: Tue May 05, 2009 11:04 am    Post subject: Reply with quote

Hi Mark,

When you read in a project file, you can apply your column and table selections using the ReplayUserActions method:
Code:
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");
                }
            }
      }
   }
}
Back to top
View user's profile Send private message
markaok1



Joined: 20 Mar 2009
Posts: 11
Location: Omaha, Nebraska

PostPosted: Tue May 05, 2009 4:42 pm    Post subject: Reply with quote

Thanks Brian!
That worked perfectly...

MarkA
Back to top
View user's profile Send private message
jamest85



Joined: 21 Apr 2010
Posts: 4

PostPosted: Fri Apr 30, 2010 7:30 pm    Post subject: Re: Reply with quote

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:

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/viewtopic.php?t=10834&highlight=project+replayuseractions, 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]
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6645

PostPosted: Mon May 03, 2010 11:22 am    Post subject: Reply with quote

It's probably quickest to compare a script generated by SQL Data Compare with a script generated by SDK.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group