如何使用Microsoft Sync Framework与过滤器数据同步

时间:2014-07-22 07:20:48

标签: c# sql sql-server synchronization microsoft-sync-framework

我正在使用sql server 2008在Microsoft Sync Framework上工作,下面的c#是我的代码

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();    
        }

我收到错误:

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

在线:

serverProvision.Apply();

如果我删除以下行,我的同步过程无需过滤器正常工作

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是我所有表格中的公共字段,我希望过滤_WORKGROUPNAME上的数据,我在_CCompanyVar._WORKGROUPNAME(字符串变量)中传递值...

我错了,请指出......

1 个答案:

答案 0 :(得分:0)

使用此...

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();

        }