0

누군가가 데이터베이스의 데이터를 업데이트 한 경우 내 앱에 알려줄 것이 필요합니다. 오해가 없다면 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 단계에서만 중단됩니다.

내가 실수를 했습니까? 내 목표에 어떻게 도달 할 수 있습니까?

답변

4

있지만 쿼리 알림 구독이 유효하지 않기 때문에 그것은 화재 초기화 단계

에 발생합니다. SqlNotificationEventArgs 회원을 검사해야합니다.Change 유형 만 변경 알림이므로 Statement 소스와 함께 Subscribe 유형이 표시됩니다. 귀하의 질의는 C reating a Query for Notification에 설명 된 기준을 충족하지 않습니다

  • 문은 열을 지정하려면 별표 (*) 또는 table_name.* 구문을 사용할 수 없습니다.
+0

나는 링크를 이용해 주셔서 감사합니다. 알림 유형을 올바르게 확인하려면 어떻게해야합니까? OnDependancyChange 메서드를 편집했습니다. –

+0

작동하는 것처럼 보이지만 ... 선택한 열의 처음 두 변경 사항 만 ... 왜? –

+0

어쩌면 속도의 주파수를 확인하기위한 타이머 같은 것이 있습니까? 그리고 어쩌면 내가 너무 빨리 삽입하고 있습니까? –