2017-09-26 174 views
0

我想第一次嘗試SqlDepenedency。我沒有收到有關數據庫更新的任何通知。SqlDependency沒有觸發

我把裏面的斷點: OnChange(object sender, SqlNotificationEventArgs e)

但它從來沒有被擊中。

這裏是我的代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 

     Label1.Text = "Cache Refresh: " + DateTime.Now.ToLongTimeString(); 
     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)) 
      { 

       SqlDependency dependency = 
         new SqlDependency(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. 
            connection.Open(); 

       dgHomeRequests.DataSource = command.ExecuteReader(); 
        dgHomeRequests.DataBind(); 

      } 
     } 
    } 


    private string GetConnectionString() 
    { 
     // To avoid storing the connection string in your code, 
     // you can retrieve it from a configuration file. 
     //return "Data Source=(local);Integrated Security=true;" +"Initial Catalog=AdventureWorks;"; 
     return ConfigurationManager.ConnectionStrings["TestData"].ConnectionString; 
    } 
    private string GetSQL() 
    { 
     return "Select [Address] From [UserAccount1]"; 
    } 


    void OnChange(object sender, SqlNotificationEventArgs e) 
    { 
     // have breakpoint here: 
     SqlDependency dependency = sender as SqlDependency; 

     // Notices are only a one shot deal 
     // so remove the existing one so a new 
     // one can be added 

     dependency.OnChange -= OnChange; 

     // Fire the event 
     /* if (OnNewMessage != null) 
     { 
      OnNewMessage(); 
     }*/ 
    } 

我也已經把一些代碼在Global.asax文件:

public class Global : HttpApplication 
{ 
    void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     SqlDependency.Start(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString); 
    } 
    protected void Application_End() 
    { 
     // Shut down SignalR Dependencies 
     SqlDependency.Stop(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString); 
    } 
} 

SQL服務器是本地機器上。我通過Visual Studio(IIS Express)運行代碼。

  1. 要在數據庫中啓用服務代理:

    ALTER DATABASE SET ENABLE_BROKER GO

  2. 要訂閱查詢通知,我們需要給權限的IIS服務帳戶

    GRANT SUBSCRIBE QUERY NOTIFICATIONS TO 「<serviceAccount>」 
    

我猜想第二點​​是不需要的,因爲我t是本地的。但我試着給它一些權限。不知道他們是否正確,因爲我不認爲它使用應用程序池。並且不需要本地環境許可。如果我是我自己的用戶並自己創建架構。

其中之一,我看到被授予問題:

alter authorization on database::<dbName> to [sa]; 

我給這個權限了。

+0

你可以檢查兩件事情:1),該通知是「設置」 ,請參閱['sys.dm_qn_subscriptions'](https://docs.microsoft.com/zh-cn/sql/relational-databases/system-dynamic-management-views/query-notifications-sys-dm-qn-subscriptions)和2)通知消息不保留在['sys.transmission_queue'](https://docs.microsoft.com/en-us/sql/relational -databases/system-catalog-views/sys-transmission-queue-transact-sql) –

+0

@RemusRusanu我進入了視圖,爲sys.dm_qn_subscriptions和sys.transmission_queue選擇了前1000行。他們都在那裏,他們都是空的。任何異常? –

+0

'sys.qn_subscriptions'中應該有一行。我懷疑你的查詢通知立即失效,因爲它違反了[這裏]列出的某個限制(https://technet.microsoft.com/en-us/library/ms181122(v = sql.105).aspx)。你可以使用一個基本的'SqlDependency'而不是'SqlCacheDependency'並且忽略你在['SqlNotificationEventArgs'](https://msdn.microsoft.com/en- us/library/system.data.sqlclient.sqlnotificationeventargs(v = vs.110).aspx) –

回答

0

我失蹤: dependency.OnChange += new OnChangeEventHandler(OnChange); 新的代碼是這樣:

   SqlDependency dependency = new SqlDependency(command); 

       dependency.OnChange += new OnChangeEventHandler(OnChange); 

現在我可以解僱 void OnChange(object sender, SqlNotificationEventArgs e)