Question

i have been using SYNC Framework to to make merging 2 different database. if my data bases are the same columns it is simple.i can easly match and sync eachother. But if i add and remove some columns inside of the tables which has got some uncommon columns. Sync can not catch also columns matching is not worked. Please dont give me article links. i have been reading all of them.

My desire below :

http://jtabadero.wordpress.com/2011/08/19/part-4-synchronizing-tables-with-different-table-names-and-column-names/

enter image description here My SQL Script:


GO

CREATE TABLE [dbo].[Customer](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
    [SurName] [nvarchar](50) NULL,
    [Age] [int] NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

USE [clientDb3]
GO

/****** Object:  Table [dbo].[Customer]    Script Date: 4/17/2014 10:15:45 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Customer](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
    [SurName] [nvarchar](50) NULL,
    [Description] [nvarchar](50) NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


C#:


namespace syncSample1
{
    class Program
    {
        static void Main(string[] args)
        {
            string serverConn = @"data source=H2014-008\SQLEXPRESS;initial catalog=serverDb3;integrated security=True;MultipleActiveResultSets=True;";
            string clientConn = @"data source=H2014-008\SQLEXPRESS;initial catalog=clientDb3;integrated security=True;MultipleActiveResultSets=True;";

            using (var serverConnection = new SqlConnection(serverConn))
            using(var clientConnection = new SqlConnection(clientConn))
            {

                var provider1 = new SqlSyncProvider("scopeTest",serverConnection);
                var provider2 = new SqlSyncProvider("scopeTest",clientConnection);


                //config

                PrepareServerForProvisioning(provider1);
                PrepareClientForProvisioning(provider2, serverConnection);

               //sync
                SyncOrchestrator sync = new SyncOrchestrator();
                sync.LocalProvider= provider1;
                sync.RemoteProvider=provider2;
                sync.Direction = SyncDirectionOrder.UploadAndDownload;
                sync.Synchronize();

                Console.Read();

            }


        }

        private static void PrepareServerForProvisioning(SqlSyncProvider provider)
        {
            SqlConnection connection = (SqlConnection)provider.Connection;
            SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning(connection);
            if (!config.ScopeExists(provider.ScopeName))
            {
                DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(provider.ScopeName);
                scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", connection));
                DbSyncTableDescription serverTableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", connection);
                serverTableDesc.GlobalName = "CustomerTable";
                config.PopulateFromScopeDescription(scopeDesc);
                config.SetCreateTableDefault(DbSyncCreationOption.CreateOrUseExisting);
                config.Apply();

            }


        }

        private static void PrepareClientForProvisioning(SqlSyncProvider provider, SqlConnection sourceConnection)
        {
            SqlSyncScopeProvisioning config = new SqlSyncScopeProvisioning((SqlConnection)provider.Connection);
            if (!config.ScopeExists(provider.ScopeName))
            {
                DbSyncScopeDescription scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(provider.ScopeName, sourceConnection);
                scopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable("Customer", sourceConnection));
                config.PopulateFromScopeDescription(scopeDesc);
                config.Apply();

            }

        }


    }
}

Was it helpful?

Solution

there's nothing special about what you want to achieve. that's very much supported out of the box. when you provision a table you can specify which columns to include. You can specify in code which specific columns should be added for every table. in your code above, you use the GetDescriptionForTable which grabs all the columns. you can retain the same code and simply remove the columns you want excluded. That approach is in the same blog post you quoted above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top