winforms 응용 프로그램 내 데이터베이스에서 삽입 이벤트를 캡처하기 위해 SQL Server 알림을 사용하고 싶습니다. SQLDependency 개체를 사용하려고합니다. MSDN 기사를 보면이 사실이 꽤 솔직하게 보입니다. 그래서 그것을 시험해보기 위해 약간의 예제 애플리케이션을 만들었습니다. 처음으로 응용 프로그램을 시작할 때만 이벤트가 발생하는 것 같습니다 (MessageBox가 나타납니다). 테이블에 데이터를 삽입해도 OnChange 이벤트가 발생하지는 않습니다. 누군가 내가 놓친 걸 말해 줄 수 있니? 감사!SQL Server 알림 - 내 OnChange가 실행되지 않습니다.
public Main()
{
InitializeComponent();
var check = EnoughPermission();
SqlDependency.Stop(constr);
SqlDependency.Start(constr);
if(connection == null)
{
connection = new SqlConnection(constr);
}
if(command == null)
{
command = new SqlCommand("Select ID, ChatMessage FROM dbo.Chat",connection);
}
connection.Open();
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
command.ExecuteReader();
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MessageBox.Show("Change!");
}
어디에서 데이터를 삽입하고 있습니까? SELECT 문은 INSERT가 아닙니다. –
쿼리 분석기에 데이터를 삽입하고 있습니다. INSERT에 대한 좋은 호출이 SELECT가 아니므로 :) Select는 데이터베이스에서보고자하는 데이터를 나타 내기 위해 필요합니다. – Nick
SqlDendency 개체에 SELECT를 건네주는 것에 주목하십시오. – Nick