누군가가 데이터베이스의 데이터를 업데이트 한 경우 내 앱에 알려줄 것이 필요합니다. 오해가 없다면 SqlDependancy가 필요합니다. 나는 this tutorial을 따라이 코드를 작성했습니다 :초기화 SqlDependency 및 변경 알림
class dbListener
{
public dbListener()
{
Debug.WriteLine(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=12345;");
SqlDependency.Start(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=12345;");
connection = new SqlConnection(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=12345;");
connection.Open();
SomeMethod();
}
SqlConnection connection;
void SomeMethod()
{
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
//{
using (SqlCommand command = new SqlCommand("SELECT * FROM dbo.ArchivioErogazioni", connection))
{
// Create a dependency and associate it with the SqlCommand.
SqlDependency dependency = new SqlDependency(command);
// Maintain the refence in a class member.
// Subscribe to the SqlDependency event.
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
// Execute the command.
command.ExecuteReader();
// }
}
}
// Handler method
void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
// Handle the event (for example, invalidate this cache entry).
MessageBox.Show("ikjkjkj");
Debug.WriteLine("fkldjkfjklgjf");
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= OnDependencyChange;
SomeMethod();
}
}
void Termination()
{
// Release the dependency.
SqlDependency.Stop(MainWindow.GetConnectionString("Model"));
}
}
그러나 작동하지 않습니다. 즉, 오류없이 실행되지만 SQL Server 2008 Management Studio에서 일부 값을 업데이트하여 테스트하려고하면 아무 일도 일어나지 않습니다. 이벤트를 관리 할 함수에 중단 점을 넣었지만 init 단계에서만 중단됩니다.
내가 실수를 했습니까? 내 목표에 어떻게 도달 할 수 있습니까?
나는 링크를 이용해 주셔서 감사합니다. 알림 유형을 올바르게 확인하려면 어떻게해야합니까? OnDependancyChange 메서드를 편집했습니다. –
작동하는 것처럼 보이지만 ... 선택한 열의 처음 두 변경 사항 만 ... 왜? –
어쩌면 속도의 주파수를 확인하기위한 타이머 같은 것이 있습니까? 그리고 어쩌면 내가 너무 빨리 삽입하고 있습니까? –