SQL Server 데이터베이스의 테이블에서 뭔가 변경되면 내 응용 프로그램에 알림을 보내기위한 자습서를 따라 왔습니다. 이것은 내 리스너 클래스입니다.SqlDependency가 이벤트를 발생시키지 않거나 트리거하지 않습니다.
class dbListener
{
public dbListener()
{
Debug.WriteLine(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
SqlDependency.Stop(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
SqlDependency.Start(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
connection = new SqlConnection(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
connection.Open();
SomeMethod();
}
SqlConnection connection;
void SomeMethod()
{
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
SqlCommand command = new SqlCommand("SELECT CODVEI FROM dbo.ArchivioErogazioni", connection);
// Create a dependency and associate it with the SqlCommand.
command.Notification = null; // ---> DO I NEED IT??
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)
{
// Handle the event (for example, invalidate this cache entry).
MessageBox.Show("ikjkjkj");
Debug.WriteLine("fkldjkfjklgjf");
SomeMethod();
}
void Termination()
{
// Release the dependency.
SqlDependency.Stop(MainWindow.GetConnectionString("Model"));
}
}
이벤트를 올바르게 발생시키지 않습니다. 응용 프로그램의 시작 부분에 메시지 상자가 표시됩니다 (이벤트 관리에서 테스트 함). 한 두 번이나, 이유를 모르겠습니다. 그런 다음 SQL Server Management Studio에서 데이터베이스의 값을 편집 할 때 메시지 상자가 표시되거나 0 또는 1 또는 2 번 표시되면 다시는 실행되지 않습니다. 내 데이터베이스에서
, 나는이 스크립트 실행이 : 나는 분명히 큰 실수 ..... 하나를 수행 한
USE master ;
GO
ALTER DATABASE IN4MATICSystem_Pie SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
GO
ALTER AUTHORIZATION ON DATABASE::IN4MATICSystem_Pie to sa;
을 ??
UPDATE :
class dbListener
{
public dbListener()
{
Debug.WriteLine(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
SqlDependency.Stop(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
SqlDependency.Start(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
connection = new SqlConnection(MainWindow.dbContext.Database.Connection.ConnectionString + "Password=xxx;");
connection.Open();
SomeMethod();
}
SqlConnection connection;
SqlCommand command;
void SomeMethod()
{
// Assume connection is an open SqlConnection.
// Create a new SqlCommand object.
if (command == null)
{
command = new SqlCommand("SELECT * FROM dbo.ArchivioErogazioni", connection);
// Create a dependency and associate it with the SqlCommand.
}
else
{
command.Notification = null; // this cancels any previous notifcation object
}
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;
//dependency.OnChange -= OnDependencyChange;
SomeMethod();
}
}
void Termination()
{
// Release the dependency.
SqlDependency.Stop(MainWindow.GetConnectionString("Model"));
connection.Close();
}
}
어딘가에 읽어야하는데, 그렇지 않으면 이벤트가 한 번만 실행됩니다. 잘못 되었나요? –
예, 청취자의 등록을 취소 한 적이 없습니다 ... 내 편집을보세요 –
복사했는데 시작시 메시지 상자를 3 번 보여줍니다 ... 그러면 관련 테이블을 편집합니다. SQL 서버 관리,하지만 아무 일도 .. 나는 또한 te 클래스의 속성과 같은 밖에서 SqlDependency 의존성의 선언을 이동하고 초기화 방법과 OnDependencyChange에서 사용하려고했지만 여전히 동일합니다. –