Aracılığıyla paylaş


Bir SqlNotificationRequest ile SqlCommand Yürütme

, SqlCommand veriler sunucudan getirildikten sonra değiştiğinde bildirim oluşturacak şekilde yapılandırılabilir ve sorgu yeniden yürütülürse sonuç kümesi farklı olur. Bu, sunucuda özel bildirim kuyruklarını kullanmak istediğiniz veya canlı nesneleri korumak istemediğiniz senaryolar için kullanışlıdır.

Bildirim İsteği Oluşturma

Bildirim isteğini bir SqlNotificationRequest nesneye bağlayarak oluşturmak için bir SqlCommand nesnesi kullanabilirsiniz. İstek oluşturulduktan sonra artık nesnesine SqlNotificationRequest ihtiyacınız olmaz. Herhangi bir bildirim için kuyruğu sorgulayabilir ve uygun şekilde yanıtlayabilirsiniz. Uygulama kapatılıp daha sonra yeniden başlatılsa bile bildirimler oluşabilir.

İlişkili bildirim içeren komut yürütürken, özgün sonuç kümesinde yapılan tüm değişiklikler, bildirim isteğinde yapılandırılan SQL Server kuyruğuna bir ileti gönderilmesini tetikler.

SQL Server kuyruğunun yoklaması ve iletiyi yorumlama yöntemi, uygulamanıza özgüdür. Uygulama kuyruğu yoklamaktan ve iletinin içeriğine göre tepki göstermeden sorumludur.

Not

ile SqlDependencySQL Server bildirim isteklerini kullanırken, varsayılan hizmet adını kullanmak yerine kendi kuyruk adınızı oluşturun.

için SqlNotificationRequestyeni istemci tarafı güvenlik öğesi yok. Bu öncelikli olarak bir sunucu özelliğidir ve sunucu, kullanıcıların bildirim istemesi için sahip olması gereken özel ayrıcalıklar oluşturmuştur.

Örnek

Aşağıdaki kod parçası, oluşturma SqlNotificationRequest ve ile SqlCommandilişkilendirmeyi gösterir.

' Assume connection is an open SqlConnection.
' Create a new SqlCommand object.
Dim command As New SqlCommand( _
  "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", connection)

' Create a SqlNotificationRequest object.
Dim notificationRequest As New SqlNotificationRequest()
notificationRequest.id = "NotificationID"
notificationRequest.Service = "mySSBQueue"

' Associate the notification request with the command.
command.Notification = notificationRequest
' Execute the command.
command.ExecuteReader()
' Process the DataReader.
' You can use Transact-SQL syntax to periodically poll the
' SQL Server queue to see if you have a new message.
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
SqlCommand command=new SqlCommand(
 "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", connection);

// Create a SqlNotificationRequest object.
SqlNotificationRequest notificationRequest=new SqlNotificationRequest();
notificationRequest.id="NotificationID";
notificationRequest.Service="mySSBQueue";

// Associate the notification request with the command.
command.Notification=notificationRequest;
// Execute the command.
command.ExecuteReader();
// Process the DataReader.
// You can use Transact-SQL syntax to periodically poll the
// SQL Server queue to see if you have a new message.

Ayrıca bkz.