如何:卸除訂閱 (以程式設計方式)
在此主題中,您將學習如何使用 SqlCeReplication 類別,在 Microsoft SQL Server Compact 3.5 資料庫上卸除訂閱。如需有關使用 SqlServerCe 命名空間的詳細資訊,請參閱 SqlServerCe 命名空間參考文件集。
SQL Server Compact 3.5 的程序
若要卸除訂閱
初始化 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));