如何:卸除 SQL Server Compact Edition 訂閱 (以程式設計的方式)
在此主題中,您將瞭解如何使用 SqlCeReplication 類別,在 Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) 資料庫上卸除訂閱。如需使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間的參考說明文件。
卸除 SQL Server Compact Edition 訂閱
初始化 SqlCeReplication 物件。
設定 SqlCeReplication 物件的下列屬性:
- SubscriberConnectionString
- Subscriber
- Publisher
- Publication
- PublisherDatabase
呼叫 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));