Good day! How can I update a DataTable using SqlDependency ?
public void StartWatching() { SqlDependency.Stop(connectionString); SqlDependency.Start(connectionString); ExecuteWatchingQuery(); } static void Main(string[] args) {} private void ExecuteWatchingQuery() { using(SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using(var command = new SqlCommand( "select * from Shedule", connection)) { var sqlDependency = new SqlDependency(command); bool res = SqlDependency.Start(connectionString); sqlDependency.OnChange += new OnChangeEventHandler(OnDatabaseChange); command.ExecuteReader(); } } Console.Read(); } private void OnDatabaseChange(object sender, SqlNotificationEventArgs args) { SqlNotificationInfo info = args.Info; if (SqlNotificationInfo.Insert.Equals(info) || SqlNotificationInfo.Update.Equals(info) || SqlNotificationInfo.Delete.Equals(info)) Console.WriteLine("Change..."); ExecuteWatchingQuery(); } That's about. The program is written in the Console app.
OnDatabaseChangeeventOnDatabaseChangeopen the connection to the database, and read the new data in theDataTable. - Alexander Petrovstatic void Main(string[] args) {}what does this method do? The class methods of the console application itself, if you plan to call them inMainshould be static. This is only a quick look. Or in the given code a hodgepodge of methods of different classes? - rdorn