public class NotoficationRepository : BaseClass { public int IsNotification(int UserID) { try { private string sqlString = string.Format("SELECT COUNT_BIG(*) AS [COUNT] FROM [dbo].[Notifications] WHERE UserID = '{0}' AND IsNotificationShow = '{1}'", UserID, false); var count = 0; SqlDependency.Stop(ConnectionSring); SqlDependency.Start(ConnectionSring); using (var connection = new SqlConnection(ConnectionSring)) { connection.Open(); using (command = new SqlCommand(sqlString, connection)) { command.Notification = null; SqlDependency dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); if (connection.State == ConnectionState.Closed) connection.Open(); count = int.Parse(command.ExecuteScalar().ToString()); } } return count; } catch { throw; } finally { base.Dispose(); } } }
void dependency_OnChange(object sender, SqlNotificationEventArgs e) { if (e.Info == SqlNotificationInfo.Insert) { MessagesHub.SendMessages(); } }
public class BaseClass : IDisposable { private bool disposed = false; protected NotificationEntities DB; protected string ConnectionSring; public BaseClass() { DB = new NotificationEntities(); ConnectionSring = DB.Database.Connection.ConnectionString; } ~BaseClass() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposed) return; if (disposing) { if (DB != null) { DB.Dispose(); DB = null; } } disposed = true; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)