简体   繁体   English

为什么我的SqlDependancy OnChange()事件不触发?

[英]Why does my SqlDependancy OnChange() event not fire?

I am trying to implement a SqlDependancy to fire events when a change is made to one of the tables in MySQL server database, however the event does not seem to fire when I make alterations to the table through SSMS, why could this be? 我正在尝试实现SqlDependancy以在对MySQL服务器数据库中的表之一进行更改时触发事件,但是当我通过SSMS对表进行更改时,该事件似乎没有触发,为什么呢?

My implmentation below is inline with Microsofts tuturiol : 我在下面的实现与Microsoft tuturiol一致

public int GetVehicleCount(bool monitorCount)
{
      int count;          
      string query = "SELECT AREA_ID, VEHICLE_COUNT, ADDED_ON FROM CAPACITY_LOG";

      using (var sql = DBClass.Instance.OpenSqlConn())
      using (var cmd = new SqlCommand(query, sql))
      {                  
           SqlDependency sqlDependancy = new SqlDependency(cmd);

           sqlDependancy.OnChange += new ChangeEventHandler(VehicleCount_Changed);

            using (var reader = cmd.ExecuteReader())
            {                            
                //Do something    
            }       

            return 0;
     }
}

private void VehicleCount_Changed(object sender, SqlNotificationEventArgs e)
{
     //throw new NotImplementedException();            
}

I have made sure of the following: 我已经确定以下几点:

  • The connection to the database can be opened. 可以打开与数据库的连接。
  • The database has the Service-Broker enabled. 数据库已启用服务代理。
  • The SqlDependancy has been started. SqlDependancy已启动。
  • No exceptions are being thrown during runtime 运行时不会引发任何异常

Note that the event fires once shortly after it has been subscribed to and never again, below are the values of the event args: 请注意,事件在订阅后不久就会触发一次,再也不会触发,以下是事件args的值: 在此处输入图片说明

Queries with a SqlDependency have a number of requirements . 具有SqlDependency查询有许多要求 When a query is executed that doesn't meet these requirements, the ChangeEventHandler fires immediately with Invalid in SqlNotificationEventArs.Info . 当执行的查询不满足这些要求时, ChangeEventHandler会立即以SqlNotificationEventArs.Info Invalid SqlNotificationEventArs.Info

In this case, the query is invalid because the table name was not schema-qualified. 在这种情况下,查询无效,因为表名不是架构限定的。 Specify a 2-part name so that the query is valid for notifications. 指定一个由两部分组成的名称,以便查询对通知有效。 This example assumes the dbo schema: 此示例假定dbo模式:

string query = "SELECT AREA_ID, VEHICLE_COUNT, ADDED_ON FROM dbo.CAPACITY_LOG";

This change should result in a valid SqlDependency . 此更改将导致有效的SqlDependency Note that when the ChangeEventHander is invoked, the normal pattern is to execute the query again with SqlDependency, which will get the latest data and re-subscribe to change notifications. 请注意,在调用ChangeEventHander时,通常的模式是使用SqlDependency再次执行查询,这将获取最新数据并重新订阅以更改通知。

The connection is disposed of, so it stops receiving input from the database. 连接已被丢弃,因此它停止接收来自数据库的输入。 I have a code sample available at https://weblogs.asp.net/ricardoperes/broadcasting-database-changes-through-signalr that does work. 我在https://weblogs.asp.net/ricardoperes/broadcasting-database-changes-through-signalr上有一个有效的代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server通知 - 我的OnChange不会触发 - SQL Server Notifications - My OnChange does not fire 为什么我的嵌套HttpModule EndRequest事件处理程序不会触发? - Why does my nested HttpModule EndRequest event handler not fire? 为什么此事件异步触发? - Why does this event fire asynchronously? SQL Server通知-我的OnChange不会从Windows服务触发 - SQL Server Notifications - My OnChange does not fire from a windows service 为什么按钮不触发点击事件 - Why does the button not fire a click event 为什么DropDownList.SelectedIndexChanged事件不会触发? - Why DropDownList.SelectedIndexChanged event does not fire? 为什么我的itemCommand DataGrid事件仅在网格控件中第二次单击项目时触发? - Why does my itemCommand DataGrid event only fire on the second click of an item in the grid control? 为什么我动态创建的用户控件不触发按钮单击事件 - Why does my dynamically created user control doesn't fire button click event SqlDependency不会触发OnChange事件 - SqlDependency doesn't fire OnChange event 为什么在 DataGridView 中的 SelectionChanged 事件时不触发 CellClick 事件? - Why CellClick event does not fire when SelectionChanged event in DataGridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM