SqlDependency travaille avec un mais pas avec l'autre table

J'ai écrit cette class pour automatiser SQLDependencys

public class DatabaseChangeAlert { private Ssortingng connectionSsortingng; private SqlConnection sc; public event EventHandler<DatabaseChangedEvent> DatabaseChangeEvent; private Ssortingng tabelle; private Ssortingng query; public DatabaseChangeAlert(Ssortingng tabelle, Ssortingng conSsortingng) { try { this.tabelle = tabelle; this.sc = new SqlConnection(conSsortingng); ;//new SqlConnection(GlobalResources.ConnectionSsortingng); this.connectionSsortingng = conSsortingng; Ssortingng sel = ""; using (SqlConnection con = new SqlConnection(conSsortingng)) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT dbo.syscolumns.name AS Spaltenname " + " FROM dbo.syscolumns INNER JOIN " + " dbo.sysobjects ON dbo.syscolumns.id = dbo.sysobjects.id " + " WHERE (dbo.sysobjects.xtype = 'U') and dbo.sysobjects.name='" + tabelle + "' "; using (SqlDataReader sr = cmd.ExecuteReader()) { while (sr.Read()) { sel += "["+sr.GetSsortingng(0)+"],"; } } sel = sel.Subssortingng(0, sel.Length - 1); } query = "SELECT " + sel + " from [dbo].[" + tabelle+"]"; } catch (Exception ex) { } } ~DatabaseChangeAlert() { if(connectionSsortingng!=null) SqlDependency.Stop(connectionSsortingng); } public void start() { try { SqlDependency.Start(connectionSsortingng); } catch (Exception ex) { // log(ex) } try { startDependency(); } catch (Exception ex) { // log(ex) } } private void startDependency() { // Assume connection is an open SqlConnection. // Create a new SqlCommand object. using (SqlCommand command = new SqlCommand(query, sc)) { sc.Open(); // 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(dependency_OnChange); // Execute the command. using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { } } sc.Close(); } } void dependency_OnChange(object sender, SqlNotificationEventArgs e) { if (e.Info != SqlNotificationInfo.Invalid) { startDependency(); if (DatabaseChangeEvent != null) { DatabaseChangeEvent(this, new DatabaseChangedEvent(tabelle,e.Info)); } } } public class DatabaseChangedEvent : EventArgs { public readonly Ssortingng Tablename; public readonly SqlNotificationInfo info; public DatabaseChangedEvent(Ssortingng tablename,SqlNotificationInfo info) { this.Tablename = tablename; this.info = info; } } } 

dans mon programme j'utilise cette class comme ça:

  DatabaseChangeAlert alerttz = new DatabaseChangeAlert("table1", GlobalResources.ConnectionSsortingng); alerttz.DatabaseChangeEvent += (e,d)=>{ MessageBox.Show("table1 changed"); }; alerttz.start(); DatabaseChangeAlert alert = new DatabaseChangeAlert("table2", GlobalResources.ConnectionSsortingng); alert.DatabaseChangeEvent += (e, d) => { MessageBox.Show("table2 changed"); }; alert.start(); 

Maintenant mon problème est que quand je change n'importe quoi dans la table 2 je n'obtiens pas une notification, mais quand je fais la même chose pour la table 1 j'obtiens une notification!

des idées quel pourrait être le problème? J'ai aussi essayé de ne m'inscrire que pour table2 … mais ça rest le même.

J'ai répété votre situation et j'ai écrit un test unitaire (TwoTablesNotificationTest), mais je n'ai rien trouvé. Ça fonctionne bien pour moi. Dans cette situation, vous pouvez cesser de recevoir des notifications de SqlDependency en cas d'appel de destructeur de certaines entités DatabaseChangeAlert , car il possède l' SqlDependency.Stop(connectionSsortingng) . Ainsi, vous devez appeler SqlDependency.Start après chaque SqlDependency.Stop pour continuer à recevoir des events, mais destructor est appelé automatiquement par le garbage collector à partir du thread parallèle et cela peut provoquer cet effet.

Et je tiens à vous avertir – soyez prudent en utilisant la class SqlDependency car elle a un problème de fuite de memory . Pour mon projet, j'ai utilisé la réalisation open source de SqlDependency – SqlDependencyEx . C'est très facile à utiliser:

 int changesReceived = 0; using (SqlDependencyEx sqlDependency = new SqlDependencyEx( TEST_CONNECTION_STRING, TEST_DATABASE_NAME, TEST_TABLE_NAME)) { sqlDependency.TableChanged += (o, e) => changesReceived++; sqlDependency.Start(); // Make table changes. MakeTableInsertDeleteChanges(changesCount); // Wait a little bit to receive all changes. Thread.Sleep(1000); } Assert.AreEqual(changesCount, changesReceived); 

J'espère que cela t'aides.