SqlDependency 通知 - 执行查询后立即失败通知

发布于 2024-12-12 10:00:52 字数 1371 浏览 0 评论 0原文

我遇到一个问题,我试图设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-12-19 10:00:52

您必须在 SQL select 语句中使用表的两个部分名称 (dbo.TableName) 才能使用 SqlDependency 通知:

SqlDependency.Start(connectStr); 
_connection = new SqlConnection(connectStr); 
_connection.Open(); 
_sqlCommand = new SqlCommand("Select [id] from dbo.TableTest", _connection); 
_sqlCommand.Notification = null; 
SqlDependency dependency = new SqlDependency(_sqlCommand); 
dependency.OnChange += this.OnDataChangeNotification; 

以下是查询通知要求的链接: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:

SqlDependency.Start(connectStr); 
_connection = new SqlConnection(connectStr); 
_connection.Open(); 
_sqlCommand = new SqlCommand("Select [id] from dbo.TableTest", _connection); 
_sqlCommand.Notification = null; 
SqlDependency dependency = new SqlDependency(_sqlCommand); 
dependency.OnChange += this.OnDataChangeNotification; 

Here is a link to requirements for query notifications: MSDN Query Notifications.

Hope, this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文