SQLDependency

编程入门 行业动态 更新时间:2024-10-20 08:00:27
本文介绍了SQLDependency_OnChange-Event仅触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用SQLDependency来通知我数据库中是否有更改.程序启动后,它可以正常工作.当我进行第一次更改时,将触发该事件.哇,太好了.但是,如果我进行了第二次更改,则该事件不会再次触发.我搜索了我认为的所有网络,但未找到有关此问题"的任何信息.仅发现OnChange-Event在循环中触发的问题.谁能帮我吗?

I'm working with SQLDependency to notify me if there is a change in the Database. After Program Start-Up it works just fine. When I make a first change the Event fires. Wohoo... that's great. But if I made a second change the event doesn't fire again. I've searched all the web I think but haven't found anything about THIS Problem. Only found problems where the OnChange-Event fires in a Loop. Can anyone help me?

这里有一段代码:

private void GetStates() { if (!DoesUserHavePermission()) return; SqlDependency.Stop(con); SqlDependency.Start(con); using (SqlConnection cn = new SqlConnection(con)) { using (SqlCommand cmd = cn.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT Bla, Bla2, ..FROM dbo.[BLA3]" cmd.Notification = null; cmd.Dispose(); SqlDependency dep = new SqlDependency(cmd); dep.OnChange += new OnChangeEventHandler(dep_OnChange); cn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { state.Clear(); //In this Case "state" is a List<string> while (dr.Read()) { state.Add(dr.GetString(0) + "|" + dr.GetInt32(3)); } dr.Dispose(); dr.Close(); } } } }

我的OnChange-Event看起来像这样:

my OnChange-Event looks like this:

private void dep_OnChange(object sender, SqlNotificationEventArgs e) { SqlDependency dep = sender as SqlDependency; dep.OnChange -= this.dep_OnChange; using (SqlConnection cn = new SqlConnection(con)) { using (SqlCommand cmd = cn.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT Bla, Bla2, ..FROM dbo.[BLA3]"; cmd.Notification = null; if (e.Type == SqlNotificationType.Change) { if (cn.State != ConnectionState.Open) { cn.Open(); } using (SqlDataReader dr = cmd.ExecuteReader()) { state.Clear(); // Clear and Refill the stringlist "state" while (dr.Read()) { state.Add(dr.GetString(0) + "|" + dr.GetInt32(3)); } } } cn.Close(); } } this.GetStates(); //to go ahead and wait for a new change }

问题出在哪里?

推荐答案

我也遇到了这个问题.您需要创建一个新的SqlDependency实体(在OnChange事件中取消现有的实体的订阅后),然后运行新的ExecuteReader命令.我从这篇文章中得到了这个主意:

I was running into this issue as well. You need to create a new SqlDependency entity (after unsubscribing the existing one from the OnChange event) and then run a new ExecuteReader command. I got the idea from this post:

www.codeproject/Articles/12335/使用-SqlDependency-for-data-change-events

这通常是有道理的,因为一旦收到更改通知,您通常将希望重新查询数据.

This usually makes sense, as once you have been notified of a change you will normally want to re-query the data.

更多推荐

SQLDependency

本文发布于:2023-11-27 12:23:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQLDependency

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!