共用方式為


使用 SqlDependency 偵測變更

下載 ADO.NET

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

安全性考量

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

範例

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

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

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

  3. 建立新 SqlDependency 物件或使用現有物件,並將其繫結至 SqlCommand 物件。 就內部而言,這個關聯會建立一個 SqlNotificationRequest 物件,並視需要將其繫結至命令物件。 此通知要求包含可唯一識別此 SqlDependency 物件的內部識別碼。 如果用戶端接聽程式尚未啟用,也會加以啟用。

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

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

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

如果有任何使用者隨後變更基礎資料,Microsoft SQL Server 會偵測到有此類變更的待處理通知,並透過藉由呼叫 SqlDependency.Start 所建立的基礎 SqlConnection,張貼已處理並轉送到用戶端的通知。 用戶端接聽程式會接收到失效訊息。 然後,用戶端接聽程式會找出相關聯的 SqlDependency 物件,並引發 OnChange 事件。

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

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);
}

下一步