SqlDependency 通知 - 执行查询后立即失败通知
我遇到一个问题,我试图设置 SqlDependency 通知,以便在 sql 服务器上的表中的数据发生更改时接收通知。但是,一旦我执行用于设置 sql 依赖关系的查询,就会立即收到一条通知,指示由于 sql 语句问题而尝试订阅失败(SqlNotificationEventArgs 显示信息:无效,来源:Statement,类型:Subscribe
)
这表明 sql 查询存在问题,但是在尝试了一个非常基本的示例以确保它不是 select 语句的问题后,我仍然收到这些立即发出“无效”通知。我还确保我已经启动了 SQL Server 的服务代理,创建了队列和通知服务,并向主体授予了所有必要的权限(在本例中是我连接到 sql server 的用户) 这是我的表格:
CREATE TABLE [dbo].[TableTest](
[id] [int] NOT NULL,
[val1] [int] NULL,
[val2] [int] NULL,
CONSTRAINT [PK_TableTest] PRIMARY KEY CLUSTERED ( [id] ASC )
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
这是代码:
SqlDependency.Start(connectStr);
_connection = new SqlConnection(connectStr);
_connection.Open();
_sqlCommand = new SqlCommand("Select [id] from TableTest", _connection);
_sqlCommand.Notification = null;
SqlDependency dependency = new SqlDependency(_sqlCommand);
dependency.OnChange += this.OnDataChangeNotification;
DataTable dt = new DataTable();
dt.Load(_sqlCommand.ExecuteReader());
调用“_sqlCommand.ExecuteReader()”后,立即调用 OnDataChangeNotification 处理程序,其中 SqlNotificationEventArgs 参数显示 Info:Invalid、Source:Statement、Type:Subscribe。
任何人都知道问题可能是什么,或者如何确定/调试它是什么(不使用 SQL 探查器,我没有 atm)。
I am having a problem where i am trying to setup SqlDependency notifications to receive notifications when data in a table on sql sever changes. However, as soon as i execute the query that i use to set up the sql depenency for, a notification is immediately received indicating that the attempt to subscribe failed due to an issue with the sql statement (SqlNotificationEventArgs shows Info: Invalid, Source: Statement, Type: Subscribe
)
This indicates that there i a problem with the sql query, but having tried a very basic example to make sure it is a not an issue with the select statement, i am still recieving these 'invalid' notifications immediately. I've also made sure that i've started SQL Server's service broker, created a queue and notification service, and granted all the necessary permissions to the principal (in this case the user i'm connecting to the sql server with)
Here is my table:
CREATE TABLE [dbo].[TableTest](
[id] [int] NOT NULL,
[val1] [int] NULL,
[val2] [int] NULL,
CONSTRAINT [PK_TableTest] PRIMARY KEY CLUSTERED ( [id] ASC )
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
here is the code:
SqlDependency.Start(connectStr);
_connection = new SqlConnection(connectStr);
_connection.Open();
_sqlCommand = new SqlCommand("Select [id] from TableTest", _connection);
_sqlCommand.Notification = null;
SqlDependency dependency = new SqlDependency(_sqlCommand);
dependency.OnChange += this.OnDataChangeNotification;
DataTable dt = new DataTable();
dt.Load(_sqlCommand.ExecuteReader());
After '_sqlCommand.ExecuteReader()' is called, immediately the OnDataChangeNotification handler is invoked with the SqlNotificationEventArgs parameter showing Info:Invalid, Source:Statement, Type:Subscribe.
Anyone know what the problem might be or how to determine/debug what it is (without using SQL profiler which i do not have atm).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在 SQL select 语句中使用表的两个部分名称 (dbo.TableName) 才能使用 SqlDependency 通知:
以下是查询通知要求的链接:MSDN 查询通知。
希望这有帮助。
You have to use the two part names (dbo.TableName) for your tables in the SQL select statement in order to use SqlDependency notification:
Here is a link to requirements for query notifications: MSDN Query Notifications.
Hope, this helps.