2014-09-04 6 views
4

이 예제를 고려 변경 감지 :이 이벤트를 인식하기 위해해야 ​​할 것이 무엇 나는 C# 형태의 응용 프로그램에 관해서, SSMS에서이 명령을 실행한다면SQL 데이터베이스

INSERT INTO [Table] (column1) 
SELECT value1 

? 이 이벤트가 발생할 때 MessageBox을 표시하는 응용 프로그램과 같은 간단한 것입니다. 난 그냥이 하나를 작동하거나 그것에 대한 유용한 데이터를 찾을 수 없습니다. 나는 SqlDependency을 사용하려고 시도했지만 행운이 없다. 그것이 내가 내려야 할 길인 경우 누군가가 개념을 조금 더 잘 이해할 수있게 도와 줄 수 있습니까?

+0

에 따라? – paqogomez

+0

@paqogomez 정답! 일종의 ssms에서 방아쇠처럼, 나는 C에서 방아쇠를 원한다 # – Volearix

+1

당신은 C# 응용 프로그램에서 데이터베이스에 무언가가 변화 할 때를 감지하고 싶습니까? SQL Server는 알림을 푸시하지 않으므로 무언가를 폴링해야합니다. 당신이 할 수있는 한가지는 필요하다면 감사 테이블에 로그를 남기고 INSERT TRIGGER를 추가하는 것입니다. – Zer0

답변

7

삽입을 변경하지 않고 변경 사항을 감지하려면 SQL Dependency을 사용하면됩니다. 링크에서 예제를 읽고 사용해 보셨습니까?

Heres a nice 'tutorial/example' that works and runs you through the basics.

Heres a nice overview of Query Notifications.

  • 낮은 수준의 구현은 통지 요구와 명령을 수행 할 수 있도록, 서버 측 기능을 노출하는 SqlNotificationRequest 클래스에서 제공되는

    .

  • 상위 수준 구현은 원본 응용 프로그램과 SQL Server간에 알림 기능의 상위 수준 추상화를 제공하는 클래스 인 SqlDependency 클래스에서 제공되므로 종속성을 사용하여 섬기는 사람. 대부분의 경우 SQL Server 용 .NET Framework 데이터 공급자를 사용하여 관리되는 클라이언트 응용 프로그램에서 SQL Server 알림 기능을 활용하는 가장 간단하고 효과적인 방법입니다.

  • 또한 ASP.NET 2.0 이상을 사용하여 작성된 웹 응용 프로그램은 SqlCacheDependency 도우미 클래스를 사용할 수 있습니다. "A SqlDependency 객체가 질의 결과 원래 검색 다를 때를 검출하기 위해 SqlCommand를 연결할 수 있습니다."라고

은 염기성이고

먼저해야 Enable Query Notifications 나는 당신이 당신의 SQL 서버가 삽입 선택 실행 때 C#을 응용 프로그램에서 이벤트를 트리거 할, 당신을 이해한다면 Creating a Query for Notification

void Initialization() 
{ 
    // Create a dependency connection. 
    SqlDependency.Start(connectionString, queueName); 
} 

void SomeMethod() 
{ 
    // Assume connection is an open SqlConnection. 
    // Create a new SqlCommand object which directly references (no synonyms) the data you want to check for changes. 
    using (SqlCommand command=new SqlCommand("SELECT value1 FROM [Table]", 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. 
     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); 
} 
+0

이렇게하면 삽입 된 행이 표시되지 않습니다. –