我正在尝试使用SqlDependency,并且阅读了Microsoft的文章和Creating a Query for Notification。我多次检查,似乎都满足了文章中提到的要求。这是我的代码。
private void InitialSqlDependency()
{
using (var connection = new SqlConnection(_connString))
{
connection.Open();
string message = string.Empty;
string query = @"SELECT ModifiedOn FROM [dbo].[ContainerTransactions]";
using (var command = new SqlCommand(query, connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
message = dr[0].ToString();
}
}
}
}
private void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
_logger.Debug("ContainerWatch Dependency Fired!");
if (e.Type == SqlNotificationType.Change)
{
_logger.Debug("ContainerWatch Change Fired!");
this.InitialSqlDependency();
}
}
但是,它始终无法订阅。而且我看到Query Notification Permissions返回了Query
,这意味着A SELECT statement that cannot be notified or was provided.
SqlNotificationInfo
SELECT语句非常简单,是否有导致失败的可能原因?
答案 0 :(得分:0)
我找到了根本原因,因为The statement must not reference tables with computed columns
。我在下面使用查询查看计算所得的列
SELECT * FROM sys.computed_columns WHERE object_id = OBJECT_ID('ContainerTransactions')
因此,我想我不能在此表上使用SqlDependency。