Options

Excluding Columns to Compare in SqlDataCompare (using API)

srikmrsrikmr Posts: 16
edited April 25, 2007 5:31AM in SQL Toolkit Previous Versions
Hi,
I'm trying to evalute SqlDataCompare API for our project.
My requirement is like this:
I am writing some test cases to compare data.
I have 2 Databases viz TestDB and ProdDB which have the same structures. I need to compare the Data in Table1 of TestDB with Data in Table1 of ProdDB. In doing so, i need to exclude few columns for comparision. I'm unable to achieve this.
I am able to see this hapening in the Tool, (with the selections), but i need to write code using your API.

Using your API help, i have developed the below code. I'm stuck at the point of comparing two tables from two databases where i can specify the columns to be compared. Could you please give your valueble inputs on this.
For your reference, below is my current code:
Database db1 = new Database();
 Database db2 = new Database();
 ComparisonSession session = new ComparisonSession();
 TableMappings mappings = new TableMappings();
  try
      {
          db1.RegisterForDataCompare(new ConnectionProperties(".", "TestDB"));
         db2.RegisterForDataCompare(new ConnectionProperties(".", "ProdDB"));
           // Create the mappings between a certain table
       TableMapping tableMapping = (TableMapping)mappings.Join(db1.Tables["[dbo].[Table1]"], db2.Tables["[dbo].[Table1]"]);
           // Set the custom comparison key for the table
       tableMapping.MatchingMappings.Clear();
       tableMapping.MatchingMappings.Add(tableMapping.FieldMappings["DeviceID"]);
       tableMapping.RefreshMappingStatus();
       mappings.Add(tableMapping);
       // Set the where clause for the comparison
       tableMapping.Where = new WhereClause("DeviceType = 'A'");
       
      // TODO: code to set the columns to be excluded
       
        // Peform the comparison
         session.CompareDatabases(db1, db2, mappings);

         TableDifference difference = session.TableDifferences["[dbo].[Table1]"];

       if (difference.DifferencesSummary.DifferenceCount() == 0)
           Console.WriteLine("Both tables are Same");
      else
           Console.WriteLine("Difference of Tables exist.");
   }
    finally
            {
                session.Dispose();
                db1.Dispose();
                db2.Dispose();
            }

Thanks
Srikmr

Comments

  • Options
    Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    Hello,

    You can set a mapping's .Include property to set whether or not it will be included in the data comparison:
    // Set the custom comparison key for the table
    				tableMapping.MatchingMappings.Clear();
    				tableMapping.MatchingMappings.Add(tableMapping.FieldMappings["WidgetID"]);
    				
    				foreach (FieldMapping m in tableMapping.FieldMappings) 
    				{
    					if (m!=tableMapping.FieldMappings["Active"] && m!=tableMapping.FieldMappings["Price"])
    					m.Include=false;
    				}
    							
    				tableMapping.RefreshMappingStatus();
    
Sign In or Register to comment.