public class DemoClientSyncProvider : SqlCeClientSyncProvider
{
public DemoClientSyncProvider()
this.ConnectionString = ConfigurationManager.ConnectionStrings["ClientConnexionString"];
}
public DemoClientSyncProvider(string connectionString)
this.ConnectionString = connectionString;
public partial class DemoSyncAgent : SyncAgent
private SyncGroup globalSyncGroup;
private SyncTable clientSyncTable;
private SyncTable clientTypeSyncTable;
public DemoSyncAgent()
this.InitializeSyncProviders();
this.InitializeSyncTables();
public SyncTable Client
get
return this.clientSyncTable;
set
this.Configuration.SyncTables.Remove(this.clientSyncTable);
this.clientSyncTable = value;
this.Configuration.SyncTables.Add(this.clientSyncTable);
public SyncTable ClientType
return this.clientTypeSyncTable;
this.Configuration.SyncTables.Remove(this.clientTypeSyncTable);
this.clientTypeSyncTable = value;
this.Configuration.SyncTables.Add(this.clientTypeSyncTable);
private void InitializeSyncProviders()
// this.RemoteProvider = new DemoServerSyncProvider();
this.LocalProvider = new DemoClientSyncProvider();
private void InitializeSyncTables()
// Creation du SyncGroup.
this.globalSyncGroup = new Microsoft.Synchronization.Data.SyncGroup("DemoGlobalSyncGroup");
// Creation des SyncTables.
this.clientSyncTable = new SyncTable();
this.clientSyncTable.TableName = "Client";
this.clientSyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;
this.clientSyncTable.SyncDirection = SyncDirection.Bidirectional;
this.clientSyncTable.SyncGroup = globalSyncGroup;
this.clientTypeSyncTable = new SyncTable();
this.clientTypeSyncTable.SyncGroup = globalSyncGroup;
this.clientTypeSyncTable.TableName = "ClientType";
this.clientTypeSyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;
this.clientTypeSyncTable.SyncDirection = SyncDirection.Snapshot;
public partial class DemoServerSyncProvider : DbServerSyncProvider
private SyncAdapter clientSyncAdapter;
private SyncAdapter clientTypeSyncAdapter;
public DemoServerSyncProvider()
string connectionString = ConfigurationManager.ConnectionStrings["ServeurConnexionString"].ConnectionString;
this.InitializeConnection(connectionString);
public DemoServerSyncProvider(string connectionString)
public SyncAdapter ClientSyncAdapter
return this.clientSyncAdapter;
this.clientSyncAdapter = value;
public SyncAdapter ClientTypeSyncAdapter
return this.clientTypeSyncAdapter;
this.clientTypeSyncAdapter = value;
private void InitializeConnection(string connectionString)
this.Connection = new System.Data.SqlClient.SqlConnection(connectionString);
private void InitializeNewAnchorCommand()
this.SelectNewAnchorCommand = new SqlCommand();
this.SelectNewAnchorCommand.CommandText =
"Select @" + SyncSesion.SyncNewReceivedAnchor + " = GETUTCDATE()";
// Cas du TimeStamp
// this.SelectNewAnchorCommand.CommandText = "Select @"
// + SyncSession.SyncNewReceivedAnchor + " = @@DBTS";
// SQL Server 2005 SP2 et plus : Utiliser "min_active_rowversion() - 1"
this.SelectNewAnchorCommand.CommandType = CommandType.Text;
SqlParameter anchorParameter =
new SqlParameter("@" + SyncSession.SyncNewReceivedAnchor, System.Data.SqlDbType.DateTime);
// SqlParameter anchorParameter = new SqlParameter("@sync_new_received_anchor",
// System.Data.SqlDbType.Timestamp);
anchorParameter.Direction = System.Data.ParameterDirection.Output;
this.SelectNewAnchorCommand.Parameters.Add(anchorParameter);
this.clientSyncAdapter = new SyncAdapter();
this.clientTypeSyncAdapter = new SyncAdapter();
SqlSyncAdapterBuilder clientBuilder = new SqlSyncAdapterBuilder();
clientBuilder.SyncDirection = SyncDirection.Bidirectional;
clientBuilder.Connection = this.Connection as SqlConnection;
SqlSyncAdapterBuilder clientTypeBuilder = new SqlSyncAdapterBuilder();
clientTypeBuilder.SyncDirection = SyncDirection.Snapshot;
clientTypeBuilder.Connection = this.Connection as SqlConnection;
// base table
clientBuilder.TableName = "Client";
clientBuilder.DataColumns.Add("ClientId");
clientBuilder.DataColumns.Add("EmployeId");
clientBuilder.DataColumns.Add("ClientTypeId");
clientBuilder.DataColumns.Add("Prenom");
clientBuilder.DataColumns.Add("Nom");
clientBuilder.DataColumns.Add("Adresse");
clientBuilder.DataColumns.Add("CreationDate");
clientBuilder.DataColumns.Add("LastEditDate");
// tombstone table
clientBuilder.TombstoneTableName = "Client_Tombstone";
clientBuilder.TombstoneDataColumns.Add("ClientId");
clientBuilder.TombstoneDataColumns.Add("DeletionDate");
// tracking\sync columns
clientBuilder.CreationTrackingColumn = @"CreationDate";
clientBuilder.UpdateTrackingColumn = @"LastEditDate";
clientBuilder.DeletionTrackingColumn = @"DeletionDate";
clientTypeBuilder.TableName = "ClientType";
clientTypeBuilder.DataColumns.Add("ClientTypeId");
clientTypeBuilder.DataColumns.Add("Libelle");
// Generation des syncadapters
this.clientTypeSyncAdapter = clientBuilder.ToSyncAdapter();
this.clientSyncAdapter = clientBuilder.ToSyncAdapter();
this.SyncAdapters.Add(this.clientSyncAdapter);
this.SyncAdapters.Add(this.clientTypeSyncAdapter);
private void btnSynchronize_Click(object sender, EventArgs e)
if (!File.Exists(SqlCeDataBaseName))
string clientConnectionString =
ConfigurationManager.ConnectionStrings["ClientConnectionString"].ConnectionString;
SqlCeEngine sqlCeEngine = new SqlCeEngine(clientConnectionString);
sqlCeEngine.CreateDatabase();
DemoSyncAgent agent = new DemoSyncAgent();
agent.Synchronize();
// Personnalisation
clientBuilder.FilterClause = "EmployeId = @EmployeId";
clientBuilder.FilterParameters.Add(new SqlParameter("@EmployeId", SqlDbType.UniqueIdentifier));
Guid empGuid = new Guid(ConfigurationManager.AppSettings["EmployeId"]);
agent.Configuration.SyncParameters.Add(
new SyncParameter("@EmployeId", empGuid));