private void ProvisionServer(string TableName, SqlParameter Parameter, string ParamValue) { try { // Create a synchronization scope for OriginState=WA. SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(_serverConnection); // populate the scope description using the template serverProvision.PopulateFromTemplate(TableName + _filter, TableName + "_Filter_Template"); // specify the value we want to pass in the filter parameter, in this case we want only orders from WA serverProvision.Tables[TableName].FilterParameters[Parameter.ParameterName].Value = Guid.Parse(ParamValue); // Set a friendly description of the template. serverProvision.UserComment = TableName + " data includes only " + ParamValue; // Create the new filtered scope in the database. if (!serverProvision.ScopeExists(serverProvision.ScopeName)) { serverProvision.Apply(); Log.WriteLogs("Server " + TableName + " was provisioned."); } else { Log.WriteLogs("Server " + TableName + " was provisioned."); } } catch (Exception ex) { Log.WriteErrorLogs(ex); } }
/// <summary> /// Create a new scope for a client. This method is called when GetChanges is passed a null blob. /// The requested scope is compared to an existing scope or a template and a new scope is provisioned /// If the requested scope is an existing template, filter parameters, if present, are added when provisioning. /// /// Note: If both scope and template match the requested scope, we prefer the scope. We would need to expose this /// out to the service if we want to make this choice configurable. /// </summary> private void CreateNewScopeForClient() { using (var serverConnection = new SqlConnection(_configuration.ServerConnectionString)) { // Default's to scope. // Note: Do not use constructors that take in a DbSyncScopeDescription since there are checks internally // to ensure that it has atleast 1 table. In this case we would be passing in a non-existing scope which throws an // exception. var provisioning = new SqlSyncScopeProvisioning(serverConnection); // Set the ObjectSchema property. Without this, the TemplateExists and ScopeExists method // always return false if the sync objects are provisioned in a non-dbo schema. if (!String.IsNullOrEmpty(_configuration.SyncObjectSchema)) { provisioning.ObjectSchema = _configuration.SyncObjectSchema; } // Determine if this is a scope or a template. //Note: Scope has a higher priority than a template. See method summary for more info. bool isTemplate; if (provisioning.ScopeExists(_scopeName)) { isTemplate = false; } else if (provisioning.TemplateExists(_scopeName)) { isTemplate = true; } else { throw SyncServiceException.CreateBadRequestError(Strings.NoScopeOrTemplateFound); } // If scope... if (!isTemplate) { DbSyncScopeDescription scopeDescription = String.IsNullOrEmpty(_configuration.SyncObjectSchema) ? SqlSyncDescriptionBuilder.GetDescriptionForScope(_scopeName, serverConnection) : SqlSyncDescriptionBuilder.GetDescriptionForScope(_scopeName, string.Empty /*objectPrefix*/, _configuration.SyncObjectSchema, serverConnection); scopeDescription.ScopeName = _clientScopeName; provisioning.PopulateFromScopeDescription(scopeDescription); // If scope then disable bulk procedures. // Template provisioning does not create anything. provisioning.SetUseBulkProceduresDefault(false); } // If template... else { provisioning.PopulateFromTemplate(_clientScopeName, _scopeName); // Add filter parameters. if (null != _filterParams && 0 != _filterParams.Count) { foreach (var param in _filterParams) { provisioning.Tables[param.TableName].FilterParameters[param.SqlParameterName].Value = param.Value; } } } if (!provisioning.ScopeExists(_clientScopeName)) { provisioning.Apply(); } } }
public static void CreateDatabaseProvisionFilter(SqlConnection serverConn, SqlCeConnection clientConn, string scopeName, string filterName, string filterValue, Collection<string> columnsToInclude) { string filterTemplate = scopeName + "_filter_template"; DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription(filterTemplate); DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(scopeName.Replace("Scope", ""), columnsToInclude, serverConn); scopeDesc.Tables.Add(tableDesc); SqlSyncScopeProvisioning serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc, SqlSyncScopeProvisioningType.Template); serverProvision.Tables[scopeName.Replace("Scope", "")].AddFilterColumn(filterName); serverProvision.Tables[scopeName.Replace("Scope", "")].FilterClause = "[side].[" + filterName + "] = @" + filterName; SqlParameter parameter = new SqlParameter("@" + filterName, SqlDbType.NVarChar, 8); serverProvision.Tables[scopeName.Replace("Scope", "")].FilterParameters.Add(parameter); if (!serverProvision.TemplateExists(filterTemplate)) { serverProvision.Apply(); } serverProvision = new SqlSyncScopeProvisioning(serverConn, scopeDesc); serverProvision.PopulateFromTemplate(scopeName + "-" + filterName + filterValue, filterTemplate); serverProvision.Tables[scopeName.Replace("Scope", "")].FilterParameters["@" + filterName].Value = filterValue; if (!serverProvision.ScopeExists(scopeName + "-" + filterName + filterValue)) { serverProvision.Apply(); } scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(scopeName + "-" + filterName + filterValue, serverConn); SqlCeSyncScopeProvisioning clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeDesc); if (!clientProvision.ScopeExists(scopeName + "-" + filterName + filterValue)) { try { clientProvision.Apply(); } catch (Exception ex) { Console.WriteLine("Error {0}", ex.Message); } } }