如何使用linqtosql实现sqldependency? [英] How do I implement sqldependency with linqtosql?

查看:155
本文介绍了如何使用linqtosql实现sqldependency?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨^ _ ^



我想观看配置表中的任何更改。该表基本上包含可由管理员覆盖的用户设置。如果某些功能或访问被关闭,那么我需要提醒客户端(使用网络套接字)。



问题是管理员应用和客户端应用是不是在同一个解决方案中。



我的同事想要调查我的数据库(他喜欢他的民意调查> _<)但我宁愿更优雅的东西。



配置不会经常更改。



所以,这就是我在我的背景信息Ctor:





Hi ^_^

I want to watch a Configuration table for any changes. This table essentially contains user settings that can be over-ridden by an Admin. If some functionality or access is turned off, then I need to alert the client side (using web sockets).

The issue is that the Admin app and Client app are not in the same solution.

My colleague wants to poll my db (he loves his polls >_<) but I would rather something more elegant.

The Configurations won't be changed very often at all.

So, here is what I have in my Context Ctor:


public Context() // custom Context ctor.
            : base(ConfigurationManager.ConnectionStrings["ebrokerAccess"].ConnectionString)
  {
    SqlDependency.Stop(ConfigurationManager.ConnectionStrings["ebrokerAccess"].ConnectionString);
    SqlDependency.Start(ConfigurationManager.ConnectionStrings["ebrokerAccess"].ConnectionString);

     SqlDependency dep = new SqlDependency((SqlCommand) GetCommand(Configurations.AsQueryable()));

    dep.OnChange += (sender, args) =>
    {
      ConfigurationView.Changed(sender, args);
    };

  }





我有服务器代理正在运行,我在数据库上启用了代理,但是当我通过MSSqlMS更改数据库值时,事件不会触发。



我是否完全误解了用法?



任何提示都会有所帮助^ _ ^

谢谢

Andy



我尝试过:



我希望使用L2S On [prop]更改/更改事件,但我认为它们只是Context实例中的PropertyChanged方法。



I have the server agent running and I've enable the broker on the database, but when I change a db value via MSSqlMS, the events don't fire.

Have I completely misunderstood the usage?

Any hints would be helpful ^_^
Thanks
Andy

What I have tried:

I was hoping to use the L2S On[prop]Changing / Changed events, but I gather that they are just PropertyChanged methods within the Context instance.

推荐答案

看起来你错过了一个至关重要的步骤:

It looks like you're missing a vital step:



5.使用<$的任何执行方法执行命令c $ c> SqlCommand 对象。因为命令绑定到通知对象,所以服务器识别出它必须生成通知,并且队列信息将指向依赖队列。


5. Execute the command using any of the Execute methods of the SqlCommand object. Because the command is bound to the notification object, the server recognizes that it must generate a notification, and the queue information will point to the dependencies queue.



换句话说,通知不是在相关命令执行之前注册。由于您从未执行该命令,因此未注册通知,并且永远不会引发更改事件。


In other words, the notification isn't registered until the associated command is executed. Since you never execute the command, the notification isn't registered, and the change event is never raised.

using (SqlCommand command = (SqlCommand)GetCommand(Configurations.AsQueryable()))
{
    SqlDependency dep = new SqlDependency(command);
    
    dep.OnChange += (sender, args) =>
    {
      ConfigurationView.Changed(sender, args);
    };
    
    using (SqlDataReader reader = command.ExecuteReader())
    {
        // You might need to read the rows here; the documentation isn't clear.
    }
}


这篇关于如何使用linqtosql实现sqldependency?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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