2016-10-20 8 views
0

Microsoftのガイドを使用して、sqldependencyを使用してテストWebアプリケーションを作成しました。 私のデータベースでは、私はNorthwindデータベースを使用しています。私はテーブルとラベルへの更新を期待しているので、依存関係は機能していないようです。 また、ブローカが有効になっています。SQLDependencyが更新されない

ここに私のコード:

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"; 
    } 
} 
} 

答えて

1
  1. あなたは、トリガーが実際に発射したときのためのイベントハンドラを持っていないのglobal.asa
  2. ののApplication_StartでsqlDependencyを起動する必要があります。

これは私があなたが探していると思うものの完全な例です。

Polling for database changes: SqlDependency, SignalR is Good

関連する問題