Esempio n. 1
0
        public void SynchronizeSubscriptionUploadOnly()
        {
            MergeSynchronizationAgent syncAgent;

            try
            {
                // Make the connection and get the subscription properties.
                subscriberConn = new ServerConnection(subscriberServer);
                subscriberConn.Connect();
            }
            catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex)
            {
                ExceptionMessageBox emb = new ExceptionMessageBox(
                    Properties.Resources.ExceptionCannotConnectLocal,
                    Properties.Resources.ExceptionSqlServerError,
                    ExceptionMessageBoxButtons.OK);
                emb.InnerException = ex;
                emb.Show(this);

                // Shutdown the application because we can't continue.
                Application.Exit();
            }

            mergePullSub = new MergePullSubscription();

            // Set the properties needed to get the subscription.
            mergePullSub.ConnectionContext = subscriberConn;
            mergePullSub.PublicationName   = publicationName;
            mergePullSub.PublisherName     = publisherServer;
            mergePullSub.PublicationDBName = publicationDatabase;
            mergePullSub.DatabaseName      = subscriptionDatabase;

            // Load the properties of the existing subscription.
            if (!mergePullSub.LoadProperties())
            {
                throw new ApplicationException(
                          Properties.Resources.ExceptionProblemLocalData
                          + Environment.NewLine
                          + Properties.Resources.ExceptionContactTechSupport);
            }

            // Get the Merge Agent for synchronous execution.
            syncAgent = mergePullSub.SynchronizationAgent;

            // Specify an upload-only exchange type.
            syncAgent.ExchangeType = MergeExchangeType.Upload;

            // Generate a troubleshooting log file.
            syncAgent.Output             = outputLogFile;
            syncAgent.OutputVerboseLevel = outputLevel;

            // Define the event handler.
            syncAgent.Status += new AgentCore.StatusEventHandler(Sync_Status);

            // Start the Merge Agent Job.
            try
            {
                syncAgent.Synchronize();
            }
            catch (Exception ex)
            {
                statusProgressBar.Value = 0;
                throw new ApplicationException(
                          Properties.Resources.ExceptionMergeAgentFailedSync,
                          ex);
            }
            finally
            {
                closeButton.Enabled = true;
                subscriberConn.Disconnect();
            }
        }
