SQLDependency OnChange事件仅触发单个时间 [英] SQLDependency OnChange Event fires only single Time
本文介绍了SQLDependency OnChange事件仅触发单个时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,我无法理解。我想在[dbo]。[Notifications]表中插入数据后立即向客户发送通知。为此,我使用sqldependency来升级onchange事件并使用SignalR发送信息客户端。问题是,当我第一次更改事件时,如果我做了第二次更改,事件就不再开火了
I have a problem, which I can not understand. I want to send notification to client as soon as data is insert in [dbo].[Notifications] table. for this I am using sqldependency for rise onchange event and to sent information client with SignalR. Problem is that when i make a first Change the Event fires, But if i made a second change the Event does not Fire any more
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();
}
}
}
OnChangeEvent
OnChangeEvent
void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Info == SqlNotificationInfo.Insert)
{
MessagesHub.SendMessages();
}
}
BaseClass
BaseClass
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;
}
}
你能帮我吗?问题是什么?
Can you help me?? what is the problem?
推荐答案
召回dependency_OnChange中的IsNotification()方法。
祝你好运
recall IsNotification() method in dependency_OnChange.
good luck
这篇关于SQLDependency OnChange事件仅触发单个时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文