다음을 통해 공유


SqlNotificationRequest를 사용하여 SqlCommand 실행

서버에서 가져온 후 데이터가 변경되고 쿼리가 다시 실행된 경우 결과 집합이 다를 때 알림을 생성하도록 A SqlCommand 를 구성할 수 있습니다. 이는 서버에서 사용자 지정 알림 큐를 사용하거나 라이브 개체를 유지 관리하지 않으려는 시나리오에 유용합니다.

알림 요청 만들기

SqlNotificationRequest 개체를 SqlCommand 개체에 바인딩하여 알림 요청을 만들 수 있습니다. 요청이 만들어지면 더 이상 개체가 SqlNotificationRequest 필요하지 않습니다. 큐에서 알림을 조회하고 적절하게 대응할 수 있습니다. 애플리케이션이 종료되고 이후에 다시 시작되는 경우에도 알림이 발생할 수 있습니다.

연결된 알림이 있는 명령이 실행되면 원래 결과 집합에 대한 변경 내용이 알림 요청에서 구성된 SQL Server 큐에 메시지를 보내는 트리거입니다.

SQL Server 큐를 폴링하고 메시지를 해석하는 방법은 애플리케이션에 따라 다릅니다. 애플리케이션은 큐를 폴링하고 메시지 내용에 따라 반응합니다.

비고

SQL Server 알림 요청을 SqlDependency사용하는 경우 기본 서비스 이름을 사용하는 대신 고유한 큐 이름을 만듭니다.

SqlNotificationRequest에는 새로운 클라이언트 측 보안 요소가 없습니다. 이는 주로 서버 기능이며, 서버는 사용자가 알림을 요청해야 하는 특별한 권한을 만들었습니다.

예시

다음 코드 조각은 SqlNotificationRequest를 생성하고 이를 SqlCommand와 연결하는 방법을 보여줍니다.

' 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.

참고하십시오