SqlCommand 可以設定為當資料從伺服器中擷取之後發生變更時產生通知,而且如果再次執行查詢,則結果集會不同。 這適用於您想要在伺服器上使用自訂通知佇列,或當您不想維護即時物件時的案例。
建立通知請求
您可以使用 SqlNotificationRequest 物件,藉由將其繫結至 SqlCommand 物件來建立通知要求。 建立要求之後,您就不再需要 SqlNotificationRequest 物件。 您可以查詢佇列中的任何通知,並適當地回應。 即使關閉了應用程式且隨後重新啟動,也可能會發生通知。
在具有相關聯通知的命令執行時,對原始結果集所做的任何變更,都會傳送訊息至在通知要求中設定的 SQL Server 佇列。
輪詢 SQL Server 佇列及轉譯訊息的方式,視應用程式而定。 應用程式會負責輪詢佇列並根據訊息的內容進行回應。
注意
將 SqlDependency 與 SQL Server 通知請求搭配使用時,請建立您自己的佇列名稱,而不要使用預設服務名稱。
SqlNotificationRequest 沒有新的用戶端安全性元素。 這主要是伺服器功能,而且伺服器已建立特殊權限,使用者必須擁有該權限,才能要求通知。
範例
下列程式碼片段示範如何建立 SqlNotificationRequest,並將其與 SqlCommand 產生關聯。
// 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.