2017-09-26 260 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)