2016-09-30 22 views
0

私はデータベーステーブルの1つにモニタを置いておく必要があるプロジェクトに取り組んでいます。新しいレコードを挿入するには、クライアントで同じものを更新する必要があります。SqlDependency変更は1回の変更でのみ有効

このため、サービスブローカーの購読にはSqlDependencyを使用しています。また、すぐにSignalRを使用してクライアント側を更新します。

私のサブスクリプションは1回の変更に対してのみ有効です。アップデートが1回であれば、私のサブスクリプションは自動的に削除されます。ここで

は変更のためにデータベースを購読するコードは次のとおりです。

SqlDependency.Start(connectionString); 
SubScribeForAttendance(); 
SqlDependency.Stop(connectionString); 

サブスクリプション機能:

public void SubScribeForAttendance() 
{ 
    // We have selected the entire table as the command, so SQL Server executes this script and sees if there is a change in the result, raise the event 
    string commandText = @"Select bnr,knrhex From dbo.TableName where bnr > SomeId"; 

    // Start the SQL Dependency 
    SqlDependency.Start(connectionString); 

    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     using (SqlCommand command = new SqlCommand(commandText, connection)) 
     { 
      connection.Open(); 
      var sqlDependency = new SqlDependency(command); 
      sqlDependency.OnChange += Dependency_OnBookingChange; 

      // NOTE: You have to execute the command, or the notification will never fire. 
      using (command.ExecuteReader()) 
      { 
      } 
     } 
    } 
} 

答えて

2

は、SQLの依存関係を登録し、通知がトリガされるたびに再する必要があり、SQLの依存関係のみ1つの通知のために登録する。再するDependency_OnBookingChangeの最後で

コールSubScribeForAttendance()レジスタのSQL依存

私はこのlink

+0

だから、再から、このソリューションの唯一のオプションがあるサブスクライブを見つけましたか?複数の変更を登録することはできませんか? –

+0

いいえ、SQL Serverは1回だけ変更を登録し、一度トリガーするとフックを解放します。 –

+1

あなたの問題を解決した場合、私の答えに合格とマークしてください。ありがとう –

関連する問題