In modern applications, keeping the UI up-to-date with database changes in real-time is critical for user experience. Traditionally, developers would use polling techniques, periodically asking the database for updates. This approach is inefficient, leading to unnecessary network and database overhead, and can also introduce noticeable delays for the user.
SQL Server offers a feature called SqlDependency
, which allows applications to be notified when data has changed. Instead of constantly asking the database for changes, our application can simply “listen” for changes and respond when they occur. This not only reduces the overhead but ensures near real-time updates. This tutorial aims to guide developers on how to harness this feature in C# applications.
Before using SqlDependency, ensure your database has Service Broker enabled.
ALTER DATABASE YourDatabaseName SET ENABLE_BROKER;
CREATE TABLE TestTable(Id INT PRIMARY KEY IDENTITY(1,1),Name NVARCHAR(50));
using System;using Microsoft.Data.SqlClient;
class Program{private static string connectionString = "YourConnectionString";static void Main(string[] args){StartDependency();Console.ReadKey();}static void StartDependency(){SqlDependency.Start(connectionString);RegisterDependency();}static void RegisterDependency(){SqlConnection connection = new SqlConnection(connectionString);connection.Open();SqlCommand command = new SqlCommand("SELECT Id, Name FROM [dbo].[TestTable]", connection);SqlDependency dependency = new SqlDependency(command);dependency.OnChange += OnDataChange;SqlDataReader reader = command.ExecuteReader();while (reader.Read()){// Process data if needed.}reader.Close();}private static void OnDataChange(object sender, SqlNotificationEventArgs e){if (e.Info == SqlNotificationInfo.Insert || e.Info == SqlNotificationInfo.Update || e.Info == SqlNotificationInfo.Delete){Console.WriteLine("Data has changed!");SqlDependency dependency = (SqlDependency)sender;dependency.OnChange -= OnDataChange;// Re-register the SqlDependency to continue receiving notifications.RegisterDependency();}}}
Issue: SqlDependency stops working after the first notification.
Issue: Notifications are not being received.
Issue: SqlDependency cannot be started because the Service Broker contract has not been enabled for the current database.
Issue: Permission errors.
Issue: High memory usage.
Issue: SqlDependency does not support express editions of SQL Server.
SqlDependency provides an efficient way to receive data change notifications from SQL Server. This tutorial has shown you how to set up SqlDependency in a C# application, and provided solutions for common issues you might encounter. Always ensure your queries are supported and that your SQL user has the necessary permissions.
Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:
If you found this guide beneficial, don’t hesitate to share it with your network and help others discover these useful features in C# and MSSQL.
Until the next guide, happy coding!
Quick Links
Legal Stuff