Search code examples
c#sqlsql-serversynchronizationmicrosoft-sync-framework

how to sync with filter data using Microsoft Sync Framework


I am woking on Microsoft Sync Framework with sql server 2008 and c# below is my code

public static void SetUp(string _pScopeName, DbSyncTableDescription _pDbSyncTable, SqlConnection serverConn, SqlConnection clientConn)
        {
            // Create a scope named "_ITEM" and add tables to it.
            DbSyncScopeDescription productScope = new DbSyncScopeDescription(_pScopeName);

            // Define the Products table.
            // Add the Table to the scope object.    
            productScope.Tables.Add(_pDbSyncTable);
            // Create a provisioning object for "_ITEM" and apply it to the on-premise database if one does not exist.
            SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, productScope);
            serverProvision.ObjectSchema = ".dbo";
            string _tblName = _pDbSyncTable.LocalName.Replace("[", "").Replace("]", "");
            serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
            serverProvision.Tables[_tblName].AddFilterColumn("_WORKGROUPNAME");
            serverProvision.Tables[_pDbSyncTable.LocalName].FilterClause = "[" + _tblName + "].[_WORKGROUPNAME] = " + _CCompanyVar._WORKGROUPNAME;
            //Skip create Sync Framework objects because we have already created them on the previous step
                serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);

                //Create new select changes procedure for our scope
serverProvision.SetCreateProceduresForAdditionalScopeDefault(DbSyncCreationOption.Create);
                if (_CPubVar._Stop_bool)
                {
                    return;
                }
            if (!serverProvision.ScopeExists(_pScopeName))
                serverProvision.Apply();

            // Provision the SQL client database from the on-premise SQL Server database if one does not exist.
            SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, productScope);

            if (_CPubVar._Stop_bool)
            {
                return;
            }
            if (!clientProvision.ScopeExists(_pScopeName))
                clientProvision.Apply();    
        }

i am getting Error :

The multi-part identifier "_ATTENDANCESTATUS._WORKGROUPNAME" could not be bound.
Invalid column name 'FPR'.

at line :

serverProvision.Apply();

if i remove below lines my Sync process work correctly without filter

string _tblName = _pDbSyncTable.LocalName.Replace("[", "").Replace("]", "");
            serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
            serverProvision.Tables[_tblName].AddFilterColumn("_WORKGROUPNAME");
            serverProvision.Tables[_pDbSyncTable.LocalName].FilterClause = "[" + _tblName + "].[_WORKGROUPNAME] = " + _CCompanyVar._WORKGROUPNAME;
            //Skip create Sync Framework objects because we have already created them on the previous step
            serverProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
            //Create new select changes procedure for our scope
            serverProvision.SetCreateProceduresForAdditionalScopeDefault(DbSyncCreationOption.Create);

_WORKGROUPNAME is a common field in my all tables and I want to filter data on _WORKGROUPNAME which i pass value in _CCompanyVar._WORKGROUPNAME (string variable)...

where I am wrong please point out ...


Solution

  • use this ...

    public static void SetUp(string _pScopeName, DbSyncTableDescription _pDbSyncTable, SqlConnection serverConn, SqlConnection clientConn)
            {
                // Create a scope named "_ITEM" and add tables to it.
                DbSyncScopeDescription productScope = new DbSyncScopeDescription(_pScopeName);
    
                // Define the Products table.
                // Add the Table to the scope object.    
                Collection<string> includeColumns = new Collection<string>();
                for (int i = 0; i < _pDbSyncTable.Columns.Count; i++)
                {
                    includeColumns.Add(_pDbSyncTable.Columns[i].UnquotedName);
                }
                DbSyncTableDescription productDescription = SqlSyncDescriptionBuilder.GetDescriptionForTable(_pScopeName, includeColumns, serverConn);
                productScope.Tables.Add(productDescription);
    
                // Create a provisioning object for "_ITEM" and apply it to the on-premise database if one does not exist.
                SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, productScope);
                serverProvision.ObjectSchema = ".dbo";                
                // Filter Rows for the ListPrice column
                serverProvision.Tables[ _pDbSyncTable.LocalName].AddFilterColumn("_WORKGROUPNAME");
                serverProvision.Tables[ _pDbSyncTable.LocalName].FilterClause = "[side].[_WORKGROUPNAME] = '" + _CCompanyVar._WORKGROUPNAME + "'";
    
                if (_CPubVar._Stop_bool)
                {
                    return;
                }
                if (!serverProvision.ScopeExists(_pScopeName))
                    serverProvision.Apply();
    
                // Provision the SQL client database from the on-premise SQL Server database if one does not exist.
                SqlSyncScopeProvisioning clientProvision = new SqlSyncScopeProvisioning(clientConn, productScope);
    
                if (_CPubVar._Stop_bool)
                {
                    return;
                }
                if (!clientProvision.ScopeExists(_pScopeName))
                    clientProvision.Apply();
    
            }