string publisherName = "DataSourceName"; string publicationName = "AdvWorksSalesOrdersMerge"; string publicationDbName = "AdventureWorksDW2008R2"; ReplicationDatabase publicationDb; MergePublication publication; // Create a connection to the Publisher. ServerConnection conn = new ServerConnection(publisherName); try { //Connect to the Publisher. conn.Connect(); // Enable the database for merge publication. publicationDb = new ReplicationDatabase(publicationDbName, conn); if (publicationDb.LoadProperties()) { if (!publicationDb.EnabledMergePublishing) { publicationDb.EnabledMergePublishing = true; } } else { // Do something here if the database does not exist. throw new ApplicationException(String.Format( "The {0} database does not exist on {1}.", publicationDb, publisherName)); } // Set the required properties for the merge publication. publication = new MergePublication(); publication.ConnectionContext = conn; publication.Name = publicationName; publication.DatabaseName = publicationDbName; // Enable precomputed partitions. publication.PartitionGroupsOption = PartitionGroupsOption.True; //Specify the Windows account under which the Snapshot Agent job runs. // This account will be used for the local connection to the // Distributor and all agent connections that use Windows Authentication. publication.SnapshotGenerationAgentProcessSecurity.Login = userid; publication.SnapshotGenerationAgentProcessSecurity.Password = password; //Explicitly set the security mode for the Publisher connection // Windows Authentication (the default). publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true; //Enable Subscribers to request snapshot generation and filtering. publication.Attributes |= PublicationAttributes.AllowSubscriberInitiatedSnapshot; publication.Attributes |= PublicationAttributes.DynamicFilters; // Enable pull and push subscriptions. publication.Attributes |= PublicationAttributes.AllowPull; publication.Attributes |= PublicationAttributes.AllowPush; if (!publication.IsExistingObject) { //Create the merge publication. publication.Create(); // Create a Snapshot Agent job for the publication. publication.CreateSnapshotAgent(); } else { throw new ApplicationException(String.Format( "The {0} publication already exists.", publicationName)); } } catch (Exception ex) { //Implement custom application error handling here. throw new Exception(String.Format("The publication {0} could not be created.", publicationName), ex); } finally { conn.Disconnect(); }
publicationDb.EnabledTransPublishing = true;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)