public void SynchronizeMergePullSubscriptionViaRMO() { // Create a connection to the Subscriber. //ServerConnection conn = new ServerConnection(subscriberName); ServerConnection conn = new ServerConnection(publisherName, srvLogin, srvPass); // Merge pull subscription MergeSubscription subscription; try { // Connect to the Subscriber. conn.Connect(); // Define the pull subscription. subscription = new MergeSubscription(); subscription.ConnectionContext = conn; subscription.DatabaseName = publicationDbName; subscription.PublicationName = publicationName; subscription.SubscriptionDBName = subscriptionDbName; subscription.SubscriberName = subscriberName; subscription.PublisherSecurity.SqlStandardLogin = srvLogin; subscription.PublisherSecurity.SqlStandardPassword = srvPass; // If the pull subscription exists, then start the synchronization. if (subscription.LoadProperties() && subscription.AgentJobId != null) { // Get the agent for the subscription. agent = subscription.SynchronizationAgent; // Set the required properties that could not be returned // from the MSsubscription_properties table. agent.DistributorSecurityMode = SecurityMode.Standard; agent.DistributorLogin = srvLogin; agent.DistributorPassword = srvPass; agent.PublisherSecurityMode = SecurityMode.Standard; agent.PublisherLogin = srvLogin; agent.PublisherPassword = srvPass; // Enable verbose merge agent output to file. //agent.OutputVerboseLevel = 4; //agent.Output = "C:\\TEMP\\mergeagent.log"; // Handle the Status event agent.Status += new AgentCore.StatusEventHandler(agent_Status); // Synchronously start the Merge Agent for the subscription. agent.Synchronize(); } else { // Do something here if the pull subscription does not exist. throw new ApplicationException(String.Format( "A subscription to '{0}' does not exist on {1}", publicationName, subscriberName)); } } catch (Exception ex) { // Implement appropriate error handling here. throw new ApplicationException("The subscription could not be " + "synchronized. Verify that the subscription has " + "been defined correctly.", ex); } finally { conn.Disconnect(); } }
private bool SynchronizeSubscription(SubscriberSubscription subscriptionparameters, string serverName) { bool result = true; ServerConnection conn = null; try { conn = new ServerConnection(serverName); // Connect to the Subscriber. conn.Connect(); if (!conn.IsOpen) { return(false); } // Define the pull subscription. MergePullSubscription subscription = new MergePullSubscription(); subscription.ConnectionContext = conn; subscription.DatabaseName = subscriptionparameters.SubscriptionDBName; subscription.PublisherName = subscriptionparameters.PublisherName; subscription.PublicationDBName = subscriptionparameters.PublicationDBName; subscription.PublicationName = subscriptionparameters.PublicationName; // If the pull subscription exists, then start the synchronization. if (subscription.LoadProperties()) { // Get the agent for the subscription. MergeSynchronizationAgent agent = subscription.SynchronizationAgent; agent.UseInteractiveResolver = true; agent.InternetTimeout = 600; //if (File.Exists(@"c:\DotNet\tosi2010-09-01.zip")) //{ // DateTime dt = DateTime.Now; // agent.Output = // string.Format(@"c:\DotNet\out_{0}_{1}_{2}_{3}_{4}.txt", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute); // agent.OutputVerboseLevel = 2; //} // Synchronously start the Merge Agent for the subscription. agent.Status += OnAgentStatusEventHandler; agent.Synchronize(); Info.ConflittiPub = agent.PublisherConflicts; Info.ConflittiSub = agent.SubscriberConflicts; Info.ModificheSub = agent.SubscriberChanges; Info.ModifichePub = agent.PublisherChanges; if (agent.PublisherConflicts > 0 || agent.SubscriberConflicts > 0) { Log("**************************************"); Log(string.Format("Conflitti: pub: {0}, sub: {1}", agent.PublisherConflicts, agent.SubscriberConflicts )); } } else { // Do something here if the pull subscription does not exist. throw new ApplicationException(String.Format( "La sottoscrizione '{0}' non esiste sul server {1}", subscriptionparameters.PublicationName, conn.ServerInstance)); } } catch (ComErrorException comErrorEx) { result = false; throw new ApplicationException( "Impossibile connettersi al server di pubblicazione, " + "verificare la connessione di rete e che la sottoscrizione " + "sia stata creata correttamnte.", comErrorEx); } catch (Exception ex) { result = false; // Implement appropriate error handling here. throw new ApplicationException("The subscription could not be " + "synchronized. Verify that the subscription has " + "been defined correctly.", ex); } finally { if (conn != null) { conn.Disconnect(); } } Info.Result = result; return(result); }