[英]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: 我已经确定以下几点:
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.