DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Create A Merge Replication

12.05.2008
| 3594 views |
  • submit to reddit
        // Create a web merge synchronization agent on a local SQL Express machine to merge over the web to a central datasource.
    Public Sub MergeCreate()
        'Create the objects that we need.'
        Dim subscription As MergePullSubscription

        Try
            'Connect to the Subscriber.'
            conn.Connect()
            'Define the pull subscription.'
            subscription = New MergePullSubscription()
            subscription.ConnectionContext = conn
            subscription.PublisherName = publisherName
            subscription.PublicationName = publicationName
            subscription.PublicationDBName = publicationDbName

            subscription.UseWebSynchronization = True
            subscription.InternetUrl = webSyncUrl
            subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication
            subscription.InternetLogin = "WebUser"
            subscription.InternetPassword = "WebPass"

            subscription.CreateSyncAgentByDefault = True

            subscription.DatabaseName = subscriptionDbName
            subscription.SubscriberType = MergeSubscriberType.Anonymous
            subscription.SubscriberSecurity.WindowsAuthentication = False
            subscription.SubscriberSecurity.SqlStandardLogin = "localusername"
            subscription.SubscriberSecurity.SqlStandardPassword = "localpassword"
            subscription.SynchronizationAgentProcessSecurity.Login = "Standard"
            subscription.SynchronizationAgentProcessSecurity.Password = "xxxxxxxx"
            subscription.HostName = GlobalBranchID
            Try
                subscription.Create()
            Catch ex As Exception
                Throw New ApplicationException("The subscription could not be " + _
             "created. Verify that the subscription has " + _
             "been defined correctly.", ex)
            End Try
            Dim agent As MergeSynchronizationAgent
            If subscription.LoadProperties() Then
                agent = subscription.SynchronizationAgent
                If agent.PublisherSecurityMode = Nothing Then
                    agent.PublisherSecurityMode = SecurityMode.Integrated
                    agent.Distributor = "Distributor_Name"
                    agent.DistributorSecurityMode = SecurityMode.Integrated
                    agent.UseWebSynchronization = True
                    agent.InternetUrl = webSyncUrl
                    agent.InternetSecurityMode = SecurityMode.Standard
                    agent.InternetLogin = winLogin
                    agent.InternetPassword = winPassword
                End If
                If My.Settings.HasProxy Then
                    agent.InternetProxyServer = My.Settings.ProxyAddress & ":" & My.Settings.ProxyPort
                    agent.InternetProxyLogin = My.Settings.ProxyUsername
                    agent.InternetProxyPassword = My.Settings.ProxyPassword
                End If
                agent.OutputVerboseLevel = 1
                If File.Exists(mPath & "\createmerge.txt") Then
                    Kill(mPath & "\createmerge.txt")
                End If
                agent.Output = mPath & "\createmerge.txt"
                agent.Synchronize()
            Else
                Throw New ApplicationException(String.Format( _
                 "A subscription to '{0}' does not exist on {1}", _
                 publicationName, subscriberName))
            End If
        Catch ex As Exception
            ThrowError(ex)
        Finally
            conn.Disconnect()
        End Try
    End Sub