共用方式為


如何:重新初始畫訂閱 (以程式設計方式)

在此主題中,您將學習如何使用 SqlCeReplication 類別,在 Microsoft SQL Server Compact 3.5 上重新初始化訂閱。如需有關使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間參考文件集。

若要重新初始化訂閱

  1. 初始化 SqlCeReplication 物件。

    SqlCeReplication repl = new SqlCeReplication();
    
  2. 設定連接屬性。這些屬性指定您要訂閱的發行集名稱及位置、本機 SQL Server Compact 3.5 資料庫的名稱及位置,以及 SQL Server Compact 3.5 伺服器代理程式的位置。

    repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa35.dll";
    repl.InternetLogin = "MyInternetLogin";
    repl.InternetPassword = "<password>";
    repl.Publisher = "MyPublisher";
    repl.PublisherDatabase = "MyPublisherDatabase";
    repl.PublisherLogin = "MyPublisherLogin";
    repl.PublisherPassword = "<password>";
    repl.Publication = "MyPublication";
    repl.Subscriber = "MySubscriber";
    repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";
    
  3. 呼叫 ReinitializeSubscription 方法來標記重新初始化的訂閱。如果傳遞 true 給 ReinitializeSubscription 方法,訂閱者的變更會在重新初始化開始前上傳到發行者。若是傳遞 false,會在重新初始化期間捨棄訂閱者上的任何變更。

    repl.ReinitializeSubscription(true);
    
  4. 若要重新擴展訂閱,必須在呼叫 ReinitializeSubscription 方法後呼叫 Synchronize 方法。

    repl.Synchronize();
    

範例

此範例顯示如何重新初始化 SQL Server Compact 3.5 訂閱。在此範例中,訂閱者的任何變更會先上傳到發行者,接著重新初始化訂閱,然後進行資料同步處理。

SqlCeReplication repl = null;

try
{
   // Create SqlCeReplication instance
   //
   repl = new SqlCeReplication();
   repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa35.dll";
   repl.InternetLogin = "MyInternetLogin";
   repl.InternetPassword = "<password>";
   repl.Publisher = "MyPublisher";
   repl.PublisherDatabase = "MyPublisherDatabase";
   repl.PublisherLogin = "MyPublisherLogin";
   repl.PublisherPassword = "<password>";
   repl.Publication = "MyPublication";
   repl.Subscriber = "MySubscriber";
   repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf";

   // Mark the subscription for reinitialization with Upload first
  
   repl.ReinitializeSubscription(true);

   // Synchronize to SQL Server to populate the Subscription 

   repl.Synchronize();
}
catch (SqlCeException)
{
   // Handle errors here
}
finally
{
   // Dispose the repl object
   
   repl.Dispose();
}
Dim repl As SqlCeReplication = Nothing

Try
   ' Create SqlCeReplication instance

   repl = New SqlCeReplication()
   repl.InternetUrl = "https://www.adventure-works.com/sqlmobile/sqlcesa35.dll"
   repl.InternetLogin = "MyInternetLogin"
   repl.InternetPassword = "<password>"
   repl.Publisher = "MyPublisher"
   repl.PublisherDatabase = "MyPublisherDatabase"
   repl.PublisherLogin = "MyPublisherLogin"
   repl.PublisherPassword = "<password>"
   repl.Publication = "MyPublication"
   repl.Subscriber = "MySubscriber"
   repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"

   ' Mark the subscription for reinitialization with Upload first
            
   repl.ReinitializeSubscription(True)

   ' Synchronize to SQL Server to populate the Subscription 

   repl.Synchronize()
Catch
   ' Handle errors here
Finally
   ' Dispose the repl object
   
   repl.Dispose()
End Try

請參閱

其他資源

使用合併式複寫

訂閱發行集 (SQL Server Compact)

重新初始化訂閱 (SQL Server Compact)