0

I am trying to create a Merge Replication using RMO Programming which i got from here!

        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();
        }

but at this line

publicationDb.EnabledTransPublishing = true;

i am getting error -" An exception occurred while executing a Transact-SQL statement or batch."

So please help me out from this problem ..

waiting for your answers..

4

1 回答 1

0

您现在可能已经回答了,但对于那些可能会问同样问题的人。这是因为您使用的是 SQL Server 的 Express 版本,并且无法在任何版本的 SQl Server Express 中创建发布者/分发者。

您在代码中拥有的实例不是有效的实例,因此会引发异常。

看一眼:

http://msdn.microsoft.com/en-us/library/ms151819(v=sql.105).aspx

和那些说

Microsoft SQL Server 2008 Express 可以作为所有类型复制的订阅者,提供一种方便的方式将数据分发到使用此版本 SQL Server 的客户端应用程序。在复制拓扑中使用 SQL Server Express 时,请牢记以下注意事项: SQL Server Express 不能用作发布者或分发者。但是,合并复制允许在发布服务器和订阅服务器之间双向复制更改。块引用

于 2013-08-06T11:00:30.973 回答