如何删除订阅(以编程方式)

本主题将介绍如何通过使用 SqlCeReplication 类来删除对 Microsoft SQL Server Compact 3.5 数据库的订阅。有关使用 SqlServerCe 命名空间的详细信息,请参阅 SqlServerCe 命名空间参考文档。

SQL Server Compact 3.5 的过程

删除订阅

  1. 初始化 SqlCeReplication 对象。

  2. 设置 SqlCeReplication 对象的下列属性:

    • SubscriberConnectionString

    • Subscriber

    • Publisher

    • Publication

    • PublisherDatabase

  3. 调用 DropSubscription 方法,传入 DropOption。使用 DropOption 可以保留或删除数据库。如果指定了 DropDatabase 的 DropOption 并且该数据库包含多个订阅,将不删除该数据库。

示例

此示例说明了如何删除名为 MyDatabase.sdf 的数据库上的订阅。删除订阅后,将删除数据库。

        SqlCeReplication repl = null;
        try 
    {
            // Set the Replication object properties.
            repl = new SqlCeReplication();
            repl.SubscriberConnectionString = 
  @"Data Source='ssce.sdf'";
            repl.Publisher = @"servername";
            repl.PublisherDatabase = @"SQLMobile";
            repl.Publication = @"SQLMobile";

            // Drop the subscription and delete the database.
            repl.DropSubscription(DropOption.DropDatabase);
        }
        catch(SqlCeException ex)
   {
            // Use your own error handling routine to show error information.
            // ShowError.ShowErrors(ex);
        }
        finally 
        {
            // Dispose of the Replication object.
            repl.Dispose();
        }
      Dim repl As SqlCeReplication = Nothing
      Try
         ' Set the Replication object properties.
         repl = New SqlCeReplication()
         repl.SubscriberConnectionString = "Data Source='ssce.sdf'"
         repl.Publisher = "servername"
         repl.PublisherDatabase = "SQLMobile"
         repl.Publication = "SQLMobile"

         ' Drop the subscription and delete the database.
         repl.DropSubscription(DropOption.DropDatabase)
      
      Catch ex As SqlCeException
      ' Use your own error handling routine to show error information.
      ' ShowErrors(ex)
      
      Finally
         ' Dispose of the Replication object.
         repl.Dispose()
      End Try
   ISSCEMerge      *pISSCEMerge = NULL;
   BSTR            bstr = NULL;

   /* Create the Replication object. */
   CoCreateInstance(CLSID_Replication, NULL, CLSCTX_INPROC_SERVER,
      IID_ISSCEMerge, (LPVOID *) &pISSCEMerge);
   
   /* Set Subscriber properties. */
   bstr = SysAllocString(L"data source=\\Ssce.sdf");
   pISSCEMerge->put_SubscriberConnectionString(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"SamplePublisher");
   pISSCEMerge->put_Publisher(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"AdventureWorks_SQLCE");
   pISSCEMerge->put_PublisherDatabase(bstr);
   SysFreeString(bstr);

   bstr = SysAllocString(L"SQLCEReplDemo");
   pISSCEMerge->put_Publication(bstr);
   SysFreeString(bstr);

   /* Drop the subscription and delete the database. */
   pISSCEMerge->DropSubscription(DROP_DATABASE));

请参阅

其他资源

使用合并复制

删除订阅