拉订阅(RMO编程)

时间:2016-06-25 07:35:04

标签: c# sql-server rmo

我创建了事务发布,并尝试为此发布创建订阅。在MS Studio中创建订阅工作正常,但是当我使用RMO时,我的订阅基础中的表永远不会出现。

  1. 我的"第一次同步初始化"有什么问题?

  2. 如何设置"立即初始化"?

  3. 我的代码:

    public static void CreateSub(string publicationName, string publisherName, string subscriberName, string subscriptionDbName, string publicationDbName)
    {
        ServerConnection subscriberConn = new ServerConnection(subscriberName);
        ServerConnection publisherConn = new ServerConnection(publisherName);
        TransPublication publication;
        TransPullSubscription subscription;
    
        try
        {
            subscriberConn.Connect();
            publisherConn.Connect();
    
            publication = new TransPublication();
            publication.Name = publicationName;
            publication.DatabaseName = publicationDbName;
            publication.ConnectionContext = publisherConn;
    
            if (publication.IsExistingObject)
            {
                if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)
                {
                    publication.Attributes |= PublicationAttributes.AllowPull;
                }
                publication.Attributes |= PublicationAttributes.ImmediateSync;
                subscription = new TransPullSubscription();
                subscription.ConnectionContext = subscriberConn;
                subscription.PublisherName = publisherName;
                subscription.PublicationName = publicationName;
                subscription.PublicationDBName = publicationDbName;
                subscription.DatabaseName = subscriptionDbName;
                subscription.SynchronizationAgentProcessSecurity.Login = @"*****";
                subscription.SynchronizationAgentProcessSecurity.Password = "****";
    
                subscription.CreateSyncAgentByDefault = true;
    
                subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Continuously;
                subscription.Create();
    
    
    
                Boolean registered = false;
    
                foreach (TransSubscription existing in publication.EnumSubscriptions())
                {
                    if (existing.SubscriberName == subscriberName&& existing.SubscriptionDBName == subscriptionDbName)
                    {
                        registered = true;
                    }
                }
                if (!registered)
                {
                    publication.MakePullSubscriptionWellKnown(
                        subscriberName, subscriptionDbName,
                        SubscriptionSyncType.Automatic,
                        TransSubscriberType.ReadOnly);
                }
                if (subscription.LoadProperties())
                {
                    if (subscription.PublisherSecurity != null)
                    {
                        subscription.SynchronizationAgent.Synchronize();// class is not registered error
                    }
                    else
                    {
                        throw new ApplicationException("There is insufficent metadata to " +
                            "synchronize the subscription. Recreate the subscription with " +
                            "the agent job or supply the required agent properties at run time.");
                    }
                }
            }
            else
            {
                throw new ApplicationException(String.Format(
                    "The publication '{0}' does not exist on {1}.",
                    publicationName, publisherName));
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException(string.Format(
                "the subscription to {0} could not be created.", publisherName), ex);
    
        }
        finally
        {
            subscriberConn.Disconnect();
            publisherConn.Disconnect();
        }
    }
    

0 个答案:

没有答案