惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 小小小程序员

转载 gridview在绑定显示的各种格式 - 小小小程序员 - 博客园 转 ASP.NET 2.0中OWC生成柱状图和饼状图的源代码 - 小小小程序员 同一个ASP站点UTF-8和GB2312的两个子站点乱码的解决方法 - 小小小程序员 转 ASP.NET 页面事件执行顺序 转 用 Javascript 验证表单(form)中的单选(radio)值 - 小小小程序员 转 .net里如何判断中文字符长度 转CSDN Sybase PowerDesigner专区 转 ASP.net中的Server.UrlEncode函数和ASP中的Server.URLEncode函数返回的值竟然不一样!!!! DIV实现下拉菜单 转 SqlServer 2005 报表批量自动部署 使用XMLHTTP 获取服务器数据,实现无刷新效果 转 SQL Server 2005中设置Reporting Services发布web报表的匿名访问 - 小小小程序员 div 属性 OBJECTPROPERTY函数使用 转 数字金额大小写转换之存储过程 转 PowerDesigner教程系列 - 小小小程序员 [转载]ASP.NET 格式化字符串 转 VS.NE类、变量、文件命名规范 - 小小小程序员 转 MVC在Web系统中的模式与应用
转 .NET 2.0 SqlDependency快速上手指南
小小小程序员 · 2008-02-14 · via 博客园 - 小小小程序员

此文以SQL Server 2005上实现为准,SQL Server 2000因为实现机制不同,请查阅相关资料。

       步骤一:在SQL Server 2005上执行ALTER DATABASE <DatabaseName> SET ENABLE_BROKER;语句让相应的数据库启用监听服务,以便支持SqlDependency特性。

       这条语句最好在数据库未执行任何事务的情况下执行。

       步骤二:调用SqlDependency.Start(String strConnectionString)方法,在应用程序端启用依赖监听器。

       该方法的参数为一个数据库的连接字符串,该数据库必须已经执行过步骤一的操作。

       对于同一连接字符串,若已经执行过该语句,再次执行不会发生任何异常,但返回值会为False.

       如果是在Web程序中使用,建议可以将该语句放在Application_Start事件中执行。

       监听是基于数据库的,而依赖才可以基于表或者查询。

       步骤三:该步骤分别有两种不同的做法。该阶段必须注意步骤。

              方法A:建立连接对象,再创建一个SqlCommand实例,创建SqlCacheDependency实例,在这步之后再调用Command对象来获取数据(这个顺序很重要)。之后调用CacheInsert语句建立一个依赖于一个具体查询数据集的Cache项。


             SqlConnection conn = new SqlConnection(strConnection);

         SqlCommand command = new SqlCommand(strCommandText, conn);

         SqlCacheDependency dependency = new SqlCacheDependency(command);

         // 注册方法到委托,该委托是

         CacheItemRemovedCallback onRemove = new CacheItemRemovedCallback(RemovedCallback);

         // 新增或修改一条缓存记录

         Cache.Insert(strCacheKey, objAppCache, dependency, absoluteExpiration, slidingExpiration, CacheItemPriority.Default, onRemove);

          方法B:建立连接对象,再创建一个SqlCommand实例,最后创建SqlDependency实例。定义SqlDependency的委托OnChange,当数据发生改变时做出相应的处理(比如清除Cache)。

              SqlConnection conn = new SqlConnection(strConnection);

         SqlCommand command = new SqlCommand(strCommandText, conn);

         SqlCacheDependency dependency = new SqlCacheDependency(command);

              dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);

     注意事项:

strCommandText参数关联的 SQL 语句必须包括以下内容:

  • 完全限定的表名,包括表所有者的名称。

    例如,要引用由数据库所有者拥有的名为 "customers" 的表,SQL 语句必须引用 "dbo.customers"

  • 在 Select 语句中显式指定列名。不能使用星号 (*) 通配符来选择表中的所有列。

    例如,不能使用 "select * from customers",而必须使用 "select name, address, city, state from dbo.customers"

       不知道是不是还存在BUG,我在项目开发中遇到一些奇怪的现象。同样的代码,在有的机器上运行则能捕捉到变化,有的则完全没反应;也有时会出现Cache刚建立就反复发生依赖改变的事件。偶尔Cache还会数据发生变化却不引发事件。

       但从最终项目实施的情况看,似乎都只是某些机器环境造成的不确定因素?这个无法确定。不过起码数据库端是否正常启用,可以通过SQL Server Profiler来查看监视。