我想讓SqlDependency對象給我通知,但到目前爲止我無法讓它工作。SqlDependency簡單選擇查詢總是返回「無效」
,我傳送給SQL查詢是這樣的:
SELECT [SocialMedia].[dbo].[Items].[Id]
FROM [SocialMedia].[dbo].[Items]
我試過它瓦特/和W/O型的支架,具有完全合格的名稱,只有變量名,和一切插圖中。
Id爲Guid
類型,我沒有看到,他們沒有在Creating a Query for Notification文章
的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]
我的DB:
USE [master]
GO
CREATE DATABASE [SocialMedia]
CONTAINMENT = NONE
ON PRIMARY
(NAME = N'SocialMedia',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DENALI\MSSQL\DATA\SocialMedia.mdf' ,
SIZE = 1110016KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB)
LOG ON
(NAME = N'SocialMedia_log',
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DENALI\MSSQL\DATA\SocialMedia_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);
。
我錯過了什麼嗎?我覺得我失去了一些明顯的...
也許儘量不要配置SqlCommand對象? – yamen 2012-04-18 22:08:47
「不起作用」是什麼意思?你能發佈錯誤嗎? – usr 2012-04-18 23:45:31
您是否已經完成了服務器端的所有必要設置? (啓用clr集成和服務代理,設置權限等) – paul 2012-04-19 09:14:34