代码之家  ›  专栏  ›  技术社区  ›  Craig Shearer

使用RMO的SQL Server Express 2005合并复制导致空引用异常

  •  1
  • Craig Shearer  · 技术社区  · 16 年前

    我正在尝试使用rmo以编程方式执行合并同步。我基本上复制了sql server示例代码,如下所示:

    // Create a connection to the Subscriber.
    ServerConnection conn = new ServerConnection(subscriberName);
    
    MergePullSubscription subscription;
    
    try
    {
        // Connect to the Subscriber.
        conn.Connect();
    
        // Define the pull subscription.
        subscription = new MergePullSubscription(subscriptionDbName, publisherName, publicationDbName,
                                                 publicationName, conn, false);
    
        // If the pull subscription exists, then start the synchronization.
        if (subscription.LoadProperties())
        {
            // Check that we have enough metadata to start the agent.
            if (subscription.PublisherSecurity != null || subscription.DistributorSecurity != null)
            {
                subscription.SynchronizationAgent.Synchronize();
            }
            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
        {
            // Do something here if the pull subscription does not exist.
            throw new ApplicationException(String.Format(
                "A subscription to '{0}' does not exist on {1}",
                publicationName, subscriberName));
        }
    }
    catch (Exception ex)
    {
        // Implement appropriate error handling here.
        throw new ApplicationException("The subscription could not be " +
            "synchronized. Verify that the subscription has " +
            "been defined correctly.", ex);
    }
    finally
    {
        conn.Disconnect();
    }
    

    我已正确定义了服务器合并发布,但运行上述代码时,在调用以下对象时出现空引用异常:

    subscription.SynchronizationAgent.Synchronize();
    

    堆栈跟踪如下:

    at Microsoft.SqlServer.Replication.MergeSynchronizationAgent.StatusEventSinkMethod(String message, Int32 percent, Int32* returnValue)
    
    at Test.ConsoleTest.Program.SynchronizePullSubscription() in F:\Visual Studio Projects\Test\source\Test.ConsoleTest\Program.cs:line 124
    

    从堆栈跟踪看来,这似乎与status事件有关,但我没有定义处理程序,定义一个处理程序也没有区别。

    1 回复  |  直到 16 年前
        1
  •  0
  •   Craig Shearer    16 年前

    我真的不明白为什么会发生这种事。我可以让它在一台机器上工作,但在另一台机器上,我经常遇到nullreferenceexception。

    原来我引用的是sqlserver2005sdk文件夹中的程序集。我删除了这些并引用了sql server 2008中的那些,从那以后它工作得很好。

    也许这能帮助别人。

    推荐文章