none
MSDN例程中的问题 RRS feed

  • 问题

  • 在以下例子中(http://msdn.microsoft.com/zh-CN/library/microsoft.sqlserver.replication.transsubscription(v=SQL.100).aspx):

    1  // Define the Publisher, publication, and databases.
    2  string publicationName = "AdvWorksProductTran";
    3  string publisherName = publisherInstance;
    4  string subscriberName = subscriberInstance;
    5  string subscriptionDbName = "AdventureWorksReplica";
    6  string publicationDbName = "AdventureWorks";
    7
    8  //Create a connection to the Publisher.
    9  ServerConnection conn = new ServerConnection(subscriberName);
    10
    11 // Create the objects that we need.
    12 TransPublication publication;
    13 TransSubscription subscription;
    14
    15 try
    16 {
    17     // Connect to the Publisher.
    18     conn.Connect();
    19
    20     // Ensure that the publication exists and that
    21    // it supports push subscriptions.
    22    publication = new TransPublication();
    23    publication.Name = publicationName;
    24    publication.DatabaseName = publicationDbName;
    25    publication.ConnectionContext = conn;
    26
    27    if (publication.IsExistingObject)
    28    {
    29        if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
    30        {
    31             publication.Attributes |= PublicationAttributes.AllowPush;
    32         }
    33
    34         // Define the push subscription.
    35         subscription = new TransSubscription();
    36         subscription.ConnectionContext = conn;
    37         subscription.SubscriberName = subscriberName;
    38         subscription.PublicationName = publicationName;
    39         subscription.DatabaseName = publicationDbName;
    40         subscription.SubscriptionDBName = subscriptionDbName;
    41
    42         // Specify the Windows login credentials for the Distribution Agent job.
    43         subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
    44         subscription.SynchronizationAgentProcessSecurity.Password = winPassword;
    45
    46         // By default, subscriptions to transactional publications are synchronized
    46         // continuously, but in this case we only want to synchronize on demand.
    48         subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.OnDemand;
    49 
    50         // Create the push subscription.
    51         subscription.Create();
    52     }
    53     else
    54     {
    55         // Do something here if the publication does not exist.
    56         throw new ApplicationException(String.Format(
    57             "The publication '{0}' does not exist on {1}.",
    58             publicationName, publisherName));
    59     }
    60 }
    61 catch (Exception ex)
    62 {
    63     // Implement the appropriate error handling here.
    64     throw new ApplicationException(String.Format(
    65         "The subscription to {0} could not be created.", publicationName), ex);
    66 }
    67 finally
    68 {
    69     conn.Disconnect();
    70 }
    71

    第9行定义了到数据库实例的链接,到底应该是连接到Publisher呢?还是应该连接到subscriber呢?因为后面既有对publication的判断(第27行),又有对subscription的定义(第36行),而这所用的是同一个conn,这不会有问题么?

    2010年9月8日 11:02

答案

  •  

     7
    8  //Create a connection to the Publisher.
    9  ServerConnection conn = new ServerConnection(subscriberName);

    29        if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
    30        {
    31             publication.Attributes |= PublicationAttributes.AllowPush;
    32         }

     

    看样子是建立推送订阅, 这种方式只需要在 publisher 上操作就可以了, 不用连接 subscriber.

    • 已标记为答案 老笨猪 2010年9月10日 17:48
    2010年9月10日 4:34

全部回复

  • Hi,

    >9.  ServerConnection conn = new ServerConnection(subscriberName);

    建立对Subscriber的一个数据库链接

    >36. subscription.ConnectionContext = conn;

    定义 TransSubscription 实例  subscription 的ConnectionContext属性为之前建立适用于 Subscriber 的数据库链接。

    之前27行是,判断在 Publication 发布者创建完成的前提下,来定义订阅者。

    Thanks.


    Microsoft Online Community Support Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    2010年9月10日 2:28
    版主
  •  

     7
    8  //Create a connection to the Publisher.
    9  ServerConnection conn = new ServerConnection(subscriberName);

    29        if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
    30        {
    31             publication.Attributes |= PublicationAttributes.AllowPush;
    32         }

     

    看样子是建立推送订阅, 这种方式只需要在 publisher 上操作就可以了, 不用连接 subscriber.

    • 已标记为答案 老笨猪 2010年9月10日 17:48
    2010年9月10日 4:34