使用 SqlDependency 偵測變更

您可以將 SqlDependency 物件與 SqlCommand 產生關聯,以便偵測查詢結果與原始擷取結果不同的時間。 您也可以將委派指派給 OnChange 事件,這會在相關聯命令的結果變更時引發。 您必須先將 SqlDependency 與此命令產生關聯,然後再執行此命令。 SqlDependencyHasChanges 屬性也可以用來判斷自第一次擷取資料之後,查詢結果是否已經變更。

安全性考量

相依性基礎結構會仰賴呼叫 SqlConnection 時所開啟的 Start,以便接收指定命令之基礎資料已經變更的通知。 讓用戶端啟始呼叫 SqlDependency.Start 的功能是透過使用 SqlClientPermission 和程式碼存取安全性屬性來控制的。 如需詳細資訊,請參閱啟用查詢通知程式碼存取安全性和 ADO.NET

範例

下列步驟說明如何宣告相依性、執行命令,並在結果集變更時收到通知:

  1. 起始與伺服器的 SqlDependency 連線。

  2. 建立 SqlConnectionSqlCommand 物件,以連接到伺服器並定義 Transact-SQL 陳述式。

  3. 建立新 SqlDependency 物件或使用現有物件,並將其繫結至 SqlCommand 物件。 如此便可在內部建立 SqlNotificationRequest 物件,並視需要將其繫結至命令物件。 此通知要求包含可唯一識別此 SqlDependency 物件的內部識別碼。 此外,它也會啟動用戶端接聽程式 (Listener) (如果尚未作用中的話)。

  4. 訂閱 SqlDependency 物件之 OnChange 事件的事件處理常式。

  5. 使用 SqlCommand 物件的任何 Execute 方法來執行命令。 因為命令已繫結至通知物件,所以伺服器了解必須產生通知,而且佇列資訊將指向相依性佇列。

  6. 停止對伺服器的 SqlDependency 連線。

如果任何使用者隨後變更基礎資料,Microsoft SQL Server 就會偵測到針對此類變更暫止的通知存在,並發佈通知,而該通知可透過呼叫 SqlConnection 所建立的基礎 SqlDependency.Start 來進行處理並轉送給用戶端。 用戶端接聽程式會接收到失效訊息。 然後,用戶端接聽程式會找出相關聯的 SqlDependency 物件,並引發 OnChange 事件。

下列程式碼片段顯示您會用來建立範例應用程式的設計模式。

Sub Initialization()
    ' Create a dependency connection.
    SqlDependency.Start(connectionString, queueName)
End Sub

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

        ' Create a dependency and associate it with the SqlCommand.
        Dim dependency As New SqlDependency(command)
        ' Maintain the reference in a class member.
        ' Subscribe to the SqlDependency event.
        AddHandler dependency.OnChange, AddressOf OnDependencyChange

        ' Execute the command.
        Using reader = command.ExecuteReader()
            ' Process the DataReader.
        End Using
    End Using
End Sub

' Handler method
Sub OnDependencyChange(ByVal sender As Object, _
    ByVal e As SqlNotificationEventArgs)
    ' Handle the event (for example, invalidate this cache entry).
End Sub

Sub Termination()
    ' Release the dependency
    SqlDependency.Stop(connectionString, queueName)
End Sub
void Initialization()
{
    // Create a dependency connection.
    SqlDependency.Start(connectionString, queueName);
}

void SomeMethod()
{
    // Assume connection is an open SqlConnection.

    // Create a new SqlCommand object.
    using (SqlCommand command=new SqlCommand(
        "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers",
        connection))
    {

        // Create a dependency and associate it with the SqlCommand.
        SqlDependency dependency=new SqlDependency(command);
        // Maintain the reference in a class member.

        // Subscribe to the SqlDependency event.
        dependency.OnChange+=new
           OnChangeEventHandler(OnDependencyChange);

        // Execute the command.
        using (SqlDataReader reader = command.ExecuteReader())
        {
            // Process the DataReader.
        }
    }
}

// Handler method
void OnDependencyChange(object sender,
   SqlNotificationEventArgs e )
{
  // Handle the event (for example, invalidate this cache entry).
}

void Termination()
{
    // Release the dependency.
    SqlDependency.Stop(connectionString, queueName);
}

另請參閱