ASP.NET 中的 SqlDependency
我正在使用 SqlDependency 来控制我的缓存。我想用它来监控几个表(大约10个)。每个监视表应该有一个 SqlDependency。
我应该为每个人创建这样的代码:
public void CreateDependency_Table()
{
if (connectionStringSettings != null)
{
using (SqlConnection conn = new SqlConnection(connectionStringSettings.ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT id from dbo.Table", conn))
{
cmd.Notification = null;
SqlDependency sqlDependency = new SqlDependency(cmd);
sqlDependency.OnChange += new OnChangeEventHandler(sqlDep_Table_OnChange);
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
}
}
}
}
}
和:
private void sqlDep_Table_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= sqlDep_Table_OnChange;
MyCacheWhatever.Clear();
//Re-attach dependency
CreateDependency_Table();
}
或者我可以在它们之间重用某些东西吗?喜欢连接吗?
这是设置多个通知的首选方式吗?
I'm using SqlDependency to control my cache. I want to use it to monitor several tables (around 10). There should be one SqlDependency per watched table.
Should I create for each of them code like that:
public void CreateDependency_Table()
{
if (connectionStringSettings != null)
{
using (SqlConnection conn = new SqlConnection(connectionStringSettings.ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT id from dbo.Table", conn))
{
cmd.Notification = null;
SqlDependency sqlDependency = new SqlDependency(cmd);
sqlDependency.OnChange += new OnChangeEventHandler(sqlDep_Table_OnChange);
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
}
}
}
}
}
and:
private void sqlDep_Table_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= sqlDep_Table_OnChange;
MyCacheWhatever.Clear();
//Re-attach dependency
CreateDependency_Table();
}
or can I reuse something between them? Like connection?
Is this the preferred way of setting multiple notifications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里我将向您展示一个可能对您有所帮助的 linq 扩展:
Here I'll show you a linq extension that may help you:
我设计了下面的类,它为一个或多个查询连接一个侦听器,它可能不是最好的解决方案,但它有效。
因此,它将为每个触发器创建一个对象,例如,可用于触发 SinalR。您只需要在 Global.asax 中启动 Dependency 和 SqlDependencyHelper 类,所有内容都将存储在 SqlDataManagement 中,就像触发器是更新或删除以及哪个 id 已更改一样。
SELECT 中名为 ReferenceItem 的第三个字段可用于了解触发器是否因更新而发生,因此我使用名为 lastChanged 的 DateTime DB 列来了解哪一行已更新。
所有查询都必须来自列表并使用以下格式
选择示例
@“SELECT 'PreStartPhotos' as QueryReferenceName, [id], '' as ReferenceItem FROM [dbo].[PreStartPhotos]”
课程
}
I've designed the below class that hook up a listener for one or more queries, it may not be the best solution but it works.
So it will create an object for every trigger that can, for example, be used to trigger the SinalR. You just need to start the Dependency and the class SqlDependencyHelper within the Global.asax and everything will be stored within the SqlDataManagement like if the trigger is an update or delete and which id has changed.
A third field within the SELECT that is called ReferenceItem can be used to know if the trigger happened due to an update, so I used the DateTime DB column called lastChanged to know which row has updated.
All queries must be from a list and using the format below
Select Sample
@"SELECT 'PreStartPhotos' as QueryReferenceName, [id], '' as ReferenceItem FROM [dbo].[PreStartPhotos]"
Classes
}