Install-Package SqlTableDependency
using System; using TableDependency.SqlClient; using TableDependency.SqlClient.Base.Enums; using TableDependency.SqlClient.Base.EventArgs; public class Program { static void Main(string[] args) { var connectionString = "Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True;"; var tableName = "MyTable"; using (var dep = new SqlTableDependency(connectionString, tableName)) { dep.OnChanged += Changed; dep.Start(); Console.WriteLine("Press a key to exit"); Console.ReadKey(); dep.Stop(); } } public class MyModel { public int Id { get; set; } public string Name { get; set; } } private static void Changed(object sender, RecordChangedEventArgs e) { if (e.ChangeType != ChangeType.None) { Console.WriteLine($"Change: {e.ChangeType}. Id: {e.Entity.Id}. Name: {e.Entity.Name}"); } } }
using System; using System.Collections.Generic; using TableDependency.SqlClient; using TableDependency.SqlClient.Base.Enums; using TableDependency.SqlClient.Base.EventArgs; public class Program { static void Main(string[] args) { var connectionString = "Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True;"; var tableNames = new ListIn conclusion, SqlTableDependency is a package library that provides an easy way to monitor SQL Server database tables for real-time data changes.{ "Table1", "Table2", "Table3" }; using (var dep = new SqlTableDependency (connectionString, tableNames)) { dep.OnChanged += Changed; dep.Start(); Console.WriteLine("Press a key to exit"); Console.ReadKey(); dep.Stop(); } } public class MyModel { public int Id { get; set; } public string Name { get; set; } } private static void Changed(object sender, RecordChangedEventArgs e) { if (e.ChangeType != ChangeType.None) { Console.WriteLine($"Change: {e.ChangeType}. Id: {e.Entity.Id}. Name: {e.Entity.Name}"); } } }