我正在尝试让 SqlDependency 对象给我通知,但到目前为止我无法让它工作。
我发送的 sql 查询是这样的:
SELECT [SocialMedia].[dbo].[Items].[Id]
FROM [SocialMedia].[dbo].[Items]
我已经尝试过不带大括号,具有完全限定的名称,只有变量名称以及介于两者之间的所有内容。
Id 属于 Guid
类型,我没有在创建通知查询一文中看到任何提及它们不适用于 SqlDependency 对象
我还尝试发送多列以查看是否有效。
SELECT SocialMedia.dbo.Items.Id,
SocialMedia.dbo.Items.DateTimeCreated,
SocialMedia.dbo.Items.AuthorId
FROM SocialMedia.dbo.Items
那也行不通。
这是我的表 SQL:
USE [SocialMedia]
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
CREATE TABLE [dbo].[Items](
[Id] [uniqueidentifier] NOT NULL,
[DateTimeCreated] [datetimeoffset](7) NOT NULL,
[AuthorId] [uniqueidentifier] NOT NULL,
[Source] [varchar](max) NOT NULL,
[PlaceId] [uniqueidentifier] NULL,
[FullText] [varchar](max) NOT NULL,
[Geography] [geography] NULL,
[DateTimeAdded] [datetime] NULL,
CONSTRAINT [PK_Items] 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] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Places]
FOREIGN KEY([PlaceId]) REFERENCES [dbo].[Places] ([Id])
ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Places]
GO
ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Users]
FOREIGN KEY([AuthorId]) REFERENCES [dbo].[Users] ([Id])
ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Users]
还有我的数据库:
USE [master]
GO
CREATE DATABASE [SocialMedia]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'SocialMedia',
FILENAME = N'C:Program FilesMicrosoft SQL ServerMSSQL11.DENALIMSSQLDATASocialMedia.mdf' ,
SIZE = 1110016KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'SocialMedia_log',
FILENAME = N'C:Program FilesMicrosoft SQL ServerMSSQL11.DENALIMSSQLDATASocialMedia_log.ldf' ,
SIZE = 123648KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [SocialMedia] SET COMPATIBILITY_LEVEL = 110
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) BEGIN
EXEC [SocialMedia].[dbo].[sp_fulltext_database] @action = 'enable'
END
GO
ALTER DATABASE [SocialMedia] SET ANSI_NULL_DEFAULT OFF
ALTER DATABASE [SocialMedia] SET ANSI_NULLS ON
ALTER DATABASE [SocialMedia] SET ANSI_PADDING ON
ALTER DATABASE [SocialMedia] SET ANSI_WARNINGS ON
ALTER DATABASE [SocialMedia] SET ARITHABORT ON
ALTER DATABASE [SocialMedia] SET AUTO_CLOSE OFF
ALTER DATABASE [SocialMedia] SET AUTO_CREATE_STATISTICS ON
ALTER DATABASE [SocialMedia] SET AUTO_SHRINK OFF
ALTER DATABASE [SocialMedia] SET AUTO_UPDATE_STATISTICS ON
ALTER DATABASE [SocialMedia] SET CURSOR_CLOSE_ON_COMMIT OFF
ALTER DATABASE [SocialMedia] SET CURSOR_DEFAULT GLOBAL
ALTER DATABASE [SocialMedia] SET CONCAT_NULL_YIELDS_NULL ON
ALTER DATABASE [SocialMedia] SET NUMERIC_ROUNDABORT OFF
ALTER DATABASE [SocialMedia] SET QUOTED_IDENTIFIER ON
ALTER DATABASE [SocialMedia] SET RECURSIVE_TRIGGERS OFF
ALTER DATABASE [SocialMedia] SET ENABLE_BROKER
ALTER DATABASE [SocialMedia] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
ALTER DATABASE [SocialMedia] SET DATE_CORRELATION_OPTIMIZATION OFF
ALTER DATABASE [SocialMedia] SET TRUSTWORTHY OFF
ALTER DATABASE [SocialMedia] SET ALLOW_SNAPSHOT_ISOLATION OFF
ALTER DATABASE [SocialMedia] SET PARAMETERIZATION SIMPLE
ALTER DATABASE [SocialMedia] SET READ_COMMITTED_SNAPSHOT OFF
ALTER DATABASE [SocialMedia] SET HONOR_BROKER_PRIORITY OFF
ALTER DATABASE [SocialMedia] SET RECOVERY FULL
ALTER DATABASE [SocialMedia] SET MULTI_USER
ALTER DATABASE [SocialMedia] SET PAGE_VERIFY CHECKSUM
ALTER DATABASE [SocialMedia] SET DB_CHAINING OFF
ALTER DATABASE [SocialMedia] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
ALTER DATABASE [SocialMedia] SET TARGET_RECOVERY_TIME = 0 SECONDS
ALTER DATABASE [SocialMedia] SET READ_WRITE
以及我用来运行它的代码:
string connString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
conn = new SqlConnection(connString);
conn.Open(); // open new connection and create command to notify about all new Chat-Messages in -ROOM_NUMBER- const
using (var sqlCommand = new SqlCommand(GetSearchQueryString(searchItems), conn))
{
var sqlDependency = new SqlDependency(sqlCommand
//handle onChange event - this will be fired whenever there is a change in database that affacts our query
sqlDependency.OnChange += (sender, e) =>
{
//change detected - get all messages...
try
{
var sql = GetSearchQueryString(searchItems);
int count = _entities.ExecuteStoreQuery<Guid>(sql).Count();
AsyncManager.Parameters["count"] = count;
}
finally
{
AsyncManager.OutstandingOperations.Decrement();
}
};
sqlCommand.ExecuteNonQuery();
}
我在程序的前面打电话给SqlDependency.Start(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
。
我错过了什么吗? 我觉得我错过了一些明显的东西...
我发现SqlDependency 非常脆弱。有时,如果您执行了无效的操作,您总是会立即收到通知。无错误消息。
尝试从查询中删除数据库名称限定。像这样引用该表:dbo.Items
我们按照 http://dimarzionist.wordpress.com/2009/04/01/how-to-make-sql-server-notifications-work/中所述的步骤进行操作
看起来您正在使用三个部分名称,即
[SocialMedia].[dbo].[Items].[Id]
文档明确指出您需要使用两个部分名称,因此
更改为 [SocialMedia].[Items].[Id]
迈克尔的回答是一个很好的来源,但有一件事Microsoft省略了:
SQL 代理将拒绝具有 计算列
如果你尝试,你会得到一个 SqlNotificationEventArgs,其中包含 info=Query,type=Subscribe,并且像我一样,你会花几个小时试图找出语句的问题,而计算列是罪魁祸首。
在 SQl 服务器中执行
使用立即回滚更改数据库 YOURDBNAME 集ENABLE_BROKER