刪除訂閱
Subscription 類別的 Delete 方法會刪除應用程式資料庫中現有的訂閱記錄。下列範例顯示如何利用 Managed 程式碼來刪除訂閱,利用 Microsoft Visual Basic Scripting Edition (VBScript) 來說明 COM Interop。
Managed 程式碼範例
下列範例顯示如何利用 SubscriptionEnumeration 物件和 GetFieldValue 方法來尋找特定訂閱,之後,再刪除訂閱。這個範例會刪除 City 值等於 Edmonds 的所有訂閱者的訂閱。
如果您在開發使用者介面,您可以利用 SubscriptionEnumeration 類別來列出訂閱者的所有訂閱,之後,再讓訂閱者選取和刪除訂閱。
// Create the NSInstance object.
NSInstance testInstance = new NSInstance("Tutorial");
// Create the NSApplication object.
NSApplication testApplication =
new NSApplication(testInstance, "Weather");
// Create the Subscription object.
Subscription testSubscription =
new Subscription(testApplication, "WeatherCity");
// Create a SubscriptionEnumeration object.
// that contains all of the subscriber's subscriptions
// in a specific subscription class
SubscriptionEnumeration testSubscriptionEnumeration =
new SubscriptionEnumeration(testApplication,
"WeatherCity", "TestUser1");
// Iterate through the subscriptions, finding the subscription
// that you want to delete.
foreach (Subscription subscription in testSubscriptionEnumeration)
{
String city = subscription.GetFieldValue("City").ToString();
if (city.Equals("Edmonds"))
testSubscription.SubscriptionId = subscription.SubscriptionId;
}
// Delete the subscription
testSubscription.Delete();
COM Interop 範例
下列 VBScript 程式碼範例顯示如何利用 COM Interop 來刪除訂閱。當使用 COM Interop 時,您必須先建立再初始化物件。程式碼的其餘部份類似於上述 Managed 程式碼範例。
Dim testInstance, testApplication, testSubscriptionEnumeration
const instanceName = "Tutorial"
const applicationName = "Weather"
const subscriptionClassName = "WeatherCity"
' Create the NSInstance object.
set testInstance = WScript.CreateObject( _
"Microsoft.SqlServer.NotificationServices.NSInstance")
testInstance.Initialize instanceName
' Create the NSApplication object.
set testApplication = WScript.CreateObject( _
"Microsoft.SqlServer.NotificationServices.NSApplication")
testApplication.Initialize (testInstance), applicationName
' Create the SubscriptionEnumeration object.
' This constructor returns the subscriptions for
' the specified application, subscription class, and subscriber.
set testSubscriptionEnumeration = WScript.CreateObject( _
"Microsoft.SqlServer.NotificationServices.SubscriptionEnumeration")
testSubscriptionEnumeration.Initialize (testApplication), _
subscriptionClassName, "TestUser1"
' Step through the subscriptions, locate the one
' that should be removed, and delete it.
for each thisSubscription in testSubscriptionEnumeration
if thisSubscription.GetFieldValue("City") = "Orlando" then
thisSubscription.Delete
end if
next
wscript.echo "Subscriber deleted."
請參閱
概念
建立訂閱物件
新增訂閱
更新訂閱
取得訂閱欄位資訊
擴展訂閱者地區設定程式碼
擴展時區清單