【发布时间】:2014-05-28 08:27:42
【问题描述】:
使用以下代码,不会引发我的 OnChange 事件:
SqlDependency.Start(connectionString);
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
using (SqlCommand command = new SqlCommand(
"SELECT Name, Description FROM dbo.Boms",
conn))
{
SqlDependency dep = new SqlDependency(command);
dep.OnChange += dep_OnChange;
// Execute the command.
using (SqlDataReader reader = command.ExecuteReader())
{
// Process the DataReader.
}
}
我搜索了任何可能的查询限制,但我的查询似乎没问题。 dep 对象也已正确创建,但我尝试(成功)在我的 Boms 表中插入一条记录,但我没有收到任何反馈或事件。 在 Sql Server 2012 中,每次启动应用程序时都会看到一个新队列。
我在系统表中查找了错误,但似乎一切正常。
任何提示至少可以理解问题出在哪里?
谢谢!
【问题讨论】:
-
执行进程是否有选择权限?
-
@LIUFA:我没有写,我也使用这种返回 true 的方法:
private bool CanRequestNotifications() { SqlClientPermission permission = new SqlClientPermission( PermissionState.Unrestricted); try { permission.Demand(); return true; } catch (System.Exception) { return false; } }
标签: c# sql sql-server-2012 sqldependency