locked
Sync Filter over WCF? RRS feed

  • Question

  • I have a simple little app that syncs to a wcf service all create with the wizard. Now I would like to add a filter and from a bit of googling it looks like I can just change the SelectIncrementalInsertsCommand + update comands?

    public partial class ProductsSyncAdapter
      {
        partial void OnInitialized()
        {
          string FilterText = "WHERE ([Sku] = 'filter') AND ";
    
          this.SelectIncrementalInsertsCommand.CommandText =
            this.SelectIncrementalInsertsCommand.CommandText.Replace("WHERE", FilterText);
        }
      }

    and this works fine but it sits on the server side code so i dont know how to pass or set the filter on my client?

    client side code for reference:

    public partial class LocalProductsCacheSyncAgent
      {
    
        partial void OnInitialized()
        {
          
          this.Products.SyncDirection = SyncDirection.Bidirectional;
    
          LocalProductsCacheClientSyncProvider localProvider = this.LocalProvider as LocalProductsCacheClientSyncProvider;
          localProvider.CreateDatabaseIfNotExists = true;
    
          this.RemoteProvider =
            new ServerSyncProviderProxy(
              new SyncServiceReference.LocalProductsCacheSyncContractClient()
            );
        }
      }

    My client sync code:

    static void Main(string[] args)
        {
          //---sync with the database---
          Console.WriteLine("Press any key to start sync process.");
          Console.ReadLine();
          Console.WriteLine("Starting sync...");
          
          LocalProductsCacheSyncAgent syncAgent = new LocalProductsCacheSyncAgent();
    
          // I think this is more or less wat i need to do but does not work
    
          // error: Cannot convert type 'Microsoft.Synchronization.SyncProvider' to 'WCFSyncServer.LocalProductsCacheServerSyncProvider'
          WCFSyncServer.LocalProductsCacheServerSyncProvider remoteProvider = (WCFSyncServer.LocalProductsCacheServerSyncProvider)syncAgent.RemoteProvider;
          //to be able to access
          //remoteProvider.SelectIncrementalInsertsCommand.CommandText
    
          SyncStatistics syncStats = syncAgent.Synchronize();
          
          Console.WriteLine(
            "Sync Completed... Down:{0} Up:{1}", 
            syncStats.TotalChangesDownloaded, 
            syncStats.TotalChangesUploaded);
          Console.WriteLine("Press any key to exit.");
          Console.ReadLine();
        }

    Friday, April 20, 2012 10:38 AM

Answers

  • Look up how to: filter rows and columns under offline scenarios in the documentation. If you have set the filter parameter in the selectxxxcommands, you can pass the filter value thru the syncagent's configuration.syncparameters property
    • Marked as answer by Louw Grobler Saturday, April 21, 2012 10:42 PM
    Friday, April 20, 2012 11:59 AM

All replies

  • where are these designer generated parameters actually being set

    this.SelectIncrementalInsertsCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_last_received_anchor", System.Data.SqlDbType.BigInt));

    then i can just add my custom filter as @filter and hopefully set the value for it on my client?

    Friday, April 20, 2012 11:04 AM
  • Look up how to: filter rows and columns under offline scenarios in the documentation. If you have set the filter parameter in the selectxxxcommands, you can pass the filter value thru the syncagent's configuration.syncparameters property
    • Marked as answer by Louw Grobler Saturday, April 21, 2012 10:42 PM
    Friday, April 20, 2012 11:59 AM
  • Thanks, I've added a @parameter to my ServerSyncAdapter's SelectIncrementalInsertsCommand

    public partial class ProductsSyncAdapter
      {
        partial void OnInitialized()
        {
          string FilterText = "WHERE ([Sku] = '@Sku') AND ";
     
          this.SelectIncrementalInsertsCommand.CommandText =
            this.SelectIncrementalInsertsCommand.CommandText.Replace("WHERE", FilterText);
        }
      }

    and then I added a 

    syncAgent.Configuration.SyncParameters.Add(new SyncParameter("@Sku""002"));
    

    on the client. The code runs without error, but it looks like there might still be a bug... no data is being returned. Reckon I'm just missing something stupid.

    thanks again.

    Saturday, April 21, 2012 10:42 PM
  • hehe getting late i see... I put the @Sku parameter in quotes : \

    also I had to add the parameter to the SelectIncrementalInsertsCommand parameter list.

    var selectIncrementalInsertsCommandFilterParameter = new SqlParameter("@FilterParameter", System.Data.SqlDbType.NChar);
    selectIncrementalInsertsCommandFilterParameter.Direction = ParameterDirection.Input;
    this.SelectIncrementalInsertsCommand.Parameters.Add(selectIncrementalInsertsCommandFilterParameter);

    working great and soo easy :)

    > \ninjabomb

    Sunday, April 22, 2012 12:05 AM