【问题标题】:Cache is not getting refreshed in sqldependancysqldependancy 中的缓存未刷新
【发布时间】:2014-03-05 09:15:11
【问题描述】:

我想在对数据库进行更改时自动刷新数据。

我用this documentation:

并将页面上的代码加载为:

 protected void Page_Load(object sender, EventArgs e)
    {
        conString = "Data Source=MITEJ5-PC\\MITEJTECHONOLY;Initial Catalog=SSISTestDatabase;Integrated Security=SSPI;";

        SqlDependency.Start(conString);
        using (SqlConnection connection =
       new SqlConnection(conString))
        {
            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();

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

 private string GetSQL()
    {
        return "select Name,Age,Address from tlbStudent;";
    }

但是当我运行它并对 SQL 表数据进行更改时,它不会自动反映在网格上。

上面的代码哪里错了???

请帮帮我。

【问题讨论】:

    标签: c# asp.net .net sqldependency


    【解决方案1】:

    首先您需要了解SqlDependency 的工作原理。阅读The Mysterious Notification 了解简短介绍。一旦您了解到真正起作用的功能是查询通知,您就可以了解使用通知进行查询的限制,请参阅Creating a Query for Notification。一旦这样的限制是:

    SELECT 语句中的投影列必须明确声明,并且表名必须由两部分名称限定。请注意,这意味着语句中引用的所有表都必须在同一个数据库中。

    对于未来的问题,请阅读Troubleshooting Query Notifications

    【讨论】:

      【解决方案2】:

      中的问题

      private string GetSQL()
      {
          return "select Name,Age,Address from tlbStudent;";
      }
      

      表名应该是两部分“”

      private string GetSQL()
      {
          return "select Name,Age,Address from dbo.tlbStudent;";
      }
      

      根据文档

      SELECT 语句中的投影列必须明确声明,表名必须用两部分名称限定。请注意,这意味着语句中引用的所有表都必须在同一个数据库中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 2012-09-19
        相关资源
        最近更新 更多