Esempio n. 2
0
        public void ReinitializeSubscriptionWithUpload()
        {
            MergeSynchronizationAgent syncAgent;

            try
            {
                // Make the connection and get the subscription properties.
                subscriberConn = new ServerConnection(subscriberServer);
                subscriberConn.Connect();
            }
            catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex)
            {
                ExceptionMessageBox emb = new ExceptionMessageBox(
                    Properties.Resources.ExceptionCannotConnectLocal,
                    Properties.Resources.ExceptionSqlServerError,
                    ExceptionMessageBoxButtons.OK);
                emb.InnerException = ex;
                emb.Show(this);

                // Shutdown the application because we can't continue.
                Application.Exit();
            }

            mergePullSub = new MergePullSubscription();

            // Set the properties needed to get the subscription.
            mergePullSub.ConnectionContext = subscriberConn;
            mergePullSub.PublicationName   = publicationName;
            mergePullSub.PublisherName     = publisherServer;
            mergePullSub.PublicationDBName = publicationDatabase;
            mergePullSub.DatabaseName      = subscriptionDatabase;

            // Load the properties of the existing subscription.
            if (!mergePullSub.LoadProperties())
            {
                throw new ApplicationException(
                          Properties.Resources.ExceptionProblemLocalData
                          + Environment.NewLine
                          + Properties.Resources.ExceptionContactTechSupport);
            }

            // Check to make sure that the Merge Agent isn't already running.
            if (mergePullSub.LastAgentStatus
                != (ReplicationStatus.Running | ReplicationStatus.Starting))
            {
                // Mark the subscription for reinitialization after uploading.
                mergePullSub.Reinitialize(true);

                // Get the Merge Agent for synchronous execution.
                syncAgent = mergePullSub.SynchronizationAgent;

                // Define the event handler.
                syncAgent.Status
                    += new AgentCore.StatusEventHandler(Sync_Status);

                syncAgent.Output             = outputLogFile;
                syncAgent.OutputVerboseLevel = outputLevel;

                // Start the Merge Agent Job.
                try
                {
                    currentStatusTextBox.Text = statusReinitialize
                                                + Environment.NewLine;
                    syncAgent.Synchronize();
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(
                              Properties.Resources.ExceptionCouldNotReInit
                              + Environment.NewLine
                              + Properties.Resources.ExceptionContactTechSupport, ex);
                }
                finally
                {
                    statusProgressBar.Value = 100;
                    closeButton.Enabled     = true;
                    subscriberConn.Disconnect();
                }
            }
            else
            {
                currentStatusTextBox.Text
                    = Properties.Resources.StatusSubscriptionAlreadySync;
                closeButton.Enabled = true;
            }
        }
        // This event handler initiates the synchronization
        private void syncBackgroundWorker_DoWork()
        {
            // Connect to the Subscriber and synchronize

            // Create a connection to the Subscriber.
            ServerConnection conn = new ServerConnection(subscriberName);

            // Merge pull subscription
            MergePullSubscription subscription;

            try
            {
                // Connect to the Subscriber.
                conn.Connect();

                // Define the pull subscription.
                subscription = new MergePullSubscription();
                subscription.ConnectionContext = conn;
                subscription.DatabaseName      = subscriptionDbName;
                subscription.PublisherName     = publisherName;
                subscription.PublicationDBName = publicationDbName;
                subscription.PublicationName   = publicationName;

                // If the pull subscription exists, then start the synchronization.
                if (subscription.LoadProperties())
                {
                    // Get the agent for the subscription.
                    agent = subscription.SynchronizationAgent;

                    // Set the required properties that could not be returned
                    // from the MSsubscription_properties table.
                    agent.PublisherSecurityMode   = SecurityMode.Integrated;
                    agent.DistributorSecurityMode = SecurityMode.Integrated;
                    agent.Distributor             = publisherName;

                    // 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();
            }
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public void SynchronizeSubscriptionUploadOnly()
        {
            MergeSynchronizationAgent syncAgent;

            try
            {
                // Make the connection and get the subscription properties.
                subscriberConn = new ServerConnection(subscriberServer);
                subscriberConn.Connect();
            }
            catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex)
            {
                ExceptionMessageBox emb = new ExceptionMessageBox(
                    Properties.Resources.ExceptionCannotConnectLocal,
                    Properties.Resources.ExceptionSqlServerError,
                    ExceptionMessageBoxButtons.OK);
                emb.InnerException = ex;
                emb.Show(this);

                // Shutdown the application because we can't continue.
                Application.Exit();
            }

            mergePullSub = new MergePullSubscription();

            // Set the properties needed to get the subscription.
            mergePullSub.ConnectionContext = subscriberConn;
            mergePullSub.PublicationName = publicationName;
            mergePullSub.PublisherName = publisherServer;
            mergePullSub.PublicationDBName = publicationDatabase;
            mergePullSub.DatabaseName = subscriptionDatabase;

            // Load the properties of the existing subscription.
            if (!mergePullSub.LoadProperties())
            {
                throw new ApplicationException(
                    Properties.Resources.ExceptionProblemLocalData
                    + Environment.NewLine
                    + Properties.Resources.ExceptionContactTechSupport);
            }

            // Get the Merge Agent for synchronous execution.
            syncAgent = mergePullSub.SynchronizationAgent;

            // Specify an upload-only exchange type.
            syncAgent.ExchangeType = MergeExchangeType.Upload;

            // Generate a troubleshooting log file.
            syncAgent.Output = outputLogFile;
            syncAgent.OutputVerboseLevel = outputLevel;

            // Define the event handler.
            syncAgent.Status += new AgentCore.StatusEventHandler(Sync_Status);

            // Start the Merge Agent Job.
            try
            {
                syncAgent.Synchronize();
            }
            catch (Exception ex)
            {
                statusProgressBar.Value = 0;
                throw new ApplicationException(
                    Properties.Resources.ExceptionMergeAgentFailedSync,
                    ex);
            }
            finally
            {
                closeButton.Enabled = true;
                subscriberConn.Disconnect();
            }
        }
Esempio n. 6
0
        public void ReinitializeSubscriptionWithUpload()
        {
            MergeSynchronizationAgent syncAgent;

            try
            {
                // Make the connection and get the subscription properties.
                subscriberConn = new ServerConnection(subscriberServer);
                subscriberConn.Connect();
            }
            catch (Microsoft.SqlServer.Replication.ConnectionFailureException ex)
            {
                ExceptionMessageBox emb = new ExceptionMessageBox(
                    Properties.Resources.ExceptionCannotConnectLocal,
                    Properties.Resources.ExceptionSqlServerError,
                    ExceptionMessageBoxButtons.OK);
                emb.InnerException = ex;
                emb.Show(this);

                // Shutdown the application because we can't continue.
                Application.Exit();
            }

            mergePullSub = new MergePullSubscription();

            // Set the properties needed to get the subscription.
            mergePullSub.ConnectionContext = subscriberConn;
            mergePullSub.PublicationName = publicationName;
            mergePullSub.PublisherName = publisherServer;
            mergePullSub.PublicationDBName = publicationDatabase;
            mergePullSub.DatabaseName = subscriptionDatabase;

            // Load the properties of the existing subscription.
            if (!mergePullSub.LoadProperties())
            {
                throw new ApplicationException(
                    Properties.Resources.ExceptionProblemLocalData
                    + Environment.NewLine
                    + Properties.Resources.ExceptionContactTechSupport);
            }

            // Check to make sure that the Merge Agent isn't already running.
            if (mergePullSub.LastAgentStatus
                != (ReplicationStatus.Running | ReplicationStatus.Starting))
            {
                // Mark the subscription for reinitialization after uploading.
                mergePullSub.Reinitialize(true);

                // Get the Merge Agent for synchronous execution.
                syncAgent = mergePullSub.SynchronizationAgent;

                // Define the event handler.
                syncAgent.Status
                    += new AgentCore.StatusEventHandler(Sync_Status);

                syncAgent.Output = outputLogFile;
                syncAgent.OutputVerboseLevel = outputLevel;

                // Start the Merge Agent Job.
                try
                {
                    currentStatusTextBox.Text = statusReinitialize
                        + Environment.NewLine;
                    syncAgent.Synchronize();
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(
                        Properties.Resources.ExceptionCouldNotReInit
                        + Environment.NewLine
                        + Properties.Resources.ExceptionContactTechSupport, ex);
                }
                finally
                {
                    statusProgressBar.Value = 100;
                    closeButton.Enabled = true;
                    subscriberConn.Disconnect();
                }
            }
            else
            {
                currentStatusTextBox.Text
                    = Properties.Resources.StatusSubscriptionAlreadySync;
                closeButton.Enabled = true;
            }
        }