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

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) 动态解析XAML文本构建WPF的UI 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(五)XML Query-XQuery SQL Server操作XML(四)XML数据类型 SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 SQL Server操作XML(一)XML子句 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之二数据绑定 照猫画虎WPF之一:命名空间 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
WPF非轮询方式实时更新数据库变化SqlDependency
挑战 · 2012-11-26 · via 博客园 - 挑战

(1)启用当前数据库的 SQL Server Service Broker

alter database 数据库名称 set enable_broker

若命令执行成功的话,验证一下,执行下面SQL语句

select IS_BROKER_ENABLED from master.sys.databases

where name='数据库名称'

值为1表示开启,为0表示未开启 

(2)后台代码

    public partial class Page1 : Page
    {
        private static string connStr;
        SqlDataReader sdr;
        public Page1()
        {
            InitializeComponent();
            connStr = 数据库连接字符串

            SqlDependency.Start(connStr);//传入连接字符串,启动基于数据库的监听
            UpdateUI();
        }

        private void UpdateUI()
        {
            using (SqlConnection connection = new SqlConnection(connStr))
            {
                //依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
                //获取要监控的数据内容,这里只监控一条数据
                using (SqlCommand command = new SqlCommand("select ID,UserID,[Message] From [dbo].[Messages] where ID=2", connection))
                {
                    command.CommandType = CommandType.Text;
                    connection.Open();
                    SqlDependency dependency = new SqlDependency(command);
                    //当后台数据库发生变化时,触发该事件
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    sdr = command.ExecuteReader();
                    while (sdr.Read())
                    {
                        //线程安全,使用Dispatch线程更新界面
                        DispatchUpdateUI(sdr);
                    }
                    sdr.Close();
                }
            }
        }

        private void DispatchUpdateUI(SqlDataReader sdr)
        {
            try
            {
                tbxUserID.Dispatcher.Invoke(new UpdatetbxUserDelegate(UpdatetbxUserIDAction));
                tbxMessage.Dispatcher.Invoke(new UpdatetbxMessageDelegate(UpdatetbxMessageAction));
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private delegate void UpdatetbxUserDelegate();
        private delegate void UpdatetbxMessageDelegate();

        private void UpdatetbxUserIDAction()
        {
            tbxUserID.Text = sdr["UserID"].ToString();
        }
        private void UpdatetbxMessageAction()
        {
            tbxMessage.Text = sdr["Message"].ToString();
        }
        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            UpdateUI();
        }

    }

DataReader始终和数据库连接,当检测数据发生变化时,触发OnChange 事件,编写后台逻辑,获取数据库的本地副本,这里使用数据绑定(ObservableCollection<T>)发现无法实时更新到客户端,有待继续研究,暂时采用了手动刷新界面的方式,涉及到了Dispatch UI线程。