SQL Server的通知 - 我的OnChange不火 [英] SQL Server Notifications - My OnChange does not fire

查看:495
本文介绍了SQL Server的通知 - 我的OnChange不火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用SQL Server的通知在WinForms应用程序中我的数据库获取插入事件。我试图使用的SqlDependency对象。 MSDN文章使这个似乎相当直截了当。所以,我创建了一个小例子应用程序可以给它一个尝试。该事件似乎只火,因为我进入我的应用程序在第一时间(出现消息框)。数据插入到表中不会引发OnChange事件又好像。谁能告诉我,我缺少的是什么? !谢谢



 公开的Main()
{
的InitializeComponent();
VAR检查= EnoughPermission();
SqlDependency.Stop(构造);
SqlDependency.Start(构造);
如果(连接== NULL)
{
连接=新的SqlConnection(构造);
}
如果(命令== NULL)
{
命令=新的SqlCommand(选择ID,ChatMessage FROM dbo.Chat连接);
}
connection.Open();
command.Notification = NULL;
的SqlDependency依赖=新的SqlDependency(命令);
dependency.OnChange + =新OnChangeEventHandler(dependency_OnChange);
Command.ExecuteReader却();
}





私人无效dependency_OnChange(对象发件人,SqlNotificationEventArgs E)
{
的MessageBox。显示(更改!);
}


解决方案

虽然我在被工作执行查询通知,我得到了确切的问题。我检查了所有的配置,代码段,甚至TCP设置,但没有任何帮助。然后,我想通了,下面的查询在数据库上运行,它解决了我的问题。也许你可以试试。



  ALTER授权数据库:: [你的数据库]萨; 


I would like to make use of SQL Server notifications to capture insert events at my database within a winforms app. I am attempting to use the SQLDependency object. The MSDN articles make this seem pretty straight forward. So I have created a little example application to give it a try. The event only seems to fire as I enter my application the first time(MessageBox appears). Inserting data into the table does not raise the OnChange event it would seem. Can someone tell me what I'm missing? Thanks!

 public Main()
    {
        InitializeComponent();
        var check = EnoughPermission();
        SqlDependency.Stop(constr);
        SqlDependency.Start(constr);
        if(connection == null)
        {
            connection = new SqlConnection(constr);
        }
        if(command == null)
        {
            command = new SqlCommand("Select ID, ChatMessage FROM dbo.Chat",connection);
        }
        connection.Open();
        command.Notification = null;
        SqlDependency dependency = new SqlDependency(command);
        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
        command.ExecuteReader();
    }





    private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        MessageBox.Show("Change!");
    }

解决方案

While I was working in the implementation of query notification, I got the exact problem. I checked all configurations, code pieces, and even TCP settings, but nothing helped. Then, I figured out the following query to run on database and it solved my problem. Maybe you can try it.

ALTER AUTHORIZATION ON DATABASE::[Your DB] TO sa;

这篇关于SQL Server的通知 - 我的OnChange不火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