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())
{
}
}
}
}
だから、再から、このソリューションの唯一のオプションがあるサブスクライブを見つけましたか?複数の変更を登録することはできませんか? –
いいえ、SQL Serverは1回だけ変更を登録し、一度トリガーするとフックを解放します。 –
あなたの問題を解決した場合、私の答えに合格とマークしてください。ありがとう –