首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQLDependency不更新

SQLDependency不更新
EN

Stack Overflow用户
提问于 2016-10-20 08:18:13
回答 1查看 428关注 0票数 0

我已经使用Microsofts创建了一个使用used依赖项的测试used应用程序。对于我的数据库,我使用的是Northwind数据库。看来,依赖关系不起作用,因为我期望对表和标签进行一个imidiate更新。此外,还启用了代理。

这里我的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Caching;


namespace WebDependency
{
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Cache Refresh: " + DateTime.Now.ToLongTimeString();

        SqlDependency.Start(GetConnectionString());

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            using (SqlCommand command = new SqlCommand(GetSQL(), connection))
            {
                SqlCacheDependency dependency = new SqlCacheDependency(command);

                int numberOfSeconds = 3;
                DateTime expires = DateTime.Now.AddSeconds(numberOfSeconds);

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

                Response.AddCacheDependency(dependency);

                connection.Open();

                GridView1.DataSource = command.ExecuteReader();
                GridView1.DataBind();
            }
        }
    }
    private string GetConnectionString()
    {
        return "Data Source=(LocalDB)\\MSSqlLocalDB;Initial Catalog=Northwind;Integrated Security=True";
    }

    private string GetSQL()
    {
        return "Select CategoryName, Description from dbo.Categories";
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-21 18:14:06

  1. 您需要在sqlDependency的Application_Start of global.asa中启动global.asa
  2. 当触发器实际启动时,您没有事件处理程序。

这有一个完整的例子,我认为你在寻找什么。

Polling for database changes: SqlDependency, SignalR is Good

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40149045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档