SqlDependency无法自动更新 [英] SqlDependency failing to update automatically

查看:101
本文介绍了SqlDependency无法自动更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个sqlDependency任务,每当我将新数据插入到我想要的数据库表中时,它都会自动刷新页面。



下面的代码,当我将新数据插入数据库表时,不会自动刷新内容。如果我手动刷新页面,则内容会更新。

I am trying to implement a sqlDependency task, which can automatically refresh the page, whenever I insert new data into my desired database table.

The code below, is not refreshing the content automatically, when I insert new data into the database table. if I refresh it the page, manually, then the content gets updated.

protected void Page_Load(object sender, EventArgs e)
  {
      Label1.Text = "Cache Refresh: " +
      DateTime.Now.ToLongTimeString();

      // Create a dependency connection to the database.
      SqlDependency.Start(GetConnectionString());

      using (SqlConnection connection =
          new SqlConnection(GetConnectionString()))
      {
          using (SqlCommand command =
              new SqlCommand(GetSQL(), connection))
          {
              SqlCacheDependency dependency =
                  new SqlCacheDependency(command);
              // Refresh the cache after the number of minutes
              // listed below if a change does not occur.
              // This value could be stored in a configuration file.
              int numberOfMinutes = 1;
              DateTime expires =
                  DateTime.Now.AddMinutes(numberOfMinutes);

              Response.Cache.SetExpires(expires);
              Response.Cache.SetCacheability(HttpCacheability.Public);
              Response.Cache.SetValidUntilExpires(true);

              Response.AddCacheDependency(dependency);

              connection.Open();

              GridView1.DataSource = command.ExecuteReader();
              GridView1.DataBind();
          }
      }
  }





我在代码中做错了什么或者我需要额外的方法来执行此任务。任何提示都将是最受欢迎的。谢谢。



Am I doing something wrong in my code or is there extra method I require for this task. Any hints would be most appreciated. Thank you.

推荐答案

要实现SQL依赖关系,你还需要在Config中添加参数。
To implement SQL dependency you needs to add paramter in Config as well.


你好,



这是一篇关于SQL依赖关系的非常好的文章:使用SqlDependency和SqlCacheDependency查询通知 [ ^ ]



如果您需要在客户端运行数据,可以使用SignalR:http://www.asp.net/signalr [ ^ ]
Hi,

Here is a very good article on SQL Dependency : Query Notification using SqlDependency and SqlCacheDependency[^]

If you required run time data push at client you can use SignalR :http://www.asp.net/signalr[^]


这篇关于SqlDependency无法自动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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