Microsoft KB Archive/319647

From BetaArchive Wiki
Knowledge Base


Article ID: 319647

Article Last Modified on 12/26/2003



APPLIES TO

  • Microsoft SQL Server 2000 Standard Edition
  • Microsoft SQL Server 2000 64-bit Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition



This article was previously published under Q319647

SUMMARY

You can use the SQL Server 2000 Replication ActiveX controls to embed replication functionality inside custom applications. This article demonstrates how to program the SQL Merge control with Microsoft Visual Basic .NET.

back to the top

Step 1

Before you insert the sample code in a Visual Basic .NET project, follow these steps:

  1. Make sure to properly configure the publisher, the distributor, and the subscriber, and make sure that they are all on SQL Server 2000.
  2. Create a merge publication that is called "SampleMergePublication". The publishing database is Northwind.

back to the top

Step 2

The following sample code generates a snapshot by using the SQL Merge control. The sample code will create the Northwind_replica subscription database. Make sure the Northwind_replica database does not already exist before you execute the sample code. Finally, the sample code creates a pull subscription in the Northwind_replica database, and then it applies the snapshot at the subscriber.

Inside the Visual Basic .NET project, add references to the Microsoft SQL Merge Control 8.0 COM object, and then add the following code:

Imports SQLMERGXLib

'This class demonstrates using the SQL Server Merge Agent replication control.
Module MergeApp

    Sub Main()

        '   Prior to running this code, replication needs to be setup as follows:
        '   
        '       Create a merge publication called "SampleMergePublication" and configure it to allow pull
        '       subscriptions. 
        '       
        '   This code will first generate the snapshot. Then the subscription database
        '   and pull subscription will be created through code. Then the snapshot will be applied at the subscriber using
        '   the SQLMergeClass object.
        '
        '   You will also need to set a reference to the following COM dll:
        '       -Microsoft SQL Merge Control 8.0     
        '

        Dim strPublisher As String
        Dim strDistributor As String
        Dim strSubscriber As String
        Dim strPublisherDatabase As String
        Dim strSubscriberDatabase As String
        Dim strPublication As String
        Dim oMerge As SQLMergeClass

        strPublisher = "PUBLISHER"   'change to the name of your publisher
        strDistributor = "DISTRIBUTOR" 'change to the name of your distributor
        strSubscriber = "SUBSCRIBER"  'change to the name of your subscriber
        strPublication = "SampleMergePublication"
        strPublisherDatabase = "Northwind"
        strSubscriberDatabase = "Northwind_replica"

        oMerge = New SQLMergeClass()

        'Set up the Publisher.
        oMerge.Publisher = strPublisher
        oMerge.PublisherSecurityMode = SQLMERGXLib.SECURITY_TYPE.NT_AUTHENTICATION
        oMerge.PublisherDatabase = strPublisherDatabase
        oMerge.Publication = strPublication

        'Set up the Distributor.
        oMerge.Distributor = strDistributor
        oMerge.DistributorSecurityMode = SQLMERGXLib.SECURITY_TYPE.NT_AUTHENTICATION

        'Set up the Subscriber.
        oMerge.Subscriber = strSubscriber
        oMerge.SubscriberDatabase = strSubscriberDatabase
        oMerge.SubscriberSecurityMode = SQLMERGXLib.SECURITY_TYPE.NT_AUTHENTICATION

        'Set up the subscription.
        oMerge.SubscriptionType = SQLMERGXLib.SUBSCRIPTION_TYPE.PULL
        oMerge.SynchronizationType = SQLMERGXLib.SYNCHRONIZATION_TYPE.AUTOMATIC
        oMerge.SubscriptionName = "PullMergeSubscription"

        'Create the database and subscription.
        oMerge.AddSubscription(SQLMERGXLib.DBADDOPTION.CREATE_DATABASE, SQLMERGXLib.SUBSCRIPTION_HOST.NONE)

        'Synchronize the subscription.
        Console.WriteLine("Starting synchronization...")
        oMerge.Initialize()
        oMerge.Run()
        oMerge.Terminate()
        Console.WriteLine("Synchronization completed.")

    End Sub

End Module

                

back to the top

REFERENCES

For a Microsoft Visual C# .NET version of this article, see 319646.

For samples that are written in earlier versions of Microsoft Visual Basic and Microsoft Visual C++, refer to the following SQL Server Books Online topics:

  • "Using SQL Merge and SQL Distribution Controls in a Custom Visual Basic Application"

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/replprog/rp_replsamp_7yr2.asp

  • "Using SQL Merge and SQL Distribution Controls in a Custom Visual C++ Application"

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/replprog/rp_replsamp_2pf2.asp

back to the top

Keywords: kbhowtomaster KB319647